Control Statement Selection

Post on 06-Feb-2016

8 views 0 download

Tags:

description

control statement selection

Transcript of Control Statement Selection

Control Statement - Selection

if Statements

ifif

if - elseif - else

if – else - ifif – else - if

if Statement

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 curly Don’t forget the curly brackets, if there are more brackets, if there are more

than ONE statementsthan ONE statements

Don’t forget the Don’t forget the brackets !!brackets !!

if Statement

Pseudo Code:if <condition is true> start

step 1

step 2

step k

end_if

Pseudo Code: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

}

if 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?

if Statement

Example: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?

if Statement

Example: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?

if - else Statement

Syntax:if (expression)if (expression)

statement1;statement1;elseelse

statement2;statement2;or

if (expression) {if (expression) {statement1;statement1;statement2;statement2;

} else} elsestatement3;statement3;

Syntax:if (expression)if (expression)

statement1;statement1;elseelse

statement2;statement2;or

if (expression) {if (expression) {statement1;statement1;statement2;statement2;

} else} elsestatement3;statement3;

if - else Statement

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;

}}

if - else 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

_

if - else 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

_

if - else 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

if – else Statement

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);

}

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?

if – else Statement

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);

}

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?

if – else Statement

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);

}}

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?

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);}

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??

Syntax:if (expression)if (expression)

statement;statement;

Syntax:if (expression)if (expression)

statement;statement;

if statementif statement

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

if – else - if Statement

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

if – else - if 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;

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 !!

Let’s recap …

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

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

Let’s recap …

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

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

Let’s recap …

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

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

Let’s recap …

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

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

Multiple Selection in C

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”);}

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?

Multiple Selection in C

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”);}

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

ExerciseDevelop a program for the following problem.

Given a mark, determine its grade based on the table below:

75 < mark < 100 grade = A65 < mark < 74 grade = B55 < mark < 64 grade = C40 < mark < 54 grade = D 0 < mark < 39 grade = F others error message

Pseudo CodeStartInput student’s markIf student’s mark is greater than or equal to 75

Print “Grade = A”else If student’s mark is greater than or equal to 65 and lower than 75

Print “Grade = B”else If student’s mark is greater than or equal to 55 and lower than 65

Print “Grade = C”else If student’s mark is greater than or equal to 40 and lower than 55

Print “Grade = D”else If student’s mark is greater than or equal to 0 and lower than 40

Print “Grade = F”else

Print “Input error”End

Answer1

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”);

Answer2

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 );

Answer3

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);

Nested ifs

ifif statement that contains other ifif / if - elseif - else statements

Nested ifs

Example:if (age > 18) {

if (age > 55) price = 2.50; /* Price for senior citizens

*/else

price = 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

*/else

price = 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

Nested ifs - Problem

Example: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?

Nested ifs - ProblemTry to understand the consequences….

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;

Nested ifs - Problem

By 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;

}

switch and break

Syntax:switch (expression) {switch (expression) {

case expression1:case expression1:statement1;statement1;break;break;

case expression2:case expression2:statement2;statement2;break;break;

……default:default:

expressionX;expressionX;break;break;

}}

Syntax:switch (expression) {switch (expression) {

case expression1:case expression1:statement1;statement1;break;break;

case expression2:case expression2:statement2;statement2;break;break;

……default:default:

expressionX;expressionX;break;break;

}}

Syntax:switchswitch (expression) { (expression) {

casecase expression1: expression1:statement1;statement1;break;break;

casecase expression2: expression2:statement2;statement2;break;break;

……defaultdefault::

expressionX;expressionX;break;break;

}}

Syntax:switchswitch (expression) { (expression) {

casecase expression1: expression1:statement1;statement1;break;break;

casecase expression2: expression2: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 !!

switchswitch and and breakbreak Important Rule !!

Syntax:switchswitch (expression) { (expression) {

casecase expression1: expression1:statement1;statement1;break;break;

casecase expression2: expression2:statement2;statement2;break;break;

……defaultdefault::

expressionX;expressionX;break;break;

}}

Syntax:switchswitch (expression) { (expression) {

casecase expression1: expression1:statement1;statement1;break;break;

casecase expression2: expression2:statement2;statement2;break;break;

……defaultdefault::

expressionX;expressionX;break;break;

}}

Must be Must be INTEGER or INTEGER or

CHAR !CHAR !

switch and break

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”);

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 _

switch and break

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”);

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 …

switch and break

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”);

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 !!

switch and break

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”);

……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 …

switch and break

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”);

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 ?

switch and break

Example: char jantina;printf (“Masukkan Jantina Anda: (L/P)\n”);scanf (“%c”,&jantina);

switch (jantina) {case ‘P’:

printf(“\nJantina adalah PEREMPUAN\n”);

break;case ‘L’:

printf(“\nJantina adalah LELAKI”);break;

default:printf(“\nTIDAK DAPAT DIPASTIKAN!”);break;

}

Example: char jantina;printf (“Masukkan Jantina Anda: (L/P)\n”);scanf (“%c”,&jantina);

switch (jantina) {case ‘P’:

printf(“\nJantina adalah PEREMPUAN\n”);

break;case ‘L’:

printf(“\nJantina adalah LELAKI”);break;

default:printf(“\nTIDAK DAPAT DIPASTIKAN!”);break;

}

Masukkan Jantina Anda: (L/P)

L

Masukkan Jantina Anda: (L/P)

L

Jantina adalah LELAKI

jantina ?L

End of Lecture

Review Questions