C語言基礎知識-程序流程結構
node
做者:尹正傑ide
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。oop
一.概述spa
C語言支持最基本的三種程序運行結構:順序結構,選擇結構,循環結構。
順序結構:程序按順序執行,不發生跳轉。
選擇結構:依據是否知足條件,有選擇的執行相應功能。
循環結構:依據條件是否知足,循環屢次執行某段代碼。
二.選擇結構操作系統
1>.if語句3d
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #include <stdio.h> int main(void) { int a = 100; int b = 20; if (a > b) { printf("a > b\n"); } return 0; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo if_demo.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo a > b [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
2>.if ... else語句code
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo2.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #include <stdio.h> int main(void) { int a = 100; int b = 200; if (a > b) { printf("a > b\n"); } else { printf("a < b\n"); } return 0; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo2 if_demo2.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo2 a < b [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
3>.if ... else if ...else語句blog
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo3.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #include <stdio.h> int main(void) { int a = 300; int b = 300; if (a > b) { printf("a > b\n"); } else if(a == b) { printf("a == b\n"); } else { printf("a < b\n"); } return 0; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo3 if_demo3.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo3 a == b [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
4>.三目運算符【其實其內部判斷條件和if類似,語法結構簡單明瞭】遊戲
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat if_demo4.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #include <stdio.h> int main(void) { int a = 100; int b = 300; int max; if (a > b) { max = a; } else { max = b; } printf("s1 = %d\n",max); a = 10; b = 20; max = (a > b ? a:b); //上面一大堆代碼,咱們僅僅用三目運算符一行簡寫。三目運算符格式爲"表達式?選項1[表達式]:選項2",即若是表達式爲真,選擇選項1的結果,若是爲假則選擇新選項2。 printf("s2 = %d\n",max); return 0; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o if_demo4 if_demo4.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./if_demo4 s1 = 300 s2 = 20 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
5>.switch語句【注意:if 條件語句執行效率差,switch條件語句執行效率相對較高,可是if能夠判斷一個區間,而switch則只能用來判斷一個值】ci
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat switch_demo.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #include <stdio.h> int main(void) { char c; c = getchar(); //注意該方法只會接收第一個字符喲~好比你輸入的是100,它只會接收第一個字符「1」 switch(c) //參數只能是整型變量 { case '1': printf("OK\n"); break; //switch遇到break就中斷了 case '2': printf("not OK\n"); break; default: //若是上面的條件都不知足,那麼執行default printf("are u OK?\n"); } return 0; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o switch_demo switch_demo.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 1 OK [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 2 not OK [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 3 are u OK? [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./switch_demo 5 are u OK? [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
三.循環結構
1>.while語句
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat while_demo.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int a = 1; while(a < 10) { printf("a = %d\n",a); a++; system("sleep 0.5"); } printf("程序執行完畢~\n"); return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o while_demo while_demo.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./while_demo a = 1 a = 2 a = 3 a = 4 a = 5 a = 6 a = 7 a = 8 a = 9 程序執行完畢~ [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
2>.do ... while語句
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat narcissus.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int index = 100; do { int one = 0, ten=0, hundred=0; //將一個三位數分解個位,十位,百位 hundred = index / 100; //百位 ten = index / 10 % 10; //十位 one = index % 10; //個位 if (hundred * hundred * hundred + ten * ten * ten + one * one * one == index) //各個位數的立方和等於該數自己,那麼它就是一個水仙花 { printf("%d是水仙花數\n",index); } index ++; }while(index < 1000); return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o narcissus narcissus.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./narcissus 153是水仙花數 370是水仙花數 371是水仙花數 407是水仙花數 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
3>.for循環
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int index = 100; int sum = 0; for(index = 0;index<=100;index++) //計算0-100之間全部數字的是總和 { sum += index; } printf("sum = %d\n",sum); return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo for_demo.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo sum = 5050 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_demo2.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { for(int i = 100;i<1000;i++) { int one = 0, ten=0, hundred=0; //將一個三位數分解個位,十位,百位 hundred = i / 100; //百位 ten = i / 10 % 10; //十位 one = i % 10; //個位 if (hundred * hundred * hundred + ten * ten * ten + one * one * one == i) //各個位數的立方和等於該數自己,那麼它就是一個水仙花 { printf("%d是水仙花數\n",i); } } return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_demo2 for_demo2.c -std=c99 #注意,在Linux系統咱們編譯for循環代碼時須要指定"-std=c99",不然會報錯"error: ‘for’ loop initial declarations are only allowed in C99 mode" [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_demo2 153是水仙花數 370是水仙花數 371是水仙花數 407是水仙花數 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
4>.嵌套循環(循環之間能夠相互嵌套)
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat for_99.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int i,j; #咱們提早聲明瞭變量i和j,若是咱們在這裏不聲明直接在for循環裏面聲明也是能夠的,只不過在Linux操做系統編譯時,咱們須要指定std的庫爲c99,默認使用的是c90庫。不然會報錯"error: ‘for’ loop initial declarations are only allowed in C99 mode" for(i = 1; i <= 9; i++) { for(j=1; j<=i; j++) { printf("%d x %d = %d\t",i,j,i * j); } printf("\n"); } return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o for_99 for_99.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./for_99 x 1 = 1 x 1 = 2 2 x 2 = 4 x 1 = 3 3 x 2 = 6 3 x 3 = 9 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
5>.循環語句練習題-猜數字遊戲
四.跳轉語句break和contiune語句
1>.break語句
在switch條件語句和循環語句中均可以使用break語句:
當它出如今switch條件語句時,做用是終止某個case並跳出switch結構。
當它出如今循環語句中,做用是跳出當前內循環語句,執行後面的代碼。
當它出現嵌套循環語句中,跳出最近的內層循環語句,執行後面的代碼。
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat do_while.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int a = 0; do { a++; if (a == 100) { break; } }while(a); //須要注意的是,儘管沒有上面的if條件判斷語句,該循環並不是死循環,只是執行的次數較多而已,由於a是一個有符號int類型的數字,而int類型是有上限的 printf("%d\n",a); return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o do_while do_while.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./do_while 100 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
2>.continue語句
在循環語句中,若是但願當即終止本次循環,並執行下一次循環,此時就須要使用continue語句。
[root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# cat continue_demo.c /* @author :yinzhengjie blog:http://www.cnblogs.com/yinzhengjie EMAIL:y1053419035@qq.com */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { int index = 0; while (index < 100) { index++; if(index % 7 == 0 || index % 10 == 7 || index / 10 == 7) //過濾掉帶7和7的倍數的數字 { continue; } printf("數字:%d\n",index); } return EXIT_SUCCESS; } [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# gcc -o continue_demo continue_demo.c [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]# ./continue_demo 數字:1 數字:2 數字:3 數字:4 數字:5 數字:6 數字:8 數字:9 數字:10 數字:11 數字:12 數字:13 數字:15 數字:16 數字:18 數字:19 數字:20 數字:22 數字:23 數字:24 數字:25 數字:26 數字:29 數字:30 數字:31 數字:32 數字:33 數字:34 數字:36 數字:38 數字:39 數字:40 數字:41 數字:43 數字:44 數字:45 數字:46 數字:48 數字:50 數字:51 數字:52 數字:53 數字:54 數字:55 數字:58 數字:59 數字:60 數字:61 數字:62 數字:64 數字:65 數字:66 數字:68 數字:69 數字:80 數字:81 數字:82 數字:83 數字:85 數字:86 數字:88 數字:89 數字:90 數字:92 數字:93 數字:94 數字:95 數字:96 數字:99 數字:100 [root@node101.yinzhengjie.org.cn /yinzhengjie/code/day002]#
3>.goto語句(無條件跳轉)