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

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

Jan 01, 2016

Download

Documents

Louisa Bell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.

CIS 234: Control Structures - Selection

Dr. Ralph D. WestfallApril, 2010

Page 2: CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 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

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

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?

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

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

many languages use = instead of ==

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

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

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

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

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

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

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

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

NOT Operator can use NOT operator (!) to

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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)

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

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

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

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;

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

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

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

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

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

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

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

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

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?