= 18 //operands, operator?"> = 18 //operands, operator?">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010."— Presentation transcript:

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

2 Selection Control Structures all selection control structures use conditions (tests) types of selection control structures if if... else nested if switch conditional (ternary) operator

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

4 Condition - 2 relational (conditional) operators ==, !=,, >= many languages use = instead of == in Java, must always put the whole conditional expression in parentheses (weight > 10)

5 boolean Expression Results boolean (conditional) expressions must always evaluate to either true or false int 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

6 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

7 Combining Conditions - 2 if (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

8 NOT Operator can use NOT operator (!) to reverse a boolean value boolean ok = true; boolean noGood = ! ok;

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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 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

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

20 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

21 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

22 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

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

24 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

25 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

26 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) "12 Days of Christmas" videokaraoke could also put if code inside a switch see days in month example (SwitchDemo2 halfway down page)example

27 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

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

29 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

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

31 Review - 3 What is a truth table? What are the boolean value of the following conditions? T || F T && F ! T

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


Download ppt "CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010."

Similar presentations


Ads by Google