cis 234: control structures - selection dr. ralph d. westfall april, 2010

32
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010

Upload: louisa-bell

Post on 01-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

CIS 234: Control Structures - Selection

Dr. Ralph D. WestfallApril, 2010

Selection Control Structures all selection control structures use

conditions (tests) types of selection control structures

if if ... else nested if switch conditional (ternary) operator

Condition something that a program checks or

tests to decide which code to run 3 parts to a conditional or "boolean"

expression something being tested (operand) relational (conditional) operator what it is being compared to (operand)age >= 18 //operands, operator?

Condition - 2 relational (conditional) operators==, !=, <, <=, >, >=

many languages use = instead of ==

in Java, must always put the whole conditional expression in parentheses(weight > 10)

boolean Expression Results boolean (conditional) expressions

must always evaluate to either true or falseint a, b;boolean r;a = 4; b = 3;r = (a==b); // value of result (r)? // = ?r = (a>b); // value of result?r = a / b; // is this syntax legal? template code for testing examples

Combining Conditions boolean operators can combine

multiple comparisons into one conditional expression

AND – requires both comparisons to be true && is used for AND in Java

OR – only 1 comparison has to be true || is used for OR in Java

Combining Conditions - 2if (weight > 50 && height > 48) //

inches getsOnTheRide = "yes"; both need to be true to get on the ride at

Magic Mountain

if (GPA>=3.0 || activeClubMember=true) numberOfInterviews = 3; people who either have higher GPAs or

are active club members get more job interviews

NOT Operator can use NOT operator (!) to

reverse a boolean valueboolean ok = true;boolean noGood = ! ok;

Practice

if (GPA>=3.0 || activeClubMember=true) numberOfInterviews = 3;

change the above code so it tests to see if both are true estimate how many interviews

change code again to apply to students for whom neither condition is true # of interviews?

Truth Tables

T && T == T

F && F == F

T && F == F

F && T == F

only T && T is

true

T || T == T

F || F == F

T || F == T

F || T == T

only F || F is

false

Practice write conditions to test for the

following (need to name variables for values) people 33 years old people over 40 years old with 2 children pets that are brown dogs paper that is any size except 8.5" x 11" some members of a team are over 5 foot

tall, but they weigh less than 120 pounds

Selection (Branching) - if always based on a condition (test)

condition must evaluate to true or false

(a == b) (a > b && c < d) 1 or more statements immediately

follow the condition need curly braces if there is more

than 1 statement statement(s) will run only if condition

evaluates to true

Simple if code runs when true, otherwise skipped one-line if structure (two statements) if (a == 2) x = x + 1; two lines, two statements if (a == 2) x++; //same as x = x + 1//note that ; ends statements, not lines

Simple if - 2 “block” if statement with curly braces if (a==3) { x = x + 1; // note semicolon y = y * 2; // note semicolon }// both statements run if a equals 3

Practice write an if control structure to print

"Good Customer" for persons who buy over $50,000

write an if control structure to print "Kid discount" and also add one to the count of these admissions for children who are under 6 years old

if ... else if (a == 4) // word if used once by itself System.out.println("x is 4"); else if (b==2) // 0 to many else ifs

System.out.println("b is 2, x isnt 4"); else // 0 or 1 else without if System.out.println("b isnt 2, x isnt 4"); // note semicolons after each print statement

if ... else With Block(s) if (a == 6) { x = x * 3; //curly braces for block y = y * 3;} //end of 1st block else { x = x + 1; //start of 2nd block y = y + 1;} //end of 2nd block // semicolons after each statement

Practice write an if structure to print "Good

Customer" for a person who buys over $50,000, and prints "OK Customer" for other customers whose sales are over $10,000

"Nested" if can put if (if ... else) inside another if if (a == 1) // 1st if statement {x = 1; if b == 2 // 2nd if statement y = 2; // end of 2nd if } // end of 1st if a=b=1;a=b=2;a=1,b=2? x,y for

each?

Limitations of if structures if can run 1 block of code if ... else (or if … else if) can run

2 blocks of code can run multiple blocks of code

with multiple else ifs and/or nested ifs however it gets awkward after about

3 or 4 additional blocks

Layouts: Multiple if Structures if (a==1) x=1; else if (a==2) x = 4; else if (a==3) x = 9; else // default x = 16;

if (a==1) x=1; else if (a==2) x = 4; else if (a==3) x = 9; else // default x = 16; // same, in less

lines

Practice write an if structure to print "Good

Customer" if a person buys over $50,000

prints "OK Customer" for other customers whose sales are over $10,000

prints "Needs Follow Up" for the rest of the customers

Selection (Branching) - switch switch better than if for multiple

tests makes code easier to read (and write) makes code easier to maintain

compares integer test value (number, variable, or expression) to multiple values matching value triggers appropriate code

default can be used when no value matches (this is optional)

switch Example switch (number) { case 0: comment = "You are nada … … zip!"; break; case 1: comment = "You are the only one!"; break; default:

comment = "You are something else!"; break;} // break optional here

break in a switch Statement break statement stops execution of

switch after a true condition is found case 0: comment = "You are nada,

zip!"; break;

put most likely conditions at top to reduce number of tests (optimize code)

break after default could make code safer to maintain e.g., adding more conditions

switch … break - 2 can omit some break statements to

handle multiple conditions code "falls through": runs all statements

after true condition before the break "12 Days of Christmas" (video) (karaoke)

could also put if code inside a switch see days in month example (SwitchDemo2

halfway down page)

Practice write a switch structure to print

"Outstanding Customer" if a person buys over $100,000, "Good Customer" if a person buys over $50,000, "OK Customer" for other customers whose sales are over $10,000, and "Needs Follow Up" for the rest of the customers

Conditional Operator question mark (?) and colon (:) used

in a one-line if … else statement that assigns a value also known as the ternary operator

result = condition ? value1 : value2; boolean x = 3>2;int larger = x ? 3 : 2;

Review Name 3 different types of selection

structures What is a condition?

Give an example of a condition What are relational operators?

Give examples of relational operators

Review - 2 What 2 values can boolean

variables hold? What are the boolean operators for

combining conditions? How many conditions can be

combined by boolean operators? (trick question?)

Review - 3 What is a truth table? What are the boolean value of the

following conditions? T || F T && F ! T

Review - 4 Provide sample code for:

Simple if if … else if … else if … else

How many else ifs can be in a control structure?

How many elses can be in a control structure?