introduction to computing lecture 05: selection (continued) introduction to computing lecture 05:...

29
Introduction to Computing Introduction to Computing Lecture 0 Lecture 0 5 5 : : Selection (continued) Selection (continued) Assist.Prof. Assist.Prof. Dr. Dr. Nükhet Nükhet ÖZBEK ÖZBEK Ege Ege University University Department of Department of Electrical & Electrical & Electronics Electronics Engineering Engineering nukhet nukhet . . ozbek ozbek @ @ ege ege .edu.tr .edu.tr

Upload: aleesha-morgan

Post on 13-Jan-2016

226 views

Category:

Documents


3 download

TRANSCRIPT

Introduction to Computing Introduction to Computing Lecture 0Lecture 055::

Selection (continued)Selection (continued)

Assist.Prof.Assist.Prof.Dr. Dr. NükhetNükhet ÖZBEKÖZBEKEgeEge University University

Department of Department of Electrical & ElectronicsElectrical & Electronics Engineering Engineeringnukhetnukhet..ozbekozbek@@egeege.edu.tr.edu.tr

TopicsTopics

•Type int as BooleanType int as Boolean

•Nested if statementsNested if statements

•switch statementswitch statement

Recall : Recall : Syntax for if Syntax for if statementstatementif (condition)if (condition)

statement;statement;OROR

ifif (condition)(condition){{

statementstatement11;;……

statementstatementnn;;}}

Recall : Recall : Syntax for if Syntax for if & else & else statementstatementif (condition)if (condition)

statementstatementTT;;elseelse

statementstatementFF;;ORORifif (condition)(condition){{

statementstatementT1T1;;……statementstatementTnTn;;

}}elseelse{{

statementstatementF1F1;;……statementstatementFnFn;;

}}

Recall : Recall : Syntax for cascaded if Syntax for cascaded if statementsstatementsif (conditionif (condition11))

statementstatement11;;else if (conditionelse if (condition22))

statementstatement22;;......

else if (conditionelse if (conditionnn))statementstatementnn;;

else else statementstatementee;;

Type Type intint as Boolean as Boolean

• In C, integers can be used as In C, integers can be used as BooleansBooleans

• Integer value Integer value 00 is is falsefalse

• Any Any non-zeronon-zero integer value is integer value is truetrue

#include <stdio.h>#include <stdio.h>

/* Test some Booleans. *//* Test some Booleans. */

int main()int main(){{ int whatever = 0;int whatever = 0;

if (whatever)if (whatever) {{ printf(“Is that true, Homer?\n”);printf(“Is that true, Homer?\n”); }} elseelse {{ printf(“No, Marge.\n”);printf(“No, Marge.\n”); }} return 0;return 0;}}

Example: What is the output?

#include <stdio.h>#include <stdio.h>

/* Test some Booleans. *//* Test some Booleans. */

int main()int main(){{ int whatever = 1;int whatever = 1;

if (whatever)if (whatever) {{ printf(“Is that true, Homer?\n”);printf(“Is that true, Homer?\n”); }} elseelse {{ printf(“No, Marge.\n”);printf(“No, Marge.\n”); }} return 0;return 0;}}

Example: What is the output?

#include <stdio.h>#include <stdio.h>

/* Test some Booleans. *//* Test some Booleans. */

int main()int main(){{ int whatever = -100;int whatever = -100;

if (whatever)if (whatever) {{ printf(“Is that true, Homer?\n”);printf(“Is that true, Homer?\n”); }} elseelse {{ printf(“No, Marge.\n”);printf(“No, Marge.\n”); }} return 0;return 0;}}

Example: What is the output?

#include <stdio.h>#include <stdio.h>

/* Test some Booleans. *//* Test some Booleans. */

int main()int main(){{ int whatever = 0.003;int whatever = 0.003;

if (whatever)if (whatever) {{ printf(“Is that true, Homer?\n”);printf(“Is that true, Homer?\n”); }} elseelse {{ printf(“No, Marge.\n”);printf(“No, Marge.\n”); }} return 0;return 0;}}

Example: What is the output?

Example: What is the output?

