分支語句和迴圈語句

2021-10-02 21:25:54 字數 4093 閱讀 2459

if語句

if(表示式) 語句;

if(表示式) 語句1;

else 語句2;

//多分支

if(表示式1)語句1;

else if(表示式2)語句2;

else 語句3;

#include int main()

else

return 0;

}

switch語句

switch語句也是一種分支語句,常常用於多分支的情況

switch(整型表示式)

其中語句項是一些case語句

default子句

如果表示式的所有case值都不匹配,程式不會終止,也不會報錯。

因此可以在任何乙個case可以出現的位置寫default。

case語句若是不搭配break語句,則會在上一條case結束後執行下一條case。

**演示

1、判斷乙個數是否為素數

#define _crt_secure_no_warnings

#include #include int main()

else

system("pause");

return 0;

}

2、輸出1-100之間的奇數

#include #include int main()

} system("pause");

return 0;

}

3、輸入數字輸出對應的星期

#define _crt_secure_no_warnings

#include #include int main()

system("pause");

return 0;

}

4、輸出週內和周麼

#define _crt_secure_no_warnings

#include #include int main()

system("pause");

return 0;

}

1、while迴圈

while(表示式)

//當條件滿足的情況下,表示式成立語句執行,直到表示式不成立,語句退出。

break在迴圈中的作用,迴圈語句遇到break,永久終止迴圈;

continue在迴圈中的作用,continue之後的語句本次迴圈不執行,直接進下一次的迴圈入口。

2、for迴圈

for(表示式1;表示式2;表示式3)

迴圈語句;

//表達1為迴圈初識條件,表示式2為條件判斷,表示式3為迭代部分。

break在迴圈中的作用,迴圈語句遇到break,永久終止迴圈;

continue在迴圈中的作用,continue之後的語句本次迴圈不執行,直接進下一次的迴圈入口。

3、do-while迴圈

do

迴圈語句;

while(表示式);

//迴圈至少執行一次

**演示

5、while語句

#include #include #if 0

int main()

printf("%d ", i);

i++;

} system("pause");

return 0;

}#else

int main()

printf("%d ", i);

} system("pause");

return 0;

}#endif

6、for迴圈

#include #include int main()

printf("%d\n", k);

system("pause");

return 0;

}

7、do-while迴圈

#include #include int main()

while (i<10);

system("pause");

return 0;

}

1、計算n的階乘

#define _crt_secure_no_warnings

#include #include int main()

printf("%d! = %d", n, res);

system("pause");

return 0;

}

2、計算階乘加法

#define _crt_secure_no_warnings

#include #include int main()

printf("%d", sum);

system("pause");

return 0;

}

3、編寫binsearch

#define _crt_secure_no_warnings

#include #include int binsearch(int x, int v, int n)

} return -1;

}int main()

; printf("%d\n", binsearch(4, arr, 10));

system("pause");

return 0;

}

4、演示多個字元從兩端向中間匯聚

#define _crt_secure_no_warnings

#include #include #define size 12

int main()

system("pause");

return 0;

}

5、模擬使用者登入

#define _crt_secure_no_warnings

#include int main()

else

} if (i == 3)

system("pause");

return 0;

}

6、猜數字遊戲

#define _crt_secure_no_warnings

#include #include #include void menu()

void game()

else if (input < random_num)

else

}}int main()

} while (input);

system("pause");

return 0;

}

7、二分查詢

#define _crt_secure_no_warnings

#include int main();

int left = 0, right = 9;

int to_find;

printf("請輸入要查詢的數字:");

scanf("%d", &to_find);

while (left <= right)

else if (arr[mid]>to_find)

else

} if (left <= right)

else

system("pause");

return 0;

}

分支語句和迴圈語句

分支語句 1.if 2.switch 迴圈語句 1.while 2.for 3.do while goto語句 c語言中由乙個分號 隔開的就是一條語句。好好學習,找到好工作,不好好學習,回家種地,好好學習和不好好學習就是兩種選擇。語法結構 if 表示式1 語句1 else if 表示式2 語句2 e...

分支語句 迴圈語句

選擇語句 1.單if語句 格式 if 條件表示式 注意 1.條件表示式的結果必須是boolean型別的 2.當if語句體中只有一條語句的時候,可以省略不寫,建議寫上 如果if語句體中有多條語句,那麼 必不可少 3.if 無論條件是否成立,那麼 中的 一定會執行 4.單if語句可以使用三目運算子改進 ...

JS 條件分支語句和迴圈語句

js 條件分支語句和迴圈語句 一 條件分支語句 單分支語句 書寫格式 if 判斷條件 判斷條件 可以是乙個表示式 變數 具體值 工作原理 條件為真,執行大括號裡面的 塊,條件為假,則不執行。例 let state 1 if state 雙分支語句 書寫格式 if 判斷條件 else 工作原理 條件為...