cis 234: control structures: selection original by dr. ralph d. westfall modified by dr v

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

Post on 20-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

CIS 234: Control Structures: Selection

Original by Dr. Ralph D. WestfallModified by Dr V

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

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)

Conditions - 2 conditional operators==, !=, <, <=, >, >= Java always puts conditions in

parentheses(weight > 10)

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

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

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

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

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

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

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]

Practice write an if structure to print Good

Customer if a person buys over $50,000

If (purchase >= 50000)displayResult(“Good Customer”);

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

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

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

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

3 possibilities: A is false, A true & B false, or both A & B are true

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

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

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

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

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

Conditional Operator shortcut for if ... elsebigger = (a>b) ? a : b; if condition true, variable on left

gets 1st value after ? mark if condition false, variable on left

gets 2nd value after ? markDr V does not like shortcuts!!

Conditional Operator - 2 if statementif (age<18)

school = "high";

else

school ="college"; using conditional statement

insteadschool = (age<18)?"high":"college";