C 筆記3 程式流程結構

2021-10-11 19:22:41 字數 1433 閱讀 8916

三種程式執行結構:順序結構、選擇結構、迴圈結構

1、if語句

int goal=5;

int guess;

cout<<

"請輸入乙個分數:"

>guess;

if(guess>5)

else

if(guess<5)

else

2、三目運算子

int a=

8,b=5;

cout<<

(a(a+b)

:(a-b)

)<

char ch=

'd';

switch

(ch)

1、while迴圈結構

int a=3;

while

(a>0)

while迴圈簡單demo:猜數字遊戲

#include

#include

#include

using

namespace std;

intmain()

else

cout<<

"請重新猜乙個數字:"

; cin>>guess;

} cout<<

"猜對了!"

("pause");

return0;

}

2、do…while迴圈語句

int a=3;

dowhile

(a>0)

;

3、for迴圈語句

for

(int i=

1;i<

11;i++

)

乘法表demo

for

(int i=

1;i<=

9;i++

) cout<}

1、break

2、continue

跳過本次迴圈尚未執行的語句,直接跳入下次迴圈

3、goto語句

對程式連貫性有影響,不推薦使用

int a=

1,b=2;

goto flag;

cout

"here is goto"

<

C 程式流程結構

1 if 2 if.else.3 if.else if.else 例項 輸入乙個年份,判斷是否為閏年。閏年的年份必須滿足以下兩個條件之一 1 能被4整除,但不能被100整除的年份都是閏年。2 能被400整除的年份都是閏年 include using namespace std int main if...

C 程式流程結構 選擇結構

格式 if 條件 注意1 條件後面不能加分號 語法 if 條件 else 語法 if 條件1 else if 條件2 else include include using namespace std intmain elseif 700 score 650 else elseif 600 score...

c 程式流程結構 迴圈結構

2 do while迴圈語句 3 for迴圈語句 4 巢狀迴圈 語法 while 迴圈條件 解釋 只要迴圈條件的結果為真,就執行迴圈語句。例子 列印0 9 int main system pause return0 注意1 一定要避免死迴圈。描述 隨機生成乙個1 100之間的數字,玩家進行猜測,如果...