Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
C Programming
Lecture no. 2
More on Control Statements
Faculty of Informatics, BUU 885288 C Programming 2
ค ำสงในกำรควบคมโปรแกรม
Faculty of Informatics, BUU 885288 C Programming 3
ค ำสง if อยำงงำย • ค ำสง if เปนค ำสงส ำหรบกำร
สรำงสำยกำรควบคมกำรท ำงำนออกเปน 2 สำย
• รปแบบ
if (condition)
statement;
Ex:
if (height >= 180) {
printf(“You are very Tall\n”);
}
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
Faculty of Informatics, BUU 885288 C Programming 5
Flow Chart ของค ำสงเงอนไข
Condition
Statement 1 Statement 2
Yes No
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;
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;
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;
}
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”);
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…
Faculty of Informatics, BUU 885288 C Programming 11
if statement
if (Age < 18)
{
printf(“A child!\n“);
}
Faculty of Informatics, BUU 885288 C Programming 12
if-else statement
if (Age < 18)
{
printf(“A child!\n“);
}
else
{
printf(“An adult!\n“);
}
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“);
}
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.
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...
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
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?
Faculty of Informatics, BUU 885288 C Programming 18
Activity Break
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“);
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
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
{
:
}
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?
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”);
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;
Faculty of Informatics, BUU 885288 C Programming 25
รปแบบ switch(variable) { case constant1: statement1; break; case constant2: statement2; break; } variable = ตวแปร หรอ นพจน constant = คาคงทชนด int หรอ char ทเปนตวเลอกท างาน
ค ำสง switch
ค ำสงเงอนไข Switch
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
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 }
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");
}
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); }
END