跳到主要內容

計算導論與 C 語言基礎:第九週(C 語言的基本成分 —— 控制成分)

C 語言的基本成分 —— 控制成分





Switch 分支語句:分支語句的特色在於若 switch 表達式符合常量表達事,則會執行該case以下的行為,並在最後執行 default 語句。


如何打破switch 語句的問題:使用break的方式來使程式結束執行,自動跳到返回語句。




範例中的習題可以注意到,default 語句其實可以不用放在最後,若改變位置,他會依序先執行,但由於它還是一個條件出口,因此仍然會執行自己以下的行為。


再談循環語句
在這裡介紹更多的循環語句
  1. for
  2. while
  3. do…...while
  4. continue


對於 for 語句而言,會先執行 i = 0 ; 接著執行 i < 10;,確定這兩句都合格,執行語句,完成後回去執行 i++,而後重新檢視 i < 10,合格即執行語句,而後回到 i++進行動作,開始循環直到條件句不合格。


while 要小心,以免造成死循環,是符合條件才會執行語句,若不合格他就會繼續執行,直到! i = 101時,這個程序才會終止,才不會執行,因此若最終輸出 i = 101。


經典習題:如何使用count


do ….. while 語句


如何終止內循環,一樣可以使用break


注意:for 裡面一定不能是空白,至少要有兩個分號!







“Continue”轉換語句




Goto 之爭


留言

這個網誌中的熱門文章

【Android Development for Beginners】 Lesson 2A : Create a New Project , Plan How to Build the Layout, Build Layout

This Notes is according to the udacity free course : Android Development for Beginners Lesson 2A : Create a New Project Plan How to Build the Layout Build layout Follow the instruction below to build up a new app called "Just Java" use empty activity .

【 Android Development for Beginners】 Lesson 3A : Object Oriented Programming - 4

(I think Lesson 3A is the most important but also the most difficult part for beginner so far ) This Notes is according to the udacity free course : Android Development for Beginners Lesson 3A : Object Oriented Programming Call Method on an objects If We want to call a method on an object object variable name . method name ( input argument ); ex: titleTextView.setText("News"); You need to make sure you put a right object so you can call the method

R語言翻轉教室-3【01-RBasic-03-Data-Structure-Object】

*R的物件結構 組成R語言的基本物件結構為「向量」。 向量物件有兩個很重要的屬性: `mode`和`length`。 `mode`代表這個向量的值型態,總共有:`logical`、`integer`、`numeric`、`complex`、`character`和`raw`。 `length`則代表這個向量中值的個數。 list可以裝所有R的物件 所有的R物件都有「屬性」 所有R 物件的attributes都是帶有名字的list。 本課程筆記,出處: R語言翻轉教室 回家作業: 連結