Introduction to the C Programming Language if CSIM

  • Slides: 19
Download presentation
Introduction to the C Programming Language 選擇敘述(if) CSIM, PU C Language

Introduction to the C Programming Language 選擇敘述(if) CSIM, PU C Language

if 敘述(if statement) q if 敘述的語法格式如下: if ( expr ) { statement 1; }

if 敘述(if statement) q if 敘述的語法格式如下: if ( expr ) { statement 1; } q if 敘述的流程圖: expr True(真) Statement 1 CSIM, PU C Language 3

if 敘述----範例 範例1:檢查密碼。 q q q #include<stdio. h> #include<stdlib. h> int main() { int

if 敘述----範例 範例1:檢查密碼。 q q q #include<stdio. h> #include<stdlib. h> int main() { int passwd; q printf("請輸入你的密碼 : "); 宣告ㄧ變數為整數型態 q scanf("%d", &passwd); q if (passwd = = 8527) q printf(“密碼正確!歡迎使用本系統”); /*密碼正確*/ q system() q return 0; q } Ch 1_1_1. c CSIM, PU C Language 4

if 敘述(if-else statements) q if-else 敘述的語法格式如下: if (expr) { q if-else 敘述的流程圖: false expr

if 敘述(if-else statements) q if-else 敘述的語法格式如下: if (expr) { q if-else 敘述的流程圖: false expr Statement 1; } else { Statement 2; } CSIM, PU true Statement 2 Statement 1 C Language 5

if 敘述----範例 範例1:輸入兩數,比較大小。 q q q #include<stdio. h> #include<stdlib. h> main() { int a

if 敘述----範例 範例1:輸入兩數,比較大小。 q q q #include<stdio. h> #include<stdlib. h> main() { int a , b; q printf("請輸入兩個數字 : "); 宣告a, b兩變數為整數型態 q scanf("%d%d", &a, &b); q if (a>b) q printf("%d is bigger than %dn", a, b); /*a>b時印出*/ q else q printf("%d is less than %dn", a , b); /*a<=b時印出*/ q system("pause"); q } Ch 1_1_1. c CSIM, PU C Language 6

if 敘述----範例 範例3 程式碼:從鍵盤輸入一個任意數, 判別是偶數還是奇數。 q #include<stdio. h> q #include<stdlib. h> q q q

if 敘述----範例 範例3 程式碼:從鍵盤輸入一個任意數, 判別是偶數還是奇數。 q #include<stdio. h> q #include<stdlib. h> q q q void main() { int number, rem; printf("1 請輸入ㄧ數字以供測試= => "); scanf("%d", &number); rem = number % 2; if ( rem == 1 ) printf("2: %d 是奇數n", number); else printf("2: %d 是偶數n", number); system("pause"); } %為取餘數的符號 比較是否相等的符號 Ch 1_1_3. c CSIM, PU C Language 8

if 敘述----範例 範例4: 讓使用者輸入 " yes " 或 " no " 的答案. q q

if 敘述----範例 範例4: 讓使用者輸入 " yes " 或 " no " 的答案. q q q q CSIM, PU #include <stdio. h> #include <stdlib. h> void main(void) { 宣告變數 ch 為字元型態 char ch; 宣告變數 ans 為整數型態 int ans; printf("Type yes or no? (y/n)"); 讀取螢幕上的字元並存到變數ch中 ch = getchar(); if ((ch == ‘Y')||(ch =='y')) ans = 1; else if((ch =='N')||(ch =='n')) ans = 0; printf("The value of answer=%dn", ans); system("pause"); } Ch 1_1_4. c C Language 9

接續上頁 q diff = i-1500; /*計算里程數超過1500公尺的里程費率*/ q q q q q if (diff%500==0) /*里程數不足

接續上頁 q diff = i-1500; /*計算里程數超過1500公尺的里程費率*/ q q q q q if (diff%500==0) /*里程數不足 500公尺仍以 500公尺費率計算*/ cost=70+(diff/500)*5; else cost=70+((diff/500)+1)*5; } printf("總共車資為: %d 元", cost); printf("n"); system("pause"); return 0; } Ch 1_1_5. c CSIM, PU C Language 12

if 敘述(else if statements) q else if 敘述的語法格式如下: q else if 敘述的流程圖: if (

if 敘述(else if statements) q else if 敘述的語法格式如下: q else if 敘述的流程圖: if ( expr 1) { statement 1; Statement 1 true expr 1 } else if (expr 2) false { statement 2; } Statement 2 true expr 2 else if (expr 3) false { statement 3; } Statement 3 true expr 3 else false { statement 4; } staement 5; CSIM, PU Statement 4 Statement 5 C Language 13

if 敘述(nested if) ----範例 範例2:Nested if 的應用。 q q q q CSIM, PU #include<stdio.

if 敘述(nested if) ----範例 範例2:Nested if 的應用。 q q q q CSIM, PU #include<stdio. h> #include<stdlib. h> void main() { int a, b; printf("請輸入一個整數a : "); scanf("%d", &a); if (a<10) printf(" %d 為小於 10 的整數n", a); else if (a>10) printf(" %d 為大於 10的正整數n", a); else printf(" %d 為等於 10 的正整數n", a); system("pause"); } Ch 1_1_7. c C Language 15

if 敘述(nested if) ----(練習 1 )cont. 1 q q q q q #include<stdio. h>

if 敘述(nested if) ----(練習 1 )cont. 1 q q q q q #include<stdio. h> void main() {   int score; printf("1: Please input the score of a student ==> "); scanf("%d", &score); if ( score >= 90 ) printf("1: The score is in A grade. n"); else if ( (score >= 80) && (score<=89) ) printf("1: The score is in B grade. n"); else if ( (score >= 70) && (score<=79)) printf("1: The score is in C grade. n"); else if ( (score >= 60) && (score<=69)) printf("1: The score is in D grade. n"); else printf("1: The score is in E grade. n"); } CSIM, PU 90 80 70 60 C Language 17

簡潔版的if-else例子(補充) q #include<stdio. h> q #include<stdlib. h> q int main(void) q{ q int n

簡潔版的if-else例子(補充) q #include<stdio. h> q #include<stdlib. h> q int main(void) q{ q int n 1, n 2, larger; q printf(“please type two numbers: ”); q scanf(“%d %d”, &n 1, &n 2); q n 1>n 2 ? (larger=n 1) : (larger=n 2); q printf(“nthe larger number is : %dn”, larger); q system(“pause”); q return 0; q} CSIM, PU C Language 19