computer science department ftsm control structure: selection (part 2) knowledge: understand various...
TRANSCRIPT
Computer Science Department FTSMFTSM
Control Structure: Control Structure: SelectionSelection(Part 2)(Part 2)
Knowledge:Understand various concepts of selection control structure
Skill:Be able to develop a program containing selection control structure
TK1913-C ProgrammingTK1913-C Programming 22
ifif StatementsStatements
ifif
if - elseif - else
if – else - ifif – else - if
TK1913-C ProgrammingTK1913-C Programming 33
ifif StatementStatementThe structure is similar to single selection (flowchart)
Syntax:if (expression)if (expression)
statement;statement;or
if (expression) {if (expression) {statement1;statement1;statement2;statement2;
}}
Syntax:if (expression)if (expression)
statement;statement;or
if (expression) {if (expression) {statement1;statement1;statement2;statement2;
}}
Don’t forget the Don’t forget the curly brackets !!curly brackets !!
Don’t forget the Don’t forget the brackets !!brackets !!
TK1913-C ProgrammingTK1913-C Programming 44
ifif StatementStatementThe similarity between single selection structure and
ifif statement:
Single Selection:if <condition is true> start
step 1
step 2
…
step k
end_if
Single Selection:if <condition is true> start
step 1
step 2
…
step k
end_if
if Statement:
if (<condition>) {
statement 1
statement 2
…statement k
}
if Statement:
if (<condition>) {
statement 1
statement 2
…statement k
}
TK1913-C ProgrammingTK1913-C Programming 55
ifif Statement Statement Example:int num1, num2, min;printf(“Key-in 2 numbers: “);scanf(“%d%d”, &num1, &num2);min = num1;if (num1 > num2)
min = num2;printf(“Smallest: %d\n”, min);
Example:int num1, num2, min;printf(“Key-in 2 numbers: “);scanf(“%d%d”, &num1, &num2);min = num1;if (num1 > num2)
min = num2;printf(“Smallest: %d\n”, min);
Key-in 2 numbers: _
num2 ?
num1 ?
min ?
Key-in 2 numbers: 20 15
_
20
15
2015
Key-in 2 numbers: 20 15
Smallest: 15
_
20 > 15?20 > 15?
TK1913-C ProgrammingTK1913-C Programming 66
ifif Statement Statement Example:int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark > 80) {
printf(“Category: Excellent\n”);
printf(“Congratulations!”);
}
Example:int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark > 80) {
printf(“Category: Excellent\n”);
printf(“Congratulations!”);
}
Mark: _
mark ?92 > 80?92 > 80? 92
Mark: 92
_
Mark: 92
Category: Excellent
Mark: 92
Category: Excellent
Congratulations!
TK1913-C ProgrammingTK1913-C Programming 77
ifif Statement StatementExample:void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);
printf(“Your mark is %d”, mark);}
Example:void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);
printf(“Your mark is %d”, mark);}
What will the output What will the output be if the mark is 65?be if the mark is 65?
TK1913-C ProgrammingTK1913-C Programming 88
ifif Statement StatementExample:void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);printf(“Your mark is %d”, mark);
}
Example:void main() {
int mark;
printf(“Mark: “);
scanf(“%d”, &mark);
if (mark >= 50)
printf(“Pass\n”);printf(“Your mark is %d”, mark);
}
What will the output What will the output be if the mark is 35?be if the mark is 35?
TK1913-C ProgrammingTK1913-C Programming 99
if - elseif - else StatementStatementThe structure is similar to double selection (flowchart)
Syntax:if (expression)if (expression)
statement;statement;elseelse
statement;statement;or
if (expression) {if (expression) {statement1;statement1;statement2;statement2;
} else} elsestatement3;statement3;
Syntax:if (expression)if (expression)
statement;statement;elseelse
statement;statement;or
if (expression) {if (expression) {statement1;statement1;statement2;statement2;
} else} elsestatement3;statement3;
TK1913-C ProgrammingTK1913-C Programming 1010
if - elseif - else StatementStatement
orif (expression) {if (expression) {
statement1;statement1;statement2;statement2;
} else {} else {statement3;statement3;statement4;statement4;
}}
orif (expression) {if (expression) {
statement1;statement1;statement2;statement2;
} else {} else {statement3;statement3;statement4;statement4;
}}
TK1913-C ProgrammingTK1913-C Programming 1111
if – elseif – else StatementStatementThe similarity between double selection structure and if - elseif - else statement:
Double Selection:if <condition is true> start
step 1…step k
end_ifelse start
step 1…step n
end_else
Double Selection:if <condition is true> start
step 1…step k
end_ifelse start
step 1…step n
end_else
if Statement:if <condition> {
statement 1…statement k
}else {
statement 1…statement n
}
if Statement:if <condition> {
statement 1…statement k
}else {
statement 1…statement n
}
TK1913-C ProgrammingTK1913-C Programming 1212
if - elseif - else Statement Statement Example:if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %d\n”, min);
Example:if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %d\n”, min);
num2 15
num1 10
min ?
10 < 15?10 < 15?
_
10
Smallest: 10
_
TK1913-C ProgrammingTK1913-C Programming 1313
if - elseif - else Statement Statement Example:if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %d\n”, min);
Example:if (num1 < num2)
min = num1;
else
min = num2;
printf(“Smallest: %d\n”, min);
num2 15
num1 20
min ?
20 < 15?20 < 15?
_
15
Smallest: 15
_
TK1913-C ProgrammingTK1913-C Programming 1414
if - elseif - else Statement Statement Example:if (num1 < num2) {
min = num1;max = num2;
}else {
min = num2;max = num1;
}printf(“Min = %d, Max = %d\n”, min, max);
Example:if (num1 < num2) {
min = num1;max = num2;
}else {
min = num2;max = num1;
}printf(“Min = %d, Max = %d\n”, min, max);
num2 125
num1 700
min ??
700 < 125?700 < 125?
_
max ??
Min = 125, Max = 700
_
125
700
TK1913-C ProgrammingTK1913-C Programming 1515
if – else if – else StatementStatementExample:void main() {
int mark;
printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}
Example:void main() {
int mark;
printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}
What will the output What will the output be if the mark is 21?be if the mark is 21?
What will the output What will the output be if the mark is 74?be if the mark is 74?
TK1913-C ProgrammingTK1913-C Programming 1616
if – else if – else StatementStatementExample:void main() {
int mark;
printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}
Example:void main() {
int mark;
printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}
What will the output What will the output be if the mark is 74?be if the mark is 74?
What will the output What will the output be if the mark is 14?be if the mark is 14?
TK1913-C ProgrammingTK1913-C Programming 1717
if – else if – else StatementStatementExample:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else {
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}}
Example:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);else {
printf(“Fail\n”);printf(“Your mark is %d”, mark);
}}
What will the output What will the output be if the mark is 14?be if the mark is 14?
What will the output What will the output be if the mark is 70?be if the mark is 70?
TK1913-C ProgrammingTK1913-C Programming 1818
Take a break ….Take a break ….
Why we need program Why we need program styles?….To ensure styles?….To ensure your program is your program is readable. Let’s look readable. Let’s look some examples..some examples..
Let’s have a minute break. Let’s have a minute break. While you’re having a break, While you’re having a break, let me introduce you some let me introduce you some program styles…..program styles…..
TK1913-C ProgrammingTK1913-C Programming 1919
Take a break … (Learn styles)Take a break … (Learn styles)Example:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);
else printf(“Fail\n”);
printf(“Your mark is %d”, mark);}
Example:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)
printf(“Pass\n”);
else printf(“Fail\n”);
printf(“Your mark is %d”, mark);}
TK1913-C ProgrammingTK1913-C Programming 2020
Take a break … (Learn styles)Take a break … (Learn styles)Example:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)printf(“Pass\n”);
else printf(“Fail\n”);
printf(“Your mark is %d”, mark);}
Example:void main() {
int mark;printf(“Mark: “);scanf(“%d”, &mark);if (mark >= 50)printf(“Pass\n”);
else printf(“Fail\n”);
printf(“Your mark is %d”, mark);}
Difficult to read!!!Difficult to read!!!Don’t you think Don’t you think so??so??
TK1913-C ProgrammingTK1913-C Programming 2121
Syntax:if (expression)if (expression)
statement;statement;
Syntax:if (expression)if (expression)
statement;statement;
if statementif statement
Let’s recap …Let’s recap …
Syntax:if (expression)if (expression)
statement;statement;else else
statement;statement;
Syntax:if (expression)if (expression)
statement;statement;else else
statement;statement;
if-else statementif-else statement
Ok now, let’s look at Ok now, let’s look at if – else –ifif – else –if statement statement
TK1913-C ProgrammingTK1913-C Programming 2222
if – else - if if – else - if StatementStatement
Syntax:if (expression)if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;
Syntax:if (expression)if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;
if-else-if statementif-else-if statementSyntax:
if (expression)if (expression)statement;statement;
else if (expression)else if (expression)statement;statement;
else if (expression)else if (expression)statement;statement;
elseelsestatement;statement;
Syntax:if (expression)if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;elseelse
statement;statement;
if-else-if statementif-else-if statement
TK1913-C ProgrammingTK1913-C Programming 2323
if – else - if if – else - if StatementStatement
Syntax:if (expression)if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;else else (expression)(expression)
statement;statement;
Syntax:if (expression)if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;else if (expression)else if (expression)
statement;statement;else else (expression)(expression)
statement;statement;
if-else-if statementif-else-if statement
Be careful…common Be careful…common mistake made by mistake made by
students !!students !!
TK1913-C ProgrammingTK1913-C Programming 2424
Let’s recap …Let’s recap …Example:
if <condition_1 is true> startstep m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Example:if <condition_1 is true> start
step m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Multiple SelectionMultiple Selection
Assume condition 1 is Assume condition 1 is true, so …true, so …
step m, step …. will be step m, step …. will be executedexecuted
TK1913-C ProgrammingTK1913-C Programming 2525
Let’s recap …Let’s recap …Example:
if <condition_1 is true> startstep m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Example:if <condition_1 is true> start
step m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Multiple SelectionMultiple Selection
Assume condition 1 is Assume condition 1 is false, so …false, so …
• step m, step …. will be step m, step …. will be skipped, and skipped, and
• condition 2 will be testedcondition 2 will be tested
TK1913-C ProgrammingTK1913-C Programming 2626
Let’s recap …Let’s recap …Example:
if <condition_1 is true> startstep m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Example:if <condition_1 is true> start
step m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Multiple SelectionMultiple Selection
Assume condition 2 is Assume condition 2 is true, so …true, so …
step n, step …. will be step n, step …. will be executedexecuted
TK1913-C ProgrammingTK1913-C Programming 2727
Let’s recap …Let’s recap …Example:
if <condition_1 is true> startstep m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Example:if <condition_1 is true> start
step m…
end_ifif <condition_2 is true> start
step n…
end_ifelse start
step x…
end_else
Multiple SelectionMultiple Selection
Assume condition 2 is also Assume condition 2 is also false, so …false, so …
• step n, step …. will be step n, step …. will be skipped, and skipped, and
• step x will be executedstep x will be executed
TK1913-C ProgrammingTK1913-C Programming 2828
Multiple Selection in CMultiple Selection in CExample:
#include <stdio.h>int main( ) {
char letter;
scanf(“%c”, &letter);if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower case\n”);else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper case\n”);else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digit\n”);else
printf(“Special character\n”);}
Example:#include <stdio.h>int main( ) {
char letter;
scanf(“%c”, &letter);if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower case\n”);else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper case\n”);else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digit\n”);else
printf(“Special character\n”);}
Is the letter a lower case? Is the letter a lower case?
Is the letter an upper case? Is the letter an upper case?
Is the letter a digit? Is the letter a digit?
TK1913-C ProgrammingTK1913-C Programming 2929
Multiple Selection in CMultiple Selection in CExample:
#include <stdio.h>int main( ) {
char letter;
scanf(“%c”, &letter);if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower case\n”);else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper case\n”);else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digit\n”);else
printf(“Special character\n”);}
Example:#include <stdio.h>int main( ) {
char letter;
scanf(“%c”, &letter);if (letter >= ‘a’ && letter <= ‘z’ )
printf(“Lower case\n”);else if (letter >= ‘A’ && letter <= ‘Z’)
printf(“Upper case\n”);else if (letter >= ‘0’ && letter <= ‘9’)
printf(“Digit\n”);else
printf(“Special character\n”);}
(the letter is a lower case) (the letter is a lower case) truetrue
(the letter is a lower case) (the letter is a lower case) falsefalse(the letter is an upper case) (the letter is an upper case) truetrue(the letter is a lower case) (the letter is a lower case) falsefalse
(the letter is an upper case) (the letter is an upper case) falsefalse(the letter is a digit) (the letter is a digit) truetrue
(the letter is a lower case) (the letter is a lower case) falsefalse(the letter is an upper case) (the letter is an upper case) falsefalse
(the letter is a digit) (the letter is a digit) falsefalse
TK1913-C ProgrammingTK1913-C Programming 3030
ExerciseExerciseDevelop a program for the following problem.
Given a mark, determine its grade based on the table below:
74 < mark < 100 grade = A64 < mark < 75 grade = B54 < mark < 65 grade = C39 < mark < 55 grade = D 0 < mark < 40 grade = E others error message
TK1913-C ProgrammingTK1913-C Programming 3131
AAnnsswweerr11
int mark;printf(“Key-in the mark: “);scanf(“%d”,&mark);if ((mark >= 75) && (mark <= 100))
printf("Grade = A”);else if ((mark >= 65) && (mark <= 74))
printf(" Grade = B”); else if ((mark >= 55) && (mark <= 64))
printf(" Grade = C”);else if ((mark >= 40) && (mark <= 54))
printf(" Grade = D”);else if ((mark >= 0) && (mark <= 39))
printf(" Grade = E”);else
printf(“Input error\n”);
TK1913-C ProgrammingTK1913-C Programming 3232
int mark;char grade ;printf(“Key-in the mark : “);scanf(“%d”,&mark);if ((mark >= 75) && (mark <= 100))
grade =‘A’;else if ((mark >= 65) && (mark <= 74))
grade =‘B’; else if ((mark >= 55) && (mark <= 64))
grade =‘C’;else if ((mark >= 40) && (mark <= 54))
grade =‘D’;else if ((mark >= 0) && (mark <= 39))
grade =‘E’;else
printf(“Input error\n”);printf(“Your grade is %c”, grade );
AAnnsswweerr22
TK1913-C ProgrammingTK1913-C Programming 3333
int mark;char grade;printf(“Key-in the mark: “);scanf(“%d”,&mark);if ((mark >= 75) && (mark <= 100))
grade=‘A’;else if ((mark >= 65) && (mark <= 74))
grade=‘B’; else if ((mark >= 55) && (mark <= 64))
grade=‘C’;else if ((mark >= 40) && (mark <= 54))
grade=‘D’;else if ((mark >= 0) && (mark <= 39))
grade=‘E’;if ((mark > 100) || (mark < 0))
printf(“Input error\n”);else
printf(“Your grade is %c”, grade);
AAnnsswweerr33
TK1913-C ProgrammingTK1913-C Programming 3434
Nested Nested ififss
ifif statement that contains other ifif / if - elseif - else statements
TK1913-C ProgrammingTK1913-C Programming 3535
Nested Nested ififssExample:if (age > 18) {
if (age > 55) price = 2.50; /* Price for senior citizens */
elseprice = 5.00; /* Price for adults */
}else {
if (age < 1)price = 0.0; /* Price for infants */
elseprice = 1.50; /* for children & teenagers*/
}
Example:if (age > 18) {
if (age > 55) price = 2.50; /* Price for senior citizens */
elseprice = 5.00; /* Price for adults */
}else {
if (age < 1)price = 0.0; /* Price for infants */
elseprice = 1.50; /* for children & teenagers*/
}
This price is valid for This price is valid for people: age > 55people: age > 55
This price is valid for This price is valid for people: 18 < age < 55people: 18 < age < 55
This price is valid for This price is valid for people: age < 1people: age < 1
This price is valid for This price is valid for people: 1 < age < 18people: 1 < age < 18
TK1913-C ProgrammingTK1913-C Programming 3636
Nested Nested ififs - Problems - ProblemExample:if (age > 18)
if (age > 55) price = 2.50; /* Price for senior citizens */
elseprice = 5.00; /* Price for adults */
Example:if (age > 18)
if (age > 55) price = 2.50; /* Price for senior citizens */
elseprice = 5.00; /* Price for adults */
Which Which ifif does this does this elseelse belong to? belong to?
TK1913-C ProgrammingTK1913-C Programming 3737
Nested Nested ififs - Problems - Problem
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
if (age > 18) {
if (age > 55)
price = 2.50;
}
else
price = 5.00;
if (age > 18) {
if (age > 55)
price = 2.50;
}
else
price = 5.00;
Which one?
TK1913-C ProgrammingTK1913-C Programming 3838
Nested Nested ififs - Problems - ProblemBy default, elseelse will be attached to the nearest ifif
if (age > 18)
if (age > 55)
price = 2.50;
else
price = 5.00;
if (age > 18)
if (age > 55)
price = 2.50;
else
price = 5.00;
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
if (age > 18) {
if (age > 55)
price = 2.50;
else
price = 5.00;
}
TK1913-C ProgrammingTK1913-C Programming 3939
switchswitch and and breakbreak The structure is similar to multiple selection (flowchart)
Syntax:switch (expression) {switch (expression) {case expression1:case expression1:
statement1;statement1;break;break;
case espression2:case espression2:statement2;statement2;break;break;
……default:default:
expressionX;expressionX;break;break;
}}
Syntax:switch (expression) {switch (expression) {case expression1:case expression1:
statement1;statement1;break;break;
case espression2:case espression2:statement2;statement2;break;break;
……default:default:
expressionX;expressionX;break;break;
}}
Syntax:switchswitch (expression) { (expression) {
casecase expression1: expression1:statement1;statement1;break;break;
casecase espression2: espression2:statement2;statement2;break;break;
……defaultdefault::
expressionX;expressionX;break;break;
}}
Syntax:switchswitch (expression) { (expression) {
casecase expression1: expression1:statement1;statement1;break;break;
casecase espression2: espression2:statement2;statement2;break;break;
……defaultdefault::
expressionX;expressionX;break;break;
}}
Don’t forget the Don’t forget the brackets !!brackets !!
Don’t forget the Don’t forget the curly brackets !!curly brackets !!Don’t forget the Don’t forget the
colons !!colons !!
TK1913-C ProgrammingTK1913-C Programming 4040
switchswitch and and breakbreak Important Rule !!
Syntax:switchswitch (expression) { (expression) {
casecase expression1: expression1:statement1;statement1;break;break;
casecase espression2: espression2:statement2;statement2;break;break;
……defaultdefault::
expressionX;expressionX;break;break;
}}
Syntax:switchswitch (expression) { (expression) {
casecase expression1: expression1:statement1;statement1;break;break;
casecase espression2: espression2:statement2;statement2;break;break;
……defaultdefault::
expressionX;expressionX;break;break;
}}
Must be Must be INTEGER or INTEGER or
CHAR !CHAR !
TK1913-C ProgrammingTK1913-C Programming 4141
switchswitch and and breakbreakExample: switch (month) {
case 1:printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Assume month = 1, Assume month = 1, so …so …
……this step will be this step will be executed. Later …executed. Later ………casecase is terminated is terminated here. Jump to …here. Jump to …
January
_
January
End _
TK1913-C ProgrammingTK1913-C Programming 4242
switchswitch and and breakbreakExample: switch (month) {
case 1:printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
……this step will be this step will be executed. Later …executed. Later …
March
_
March
End _
Assume month = 3, Assume month = 3, so …so …
……casecase is terminated is terminated here. Jump to …here. Jump to …
TK1913-C ProgrammingTK1913-C Programming 4343
switchswitch and and breakbreakExample: switch (month) {
case 1:printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
}printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);break;
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
}printf(“End”);
Now…what will Now…what will happen if this happen if this breakbreak is taken out from the is taken out from the
program?program?
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
No more !!No more !!
TK1913-C ProgrammingTK1913-C Programming 4444
switchswitch and and breakbreakExample: switch (month) {
case 1:printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
……this step will be this step will be executed. Later …executed. Later …
February
_
February
March
_
Assume month = 2, Assume month = 2, so …so …
……casecase is terminated is terminated here. Jump to …here. Jump to …
February
March
End _
……execution continues. Thus, execution continues. Thus, this step is executed . So …this step is executed . So …
TK1913-C ProgrammingTK1913-C Programming 4545
switchswitch and and breakbreakExample: switch (month) {
case 1:printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Example: switch (month) {case 1:
printf(“January\n”);break;
case 2:printf(“February\n”);
case 3:printf(“March\n”);break;
default:printf(“Others\n”);break;
} printf(“End”);
Now…what will Now…what will happen if these happen if these
breakbreaks are taken out s are taken out from the program?from the program?
And … And … if month = 1 ?if month = 1 ?And … And … if month = 34 ?if month = 34 ?
TK1913-C ProgrammingTK1913-C Programming 4646
ExerciseExercise
To practice what you’ve learned, try exercises on page …
/* Do exercises from pg 155 -156 of Pengaturcaraan C