Computer Science Departme nt FTSM FTSM Control Structure: Control Structure: Selection Selection (Part 2) (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure
47
Embed
Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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
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)
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))