跳到主要內容

計算導論與 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 之爭


留言

這個網誌中的熱門文章

【Quora 翻譯&整理】對於程式初學者嘗試解決問題的好網站推薦

原文出處: What are some suggested websites to solve programming problems for beginners? (2016/5/15) 此篇文章強調的是去參加project的那種網站推薦

第一章 電腦、網際網路與全球資訊網簡介

C++  How to Program 7/E 筆記 第一章 電腦、網際網路與全球資訊網簡介 學習目標 基本軟硬體觀念 物件技術觀念(類別、物件、屬性、行為、封裝、繼承) 程式語言的種類 典型的C++開發環境 業界標準物件導向素模語言——UML 沿革 網際網路、球球資訊網與Web 2.0現象的沿革 在Linux 的GNU C++ 與在Microsoft Visual C++環境中測試C++ 應用程式 *結構化程式設計(structured programming) *物件導向程式設計

【 Intro to Java Programming 】Lesson 1 : Introduction-5 (Algorithms)

This Notes is according to the udacity free course : Intro to Java Programming Lesson 1 : Introduction-5(Algorithms) Key Word : pseudocode Algorithms just like a doing things protocol+condition. Without protocol+condition, you can not make choice or do the decision. So if you want to buy car, you know the car speed and you don't consider the car price or the car condition, so it is your thought, it is the algorithm you buy a car. For computer, we give them the program ===>for them to do algorithm, but now we try to write done in Pseudocode. Pseudocode write in "spoken language" which help us to understand the problem so we can translate them into computer language ==>JAVA Code.org maybe it could help you have fun from learning code