บทที่ 1 · Web viewบทค ดย อว ทยาน พนธ ภาษาไทยและภาษาอ งกฤษ ในร ปแบบ .doc (Microsoft Word
C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C...
Transcript of C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C...
![Page 1: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/1.jpg)
C Programming
Lecture no. 2
More on Control Statements
![Page 2: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/2.jpg)
Faculty of Informatics, BUU 885288 C Programming 2
ค ำสงในกำรควบคมโปรแกรม
![Page 3: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/3.jpg)
Faculty of Informatics, BUU 885288 C Programming 3
ค ำสง if อยำงงำย • ค ำสง if เปนค ำสงส ำหรบกำร
สรำงสำยกำรควบคมกำรท ำงำนออกเปน 2 สำย
• รปแบบ
if (condition)
statement;
Ex:
if (height >= 180) {
printf(“You are very Tall\n”);
}
![Page 4: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/4.jpg)
Faculty of Informatics, BUU 885288 C Programming 4
ค ำสงเงอนไข if...else
รปแบบ
if <condition> statement1;
else statement2;
condition = เงอนไขในการตดสนใจ statement = ค าสงหรอกลมค าสง
ตวอยำง
if (score>80) printf (“Excellent”);
else printf (“Good”);
ค ำสง if / else
![Page 5: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/5.jpg)
Faculty of Informatics, BUU 885288 C Programming 5
Flow Chart ของค ำสงเงอนไข
Condition
Statement 1 Statement 2
Yes No
![Page 6: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/6.jpg)
Faculty of Informatics, BUU 885288 C Programming 6
Activity Break
if (x == 0.0)
printf ("x is equal to 0.\n");
y = 0;
else
printf ("x is greater or less
than 0.\n");
y = 1;
![Page 7: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/7.jpg)
Faculty of Informatics, BUU 885288 C Programming 7
Activity Break
if (x == 0.0) {
printf("x is equal to 0.\n");
y = 0;
}
else
printf("x is greater or less than 0.\n");
y = 1;
![Page 8: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/8.jpg)
Faculty of Informatics, BUU 885288 C Programming 8
Activity Break
if (x == 0.0) {
printf("x is equal to 0.\n");
y = 0;
}
else {
printf("x is greater or less than 0.\n");
y = 1;
}
![Page 9: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/9.jpg)
Faculty of Informatics, BUU 885288 C Programming 9
รปแบบ if <condition1> statement1;
else if <condition2> statement2;
else statement3;
ตวอยำง 0 <= x <= 69 : Standard
70 <= x <= 79 : Good
x >= 80 : Very Good
ค ำสง if / else หลำยทำงเลอก
ค ำสงเงอนไข if...else if
if (score >= 80)
printf (“Very Good”);
else if (score >= 70)
printf (“Good”);
else
printf (“Standard”);
![Page 10: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/10.jpg)
Faculty of Informatics, BUU 885288 C Programming 10
If-then-else – a review
Let’s start out by reviewing what we know about the if-
then-else statement…
![Page 11: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/11.jpg)
Faculty of Informatics, BUU 885288 C Programming 11
if statement
if (Age < 18)
{
printf(“A child!\n“);
}
![Page 12: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/12.jpg)
Faculty of Informatics, BUU 885288 C Programming 12
if-else statement
if (Age < 18)
{
printf(“A child!\n“);
}
else
{
printf(“An adult!\n“);
}
![Page 13: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/13.jpg)
Faculty of Informatics, BUU 885288 C Programming 13
if-else-if statement
if (Age < 13)
{
printf(“A child!\n“);
}
else if ((Age >= 13 ) && (Age <= 17))
{
printf(“A teenager!\n“);
}
else
{
printf(“An adult!\n“);
}
![Page 14: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/14.jpg)
Faculty of Informatics, BUU 885288 C Programming 14
Braces { }
if (Age < 13)
printf(“A child!\n“);
else if ((Age >= 13 ) && (Age <= 17))
printf(“A teenager!\n“);
else
printf(“An adult!\n“);
Not required if branch has only one statement. We recommend you always use braces in this course. Sometimes we omit them to fit our examples on a slide.
![Page 15: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/15.jpg)
Faculty of Informatics, BUU 885288 C Programming 15
Nested if/else
if (Gender == ‘M’) if (Age < 18)
printf(“A male child!\n“); else
printf(“A man!\n“); else
if (Age < 18)
printf(“A female child!\n“); else
printf(“A woman!\n“);
Nested if/else is different from if-else-if...
![Page 16: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/16.jpg)
Faculty of Informatics, BUU 885288 C Programming 16
Nested if/else vs if-then-else-if
Gender
= ‘M’
else
Age
< 18
else else Age
< 18
male
child
man female
child
woman
Age
< 13 Age >= 13
&&
Age <= 17
Age
> 17
child
teenager
adult
![Page 17: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/17.jpg)
Faculty of Informatics, BUU 885288 C Programming 17
Activity
Can our nested if/else example be re-code as an if-then-else-
if?
If so, do so!
If so, can a nested if/else always be re-coded as an if-then-
else-if?
If so, why not always use if-then-else-if instead of a nested
if/else?
![Page 18: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/18.jpg)
Faculty of Informatics, BUU 885288 C Programming 18
Activity Break
![Page 19: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/19.jpg)
Faculty of Informatics, BUU 885288 C Programming 19
Activity Feedback
if ((Gender == ‘M’) && (Age < 18)) printf(“A male child!\n“); else if (Gender == ‘M’) printf(“A man!\n“); else if (Age < 18)
printf(“A female child!\n“); else
printf(“A woman!\n“);
![Page 20: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/20.jpg)
Faculty of Informatics, BUU 885288 C Programming 20
Activity Feedback
Nested if/else can always be re-coded as an if-then-else-if.
We do not always use if-then-else-if instead of a nested
if/else because:
• conditions become more complicated
• we may wish to perform some common
processing for all cases in a sub-branch
![Page 21: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/21.jpg)
Faculty of Informatics, BUU 885288 C Programming 21
Activity Feedback
if (Gender == ‘M’) {
NbrMales = NbrMales + 1;
if (Age < 18)
printf(“A male child!\n”); else
printf(“A man!\n”); }
else
{
:
}
![Page 22: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/22.jpg)
Faculty of Informatics, BUU 885288 C Programming 22
Handling other cases
One must always be aware of the need to handle other
cases..
What are the cases that we are expected to be able to
handle?
Do we need to consider the possibility of encountering other
cases?
If so, how should we handle them?
![Page 23: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/23.jpg)
Faculty of Informatics, BUU 885288 C Programming 23
Nested if/else
if (Gender == ‘M’) if (Age < 18)
printf(“A male child!\n”);
else
printf(“A man!\n”);
else if (Gender == ‘F’)
if (Age < 18)
printf(“A female child!\n”);
else
printf(“A woman!\n”); else
printf(“Unknown gender!\n”);
![Page 24: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/24.jpg)
Faculty of Informatics, BUU 885288 C Programming 24
Activity (เงอนไขทไมจ ำเปน)
if (x < 0.0) {
range = 0;
}
else if (x >= 0.0 && x < 10.0) {
range = 1;
}
else if (x >= 10.0 && x < 100.0) {
range = 2;
}
else
range = 3;
if (x < 0.0) {
range = 0;
}
else if (x < 10.0) {
range = 1;
}
else if (x < 100.0) {
range = 2;
}
else
range = 3;
![Page 25: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/25.jpg)
Faculty of Informatics, BUU 885288 C Programming 25
รปแบบ switch(variable) { case constant1: statement1; break; case constant2: statement2; break; } variable = ตวแปร หรอ นพจน constant = คาคงทชนด int หรอ char ทเปนตวเลอกท างาน
ค ำสง switch
ค ำสงเงอนไข Switch
![Page 26: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/26.jpg)
Faculty of Informatics, BUU 885288 C Programming 26
Flow Chart ของค ำสงแบบหลำยเงอนไข
Condition 1
Condition 2
Condition 3 Statement 1
Statement 2
Statement 3
No
No
No
Yes
Yes
Yes
Statement 4
![Page 27: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/27.jpg)
Faculty of Informatics, BUU 885288 C Programming 27
If-else-if
if (day == 1)
printf ("Sunday\n");
else if (day == 2)
printf ("Monday\n");
else if (day == 3)
printf ("Tuesday\n");
else if (day ==4)
printf ("Wednesday\n");
else if (day ==5)
printf ("Thursday\n");
else if (day ==6)
printf ("Friday\n");
else if (day ==7)
printf ("Saturday\n");
else printf ("Unknown\n")
switch (expression)
{
case constant1 :
statement_sequence1
break;
case constant2 :
statement_sequence2
break;
.
.
.
case constantN :
statement_sequenceN
break;
default :
statement_sequenceN+1 }
![Page 28: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/28.jpg)
Faculty of Informatics, BUU 885288 C Programming 28
If-else-if and Switch
if (day == 1)
printf ("Sunday\n");
else if (day == 2)
printf ("Monday\n");
else if (day == 3)
printf ("Tuesday\n");
else if (day ==4)
printf ("Wednesday\n");
else if (day ==5)
printf ("Thursday\n");
else if (day ==6)
printf ("Friday\n");
else if (day ==7)
printf ("Saturday\n");
else
printf ("Unknown\n")
switch (day)
{
case 1 : printf ("Sunday\n");
break;
case 2 : printf ("Monday\n");
break;
case 3 : printf ("Tuesday\n");
break;
case 4 : printf ("Wednesday\n");
break;
case 5 : printf ("Thursday\n");
break;
case 6 : printf ("Friday\n");
break;
case 7 : printf ("Saturday\n");
break;
default : printf ("Unknown option! \n");
}
![Page 29: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/29.jpg)
Faculty of Informatics, BUU 885288 C Programming 29
Activity
switch (digit) { case 2: case 3: case 5: case 7: printf("%d is a prime number.\n", digit); break; default: if (digit >= 0 && digit <= 9) printf("%d is not a prime number.\n", digit); else printf("Invalid option: %d\n", digit); }
![Page 30: C Programming - Burapha Universitykrisana/885288/...Faculty of Informatics, BUU 9 885288 C Programming ร ปแบบ if statement1; else if](https://reader034.fdocuments.net/reader034/viewer/2022042301/5eccc745817f61116127b260/html5/thumbnails/30.jpg)
END