sequence structure selection structure iteration structure ifelse switchcase

  • Slides: 18
Download presentation
程式的結構設計 循序性結構 sequence structure 選擇性結構 selection structure 重複性結構 iteration structure

程式的結構設計 循序性結構 sequence structure 選擇性結構 selection structure 重複性結構 iteration structure

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… do-while…

控制流程 選擇性結構 if-else… switch-case 重複性結構 for… while… do-while…

if 敘述 ¡ Syntax 1: if (關係運算元) statement ; ¡ Syntax 2: if (關係運算元)

if 敘述 ¡ Syntax 1: if (關係運算元) statement ; ¡ Syntax 2: if (關係運算元) { statement 1 ; statement 2 ; . . . statement n ; }

if -- else 敘述 ¡ Syntax 3: if (關係運算元) statement ; else statement ;

if -- else 敘述 ¡ Syntax 3: if (關係運算元) statement ; else statement ; ¡ Syntax 4: if { (關係運算元) statement 1 ; statement 2 ; … } else { statement 1 ; statement 2 ; … }

if -- else 範例 #include <stdio. h> #include <stdlib. h> int main() { int

if -- else 範例 #include <stdio. h> #include <stdlib. h> int main() { int b; printf("Enter a value: "); scanf("%d", &b); if (b < 0) printf("The value is negative. "); else printf("The value is not negative"); system("PAUSE"); return 0; }

if - else #include <stdio. h> #include <stdlib. h> 若 b = 5…… int

if - else #include <stdio. h> #include <stdlib. h> 若 b = 5…… int main() { int b; printf("Enter a value: "); scanf(“%d”, b); 喔哦! FALSE! if (b < 0) printf("The value is negative. n"); 喔哦! FALSE! else if (b = = 0) printf("The value is zero. n"); YA! TRUE!! else printf("The value is positive. n"); system("PAUSE"); return 0; }

if 其他範例一 main( ) 執行結果: { 2 th step. int x=1, y=2; if( x

if 其他範例一 main( ) 執行結果: { 2 th step. int x=1, y=2; if( x == 1 && y == 3 ) printf("1 th step. n"); else if( x == 1 || y ==3 ) printf("2 th step. n"); }

switch--case(可多重選擇) switch (expression) { case const_exp 1: statements 1; break; case const_exp 2: statements

switch--case(可多重選擇) switch (expression) { case const_exp 1: statements 1; break; case const_exp 2: statements 2; break; default: statements; }

switch--case 範例一 char grade; printf("請輸入你現在就讀的年級: "); scanf(" %c", &grade); switch ( grade ) {

switch--case 範例一 char grade; printf("請輸入你現在就讀的年級: "); scanf(" %c", &grade); switch ( grade ) { case ‘ 1’: printf("喔!你是小綠綠!"); break; case ‘ 2’: printf("嗯!你是中綠綠呢!"); break; case ‘ 3’: printf("哇!你是大綠綠耶!"); break; default : printf("你不是學生嗎?"); }

switch--case 範例二 int month; printf(“請輸入現在的月份: ”); scanf(“%d”, &month); switch (month) { case 3 :

switch--case 範例二 int month; printf(“請輸入現在的月份: ”); scanf(“%d”, &month); switch (month) { case 3 : case 4 : case 5 : printf("現在是春天!n"); break; case 6 : case 7 : case 8 : printf("現在是夏天!n"); break; case 9 : case 10: case 11: printf("現在是秋天!n"); break; case 12: case 1 : case 2 : printf("現在是冬天!n"); break; default : printf("月份不存在!n"); }