syntax 1: if (關係運算元) statement ; | syntax 3: if (關係運算元) statement ; else statement ; |
| syntax 2: if (關係運算元) { statement 1 ; statement 2 ; . . . statement n ; } | syntax 4: if (關係運算元) { statement 1 ; statement 2 ; . statement n ; } else { statement 1 ; statement 2 ; . statement n ; } |
Example 1: void main(void) { int x; scanf("%d",&x); if ( x >= 10 ) printf("%d >= 10\n",x); else printf("%d < 10\n",x); } Result: 10 ---> 此處為鍵盤輸入值 10 >= 10 | Example 2: void main(void) { char c; scanf("%c",&c); if( c == 'A' ) { printf("c=%c\n",c); printf("This is true.\n"); } else { printf("c!=A\n"); printf("That isn't true.\n"); } Result: A -----> 此處為鍵盤輸入值 c=A This is true |
| Syntax 1: while ( 關係運算元 ) statement ; | Syntax 2: while ( 關係運算元 ) {statement 1 ; statement n ; } |
Example 1: main( ) { int i=0; while ( i < 5 ) printf("%d\n",i++); printf("out of loop.\n"); } Result = ????? | Example 2: main( ) { int i=0,c=0; while ( i < 5 ) { printf("%d",i++); printf("%d\n",++c); } printf("out of loop.\n");} Result = ????? |
Syntax 1: do statement ; while ( 關係運算元 ) ; | Example 1: main( ) { int i=0,c=0; do { printf("%d",i++); printf("%d\n",++c); } while ( i < 5 ) ; printf("We're out of the loop.\n"); } Result = ????? |
| Syntax 2: do { statement 1 ; statement 2 ; . statement n ; } while ( 關係運算元 ) ; |
| Example 1: main( ) { int i; for( i = 0 ; i <= 5 ; ++i ) printf("%d\n",i); } Result = ????? | Example 2:main( ){int i,c; for ( i=0,c=1 ; i < 5 ; ++i,++c ) { printf("%d",i); printf("%d\n",c); } printf("Out of Loop.\n");}Result = ????? |
Example 1: main( ) { char var='D'; switch (var ) { case 'A' : printf("var = A\n"); break; case 'B' : printf("var = B\n"); break; case 'C' : printf("var =C\n"); break; default : printf("I'm in default."); } /* end of switch */ } /*end of main */ | Example 2: main( ) { int var=3; switch ( var ) { case 1 : printf("var = 1\n"); break; case 2 : printf("var = 2\n"); break; case 3 : printf("var = 3\n"); break; default : printf("I'm in default"); } /* end of switch */ }/*end of main */ |
Syntax : goto label ; label : | Example: main( ) { int i=1111; repeat : printf("%d\n",i); goto repeat ; } |
| 歡迎光臨 中央論壇 - CENTER BBS (https://centerbbs.com/) | Powered by Discuz! X3 |