#include <stdio.h>#include <stdio.h>

/* Test some Booleans. *//* Test some Booleans. */

int main()int main(){{ float whatever = 0.003;float whatever = 0.003;

if (whatever)if (whatever) {{ printf(“Is that true, Homer?\n”);printf(“Is that true, Homer?\n”); }} elseelse {{ printf(“No, Marge.\n”);printf(“No, Marge.\n”); }} return 0;return 0;}}

Behavior is “undefined”

Nested if statementsNested if statements

• We can nest if statements (one if We can nest if statements (one if statement inside another) to code statement inside another) to code decisions with multiple alternativesdecisions with multiple alternatives

Example: Example: Nested Nested ifif

if (num > 0)if (num > 0)

printf(“positive”);printf(“positive”);

elseelse

if (num < 0)if (num < 0)

printf(“negative”);printf(“negative”);

else /* num is zero */else /* num is zero */

printf(“zero”);printf(“zero”);

Example: Example: Nested Nested ifif

if(age<2)if(age<2)

printf(“printf(“Infant”Infant”));;

elseelse

if(age<18)if(age<18)

printf(“printf(“Child”Child”));;

else /else /* * age >= 18age >= 18 */ */

printf(printf(“Adult”“Adult”));;

/* Print a message if all criteria are met *//* Print a message if all criteria are met */

if (marital_status == ‘S’)if (marital_status == ‘S’)if (gender == ‘M’)if (gender == ‘M’)

if (age >= 18 && age <= 26)if (age >= 18 && age <= 26)printf(“All criteria are met.”);printf(“All criteria are met.”);

IS EQUIVALENT TOIS EQUIVALENT TO

if (marital_status == ‘S’ && gender == ‘M’&& if (marital_status == ‘S’ && gender == ‘M’&& age >= 18 && age <= 26)age >= 18 && age <= 26)

printf(“All criteria are met.”);printf(“All criteria are met.”);

Example: Example: Nested Nested ifif

Notes on Cascaded Notes on Cascaded ifif

if (number >= if (number >= 55)){{ if(number >= 10)if(number >= 10)

{{if (number < 20)if (number < 20)

printf(“S1”);printf(“S1”);elseelse

printf(“S2”);printf(“S2”);}}elseelse

printf(“S3);printf(“S3);printf(“S4”);printf(“S4”);

}}elseelse

printf(“S5”);printf(“S5”);

What is the output if:

• number is equal to 3

• number is equal to 5

• number is equal to 10

•number is equal to 35

Q:

Common MistakeCommon Mistakeif(age>2) /* if greater than 2 */

if(age<18) /* and less than 18 */printf (“Child”); /* it's a child */

elseprintf( “Infant”); /* ERROR: inappropriate response */

if(age>2) /* if greater than 2 */{

if(age<18) /* and less than 18 */printf (“Child”); /* it's a child */

}else

printf( “Infant”);

CONDITIONAL OPERATORCONDITIONAL OPERATOR

• Conditional Operator (?:) is ternary operator Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain (demands 3 operands), and is used in certain situations, replacing if-else condition phrases. situations, replacing if-else condition phrases.

• Conditional operator’s shape is:Conditional operator’s shape is:– Condition_phrase ? phrase1 : phrase2;Condition_phrase ? phrase1 : phrase2;

• If conditional_phrase is true, phrase1 is executed If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned phrase2 is executed, and whole phrase is assigned with phrase2 value.with phrase2 value.

Example - Example - CONDITIONAL CONDITIONAL OPERATOROPERATOR

int a, b, c;int a, b, c;......c = a > b ? a : b;c = a > b ? a : b;

IS THE SAME ASIS THE SAME AS

if(a > b)if(a > b)c = a;c = a;

elseelsec = b;c = b;

Multiple Choice: switch and Multiple Choice: switch and breakbreak

• If choosing more than 2 alternatives If choosing more than 2 alternatives you have use ifyou have use if……else if…elseelse if…else

• Another alternative is the switch Another alternative is the switch statementstatement

switch exampleswitch example

