c 流程結構

2021-10-04 18:46:26 字數 4883 閱讀 4687

選擇結構

#include

using

namespace std;

intmain01()

//②多行if語句

//使用者輸入分數,如果分數大於600,視為考上一本,在螢幕上輸出

//如果沒考上一本大學,列印未考上一本大學

int score1 = 0;

cout << "請輸入乙個考試分數:" << endl;

cin >> score1;

cout<< "您輸入的分數為:" << score1 << endl;

if (score1 > 600)

else

//③多條件if語句

//大於600視為一本大學,大於500視為考上二本大學,大於400視為考上三本大學

//小於等於400視為未考上本科生

int score2 = 0;

cout << "請輸入乙個考試分數:" << endl;

cin >> score2;

cout << "您輸入的分數為:" << score2 << endl;

if (score2 > 600)

else if(score2 > 500)

else if (score2 > 400)

else

*///巢狀if語句

//輸入乙個高考成績,大於600視為一本,大於500視為二本,大於400視為三本,其他為未考上

//一本分數中,大於700,考入北大,大於650,考入清華,大於600,考入人大

int score3 =0;

cout <<

"請輸入乙個考試分數:"

<< endl;

cin >> score3;

cout <<

"您輸入的分數為:"

<< score3 << endl;

if(score3 >

600)

else

if(score3 >

650)

else

}else

if(score3 >

500)

else

if(score3 >

400)

else

system

("pause");

return0;

}

if案例——3個人比體重

#include

using

namespace std;

intmain02()

else

}else

else

}system

("pause");

return0;

}

選擇結構三目運算子

#include

using

namespace std;

intmain03()

switch語句與if語句

#include

using

namespace std;

intmain04()

*///switch缺點:判斷時候只能是整型或者字元型,不可以是區間

//switch優點:結構清晰,執行效率高

double score1 =

0.0;

cout <<

"請您評分:"

<< endl;

cin >> score1;

cout <<

"您打的分數是:"

<< score1 << endl;

if(score1 >=

9&& score1 <=10)

else

if(score1 >=

8&& score1 <=9)

else

if(score1 >=

7&& score1 <=8)

else

if(score1 >=

6&& score1 <=7)

else

if(score1 >=

5&& score1 <=6)

else

system

("pause");

return0;

}

迴圈語句——while迴圈

#include

using

namespace std;

intmain05()

//在螢幕中列印0~9這10個數字

int num =0;

/*cout << num << endl;

num++;

cout << num << endl;*/

while

(num <10)

system

("pause");

return0;

}

迴圈語句案例——猜體重遊戲

#include

using

namespace std;

#include

intmain06()

else

if(val < num)

else

//猜對 退出遊戲

//猜錯 提示猜的結果 過大或過小 重新返回第第二步

}system

("pause");

return0;

}

do…while迴圈

#include

using

namespace std;

intmain07()

while(迴圈語句);

//先執行一次

//在螢幕中輸出0~9這10個數字

int num =0;

dowhile

(num <10)

;system

("pause");

return0;

}

do…while迴圈案例——水仙花數

#include

using

namespace std;

intmain08()

while

(num <

1000);

system

("pause");

return0;

}

for迴圈

#include

using

namespace std;

intmain09()

//列印從0到9

for(

int i =0;

/*0*/ i <10;

/*1*/i++

/*3*/

)/*執行順序 0 1 2 3 1 2 3 1 2 3*/

int j =0;

for(;;

) cout << j << endl;

j++;}

system

("pause");

return0;

}

for迴圈案例——敲桌子

#include

using

namespace std;

intmain10()

else

}system

("pause");

return0;

}

巢狀迴圈

#include

using

namespace std;

intmain11()

cout << endl;

}system

("pause");

return0;

}

巢狀迴圈案例——乘法口訣表

#include

using

namespace std;

intmain12()

cout << endl;

}system

("pause");

return0;

}

跳轉語句——break

#include

using

namespace std;

intmain13()

*/// 2.出現在迴圈語句中,作用是跳出當前迴圈語句

/*for (int i = 0; i < 10; i++)

cout << i;

}cout << endl;*/

// 3.出現在巢狀語句中,跳出最近的內層迴圈語句

for(

int i =

0; i <

9; i++

) cout <<

"* "

;}cout << endl;

}system

("pause");

return0;

}

跳轉語句——continue

#include

using

namespace std;

intmain14()

cout << i << endl;

}system

("pause");

return0;

}

跳轉語句——goto語句

#include

using

namespace std;

intmain()

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之間的數字,玩家進行猜測,如果...