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 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 == 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 2; break; default: statements; }
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"); }