switch(day)switch(day){{

case 1: printf(“Monday”); break;case 1: printf(“Monday”); break;case 2: printf(“Tuesday”); break;case 2: printf(“Tuesday”); break;case 3: printf(“Wednesday”); break;case 3: printf(“Wednesday”); break;case 4: printf(“Thursday”); break;case 4: printf(“Thursday”); break;case 5: printf(“Friday”); break;case 5: printf(“Friday”); break;case 6: printf(“Saturday”); break;case 6: printf(“Saturday”); break;case 7: printf(“Sunday”); break;case 7: printf(“Sunday”); break;case default: printf(“Day number should be between 1 and case default: printf(“Day number should be between 1 and 7”);7”);

}}

Syntax for switch statementSyntax for switch statement

switch(controlling expression) {switch(controlling expression) {label setlabel set11::

statementsstatements11;;break;break;

label setlabel set22::statementsstatements22;;break;break;

..

..

..label setlabel setnn::

statementsstatementsnn;;break;break;

default:default:statementsstatementsdd;;

}}

Program flow in switch Program flow in switch statementsstatements

switch exampleswitch exampleswitch(class)switch(class){{

case ‘B’: case ‘B’: case ‘b’:case ‘b’:

printf(“Battleship”); break;printf(“Battleship”); break;case ‘C’: case ‘C’: case ‘c’:case ‘c’:

printf(“Cruiser”); break;printf(“Cruiser”); break;case ‘D’: case ‘D’: case ‘d’:case ‘d’:

printf(“Destroyer”); break;printf(“Destroyer”); break;case ‘F’: case ‘F’: case ‘f’:case ‘f’:

printf(“Frigate”); break;printf(“Frigate”); break;default:default:

printf(“unknown ship class %c”,class);printf(“unknown ship class %c”,class);}}

Notes on switch statementNotes on switch statement

• Controlling expression may be of Controlling expression may be of type int or char, type int or char, but not float or but not float or doubledouble

• An equivalent statement using if…An equivalent statement using if…else if statements can be written for else if statements can be written for each switch statement, each switch statement, but not vice but not vice versaversa

switch exampleswitch example

switch(month)switch(month){{

case 3: case 3: case 4:case 4:case 5: printf(“Spring”); break;case 5: printf(“Spring”); break;case 6: case 6: case 7:case 7:case 8: printf(“Summer”); break;case 8: printf(“Summer”); break;case 9: case 9: case 10:case 10:case 11: printf(“Autumn”); break;case 11: printf(“Autumn”); break;case 12: case 12: case 1:case 1:case 2: printf(“Winter”); break;case 2: printf(“Winter”); break;default: printf(“month must be between 1 and 12”);default: printf(“month must be between 1 and 12”);

}}

Same example using if…else if Same example using if…else if statementsstatements

if (month==3 || month==4 || month==5)if (month==3 || month==4 || month==5)printf(“Spring”);printf(“Spring”);

else if (month==6 || month==7 || month==8)else if (month==6 || month==7 || month==8)printf(“Summer”);printf(“Summer”);

else if (month==9 || month==10 || month==11)else if (month==9 || month==10 || month==11)printf(“Autumn”);printf(“Autumn”);

else if (month==12 || month==1 || month==2)else if (month==12 || month==1 || month==2)printf(“Winter”);printf(“Winter”);

elseelseprintf(“month must be between 1 and 12”);printf(“month must be between 1 and 12”);

}}

switch and if elseswitch and if else

• When should you use a switch and When should you use a switch and when should you use the if else when should you use the if else construction? construction? – Often you don't have a choice. Often you don't have a choice.

•You can't use a switch if your choice is You can't use a switch if your choice is based on evaluating a float or a double based on evaluating a float or a double variable or expressionvariable or expression

• Nor can you conveniently use a switch if a Nor can you conveniently use a switch if a variable must fall into a certain range. It is variable must fall into a certain range. It is simple to write the following:simple to write the following:

– if (integer < 1000 && integer > 2) if (integer < 1000 && integer > 2)

SummarySummary

• Type Type int int as Booleanas Boolean

• Nested if statementsNested if statements

• switch statementswitch statement