CSE 1223 Introduction to Computer Programming in Java Chapter 3 Branching 1 Flow of Control The order in which statements in a program are executed is called the flow of control So far we have only seen sequential execution statements execute one after the other in the order in which they appear in the program 2 Flow of Control cont Consider the following tasks You want to compute the quotient of two variables but only if the divisor is not zero You input some value e g a date and if it is in the correct format mm dd yyyy you continue the computation otherwise you print an error Given a grade between 0 and 100 you want to convert the numeric value to a letter grade e g A for grade greater than 90 B for grade between 80 and 90 etc How can we check these conditions and execute the appropriate piece of code depending on the outcome of the check 3 Selection Statements Scanner in new Scanner System in System out print Enter the dividend int dividend in nextInt System out print Enter the non zero divisor int divisor in nextInt Boolean expression if divisor 0 int quotient dividend divisor System out println dividend divisor quotient else System out println Cannot divide by 0 4 The boolean Type A variable of the boolean data type stores one of two values true or false true and false are the only two boolean constants boolean values expressions are used to make decisions in programs For example if x 0 boolean expression System out println x is positive 5 Boolean Expressions There are several kinds of boolean valued expressions a boolean variable or constant e g boolean boolVar in nextBoolean if boolVar an arithmetic expression followed by a relational operator followed by an arithmetic expression e g int intVar in nextInt if intVar 0 6 Relational Operators equal not equal x y x y x y x y x y x y 7 Boolean Operators We can also build boolean expressions by combining two boolean expressions with a boolean operator and or not x 0 x 10 x 0 x 10 x 0 8 Boolean Operators cont If A and B are boolean expressions A B is true if and only if both A and B are true in other words if either A or B or both are false A B is false If A and B are boolean expressions A B is true if either A or B or both are true in other words A B is false only if both A and B are false If A is a boolean expression A is true if A is false and A is false if A is true 9 Some Boolean Expressions What s the value of each of the following expressions int x 5 y 12 boolean a true b false c true x 0 x 10 x 0 x 10 a b c a b c x 1 y 5 y 5 x y x y 10 Your Turn Given three integer variable i j and k write a boolean expression for each of the following problems i is equal to 3 or 5 i is between 1 and 7 but not 1 or 7 i is even i is odd i is the smallest of i j and k 11 If Syntax and Flow Chart boolean expression if test if block test false true if block statement sequence 12 If Else Syntax and Flow Chart boolean expression if test if block else else block true if block test false else block statement sequences 13 An Example Given two integers i and j write a piece of code that sets integer variable max to the value of the larger of the two if i j max i else i j max j 14 Tracing an if else statement To trace an if else statement trace only the portions of the statement that get executed Base this on an evaluation of the boolean expression 15 A trace with an initial state Program Line Program state i 10 j 5 max 0 if i j max i else max j 16 A trace with an initial state Program Line Program state i 10 j 5 max 0 if i j max i i 10 j 5 max 10 else max j 17 A trace with an initial state Program Line Program state i 10 j 5 max 0 if i j max i i 10 j 5 max 10 else max j i 10 j 5 max 10 18 A trace with an initial state Program Line Program state i 10 j 5 max 0 if i j max i Note that this portion gets skipped over with these inputs i 10 j 5 max 10 else max j i 10 j 5 max 10 19 A trace with another initial state Program Line Program state i 10 j 500 max 0 if i j max i else max j 20 A trace with another initial state Program Line Program state i 10 j 500 max 0 if i j max i else max j i 10 j 500 max 500 21 A trace with another initial state Program Line Program state i 10 j 500 max 0 if i j max i else max j i 10 j 500 max 500 i 10 j 500 max 500 22 A trace with another initial state Program Line And this time it s THIS portion that gets skipped with these inputs Program state i 10 j 500 max 0 if i j max i else max j i 10 j 500 max 500 i 10 j 500 max 500 23 An alternative solution Given two integers i and j write a piece of code that sets integer variable max to the value of the larger of the two max i if j max max j Trace through the code above Does it do the same thing as the previous code Sometimes a good choice of initial value can make for simpler code 24 Problem solving with if else Suppose we have an integer i and we need to know whether it is even or not Recall the operation This suggests a choice i is even if i 2 0 If i 2 0 then i is even Otherwise i is odd Problems that can be solved with if else often have this form If x is true then perform action y otherwise perform action z 25 Problem solving with if else Given an integer i write some code that will set a string to even if i is even and odd if I is odd 26 Problem solving with if else Given an integer i write some code that will set a string to even if i is even and odd if I is odd String result 27 Problem solving with if else Given an integer i write some code that will set a string to even if i is even and odd if I is odd String result if i is even set result to even else i …
View Full Document