= 18)"> = 18)">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V.

Similar presentations


Presentation on theme: "CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V."— Presentation transcript:

1 CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V

2 Selection Structures Conditions – result in true or false if if... else nested if switch conditional operator

3 Conditions something that a program checks to decide which code to run 3 parts to a "conditional expression" something being tested (operand) conditional operator (I.e. ==,!==) what it is being compared to (operand) example: (age >= 18)

4 Conditions - 2 conditional operators ==, !=,, >= Java always puts conditions in parentheses (weight > 10)

5 Combining Conditions multiple comparisons can be combined in 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

6 Combining Conditions - 2 (if color.equals("red“) && price < 10000) both need to be true (if color.equals("red“) || price < 10000) only 1 needs to be true

7 Truth Tables T && T = T T && F = F F && T = F F && F = F T || T = T T || F = T F || T = T F || F = F

8 Practice write conditions for the following (need to make up 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, some weigh less than 120 pounds

9 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 they will run if condition evaluates to true

10 Simple if code runs if true, skipped if false one-line if statement if (a==2) x = x + 1; two-line statement if (a==2) x = x + 1; // note ; end both statements

11 Simple if - 2 “block” if statement with curly braces if (a==3) { x = x + 1; // note ; y = y * 2; // note ; } //[statement(s) runs if true]

12 Practice write an if structure to print Good Customer if a person buys over $50,000 If (purchase >= 50000) displayResult(“Good Customer”);

13 if... else if (a==4) x = x * 5; else x = x * 2; // note only 1 semicolon, at // very end

14 if... else - 2 if (a==6) { x = x * 3; y = y * 3;} else { x = x + 1; y = y + 1;} // ; after each statement

15 Practice write an if structure to print Good Customer if a person buys over $50,000, and prints OK Customer for other customers whose sales are over $10,000

16 "Nested" if can put if (if... else) inside another if if (a==1) {x = 1; //1 st statement if b==2 y = 2; //2 nd statement } 3 possibilities: A is false, A true & B false, or both A & B are true

17 Limitations of if if can run 1 block of code if... else can run 2 blocks of code nested ifs can run multiple blocks of code however it gets awkward after about 3 or 4 blocks

18 Multiple Nested ifs if (a==1) x=1; else if (a==2) x = 4; else if (a==3)....... Suggest use of blocks to make it easier to read if (a==1) x=1; else if (a==2) x = 4; else if (a==3).......

19 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

20 Selection (Branching) - switch switch is better than if for multiple tests makes code easier to read makes code easier to maintain uses a test variable, which is compared to multiple values matching value triggers appropriate code default can be used when no value matches (optional)

21 switch - 2 switch (vegetableChar)//selector { case "b”: // label for broccoli caloryCount = 50; break; case "c”: // label for corn caloryCount = 111; break; default: caloryCount = 375; }// note switch selector must be of type int,boolean, or char

22 Practice write a switch structure to print Good Customer if a person buys over $50,000, and prints OK Customer for other customers whose sales are over $10,000, and Needs Follow Up for the rest of the customers

23 Conditional Operator shortcut for if... else bigger = (a>b) ? a : b; if condition true, variable on left gets 1 st value after ? mark if condition false, variable on left gets 2 nd value after ? mark Dr V does not like shortcuts!!

24 Conditional Operator - 2 if statement if (age<18) school = "high"; else school ="college"; using conditional statement instead school = (age<18)?"high":"college";


Download ppt "CIS 234: Control Structures: Selection Original by Dr. Ralph D. Westfall Modified by Dr V."

Similar presentations


Ads by Google