CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching © Janice Regan, CMPT 128, Jan 2007 0 Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches In sequence Branching Looping © Janice Regan, CMPT 128, 2007-2013 1 Control Structures Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repetition of a particular block of statement(s) © Janice Regan, CMPT 128, 2007-2013 2 Branching One way selection Two way selection Multiple way selection © Janice Regan, CMPT 128, 2007-2013 3 Flowcharts Flowcharts use some basic symbols To start or end a function To contain calculations To make decisions To connect different parts of an algorithm © Janice Regan, CMPT 128, 2007-2013 4 One-way selection Simplest form of a branch. Evaluate a condition that can be True or False If the condition is true a series of actions are executed If the condition is false that series of actions are not executed C and C++ implementation: if statement © Janice Regan, CMPT 128, 2007-2013 5 Flowchart: one way selection Write the condition that needs to be satisfied in the decision box (diamond). Based upon the value of the condition (boolean T or F) choose what to do next C and C++ implementation if statement condition T Statement 1; ⋮ Statement n; F The sequence of statements to be executed if the condition is true is placed in the box © Janice Regan, CMPT 128, 2007-2013 6 One-way selection Example if statement in C and C++: setFlagOrderBoxes = 0; if (numberOfBoxes < minimumBoxInventory) setFlagOrderBoxes = 1; setFlagOrderBags = 2; // always executed setFlagOrderBags is always set to 2, even if the condition in the if statement is false © Janice Regan, CMPT 128, 2007-2013 7 C++ Compound/Block Statement Only one statement following the if statement is part of the if control structure What if we need more than one statement done if the condition is true? Must use a block of statements (also called a compound statement) C++ uses { }, to contain all the statements in a block of statements © Janice Regan, CMPT 128, 2007-2013 8 One way selection: sample Do a series of actions only if a given condition holds If the condition does not hold skip the actions if (myScore > yourScore) { cout << “My score was lower than yours “; difference = yourScore – myScore; cout << “I only got “ << difference << “ less points than you did” ; } cout << “Thanks for studying with me”; © Janice Regan, CMPT 128, 2007-2013 9 Two-way selection Evaluate a condition that can be True or False One “Set of things to do” if the condition is true, A different “Set of things to do” if the condition is false C and C++ implementation: if-else statement © Janice Regan, CMPT 128, 2007-2013 10 Flowchart: two way selection Based upon the value of the condition (boolean T or F) choose what to do next The sequence of statements to be executed if the condition is true is placed in the box at the right The sequence of statements to be executed if the condition is false is placed in the box below the condition © Janice Regan, CMPT 128, 2007-2013 condition F T Statement 1; Statement n; ⋮ Statement 1; Statement n; ⋮ Implemented in C and C++ as an if-else statement 11 Example of two-way selection Example if-else statement in C and C++: if (examScore > 50) myCourseGrade = “PASS”; else myCourseGrade = “FAIL”; NOTE: Only one statement follows the if, and one statement follows the else © Janice Regan, CMPT 128, 2007-2013 12 Two way selection in C++ Complete one of two possible series of actions First series of actions is complete if condition is true Second series of actions is completed if condition is false if (condition) { //Series of actions to be taken when the condition is true action 1; ⋮ action n; } else { // Series of actions to be taken when the condition is false action 1; ⋮ action n; } © Janice Regan, CMPT 128, 2007-2013 13 Two way selection in C++ CODING STANDARD: always use {} to make blocks if (X<Y && Y<Z) //Never X<Y<Z { //Series of actions to be taken when the condition is true cout < < “Y is between X and Z” ; } else { // Series of actions to be taken when the condition is false cout<< “ Y is not between X and Z”; } cout << endl << “Y is “ << Y; cout << “ X is “ << X << “ Z Is “ << Z; © Janice Regan, CMPT 128, 2007-2013 14 Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 T T F F © Janice Regan, CMPT 128, 2007-2013 T F T F EXPRESSION1 && EXPRESSION2 T F F F 15 Multiple-way selection If condition1 is true the 1st series of actions is completed If condition1 is false and condition2 is true the 2nd series of actions is completed If condition1 and condition2 are false and condition3 is true the 3rd series of actions is completed … If all conditions are false the final series of actions is completed Implemented in C++ as an if-elseif-else statement © Janice Regan, CMPT 128, 2007-2013 16 Flowchart for multiple selection Statement 1; T condition F condition2 T ⋮ Statement n; Statement 1; ⋮ Statement n; F Statement 1; ⋮ Statement n; © Janice Regan, CMPT 128, 2007-2013 17 Example of Multiple selection Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 70) myCourseGrade = “B ”; else myCourseGrade = “FAIL”; © Janice Regan, CMPT 128, 2007-2013 18 Example of Multiple selection Example: always use { } to create blocks if ( X > 80 || Y < 100) { cout << “ (X >80 , Y any value) OR (X any value, Y<100)” ; } else if (X < 80) { cout << “X < 80 and Y >= 100” ; } else { cout <“X =80 Y >= 100”; } © Janice Regan, CMPT 128, 2007-2013 19 Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 T T F F © Janice Regan, CMPT 128, 2007-2013 T F T F EXPRESSION1 || EXPRESSION2 T T T F 20 Selection: decision statements Each decision statement contains a condition The condition is an expression with a logical value (true or false) The condition is a Boolean expression A relational expression (a type of logical expression) Another type of logical expression A Boolean variable or constant © Janice Regan, CMPT 128, 2007-2013 21 Binary Relational Operators C, C++ < <= > >= less than less than or equal to greater than greater than or equal to Binary Equality Operators in C, C++ == != equal to not equal to Evaluated left to right © Janice Regan, CMPT 128, 2007-2013 22 Relational Expressions A type of logical expression Combines two numbers, strings, characters to give a value of true or false A simple relational expression is Two numerical values combined using a binary relational operator A more complex relational expression is A combination of simple relational expressions © Janice Regan, CMPT 128, 2007-2013 23 Other logical expressions Other types of logical expression include Two logical variables / constants combined with a binary logical operator One logical variable or constant One logical variable or constant operated on by a unary logical operator Two relational expressions combined with a binary logical operator … Any more complex logical expression © Janice Regan, CMPT 128, 2007-2013 24 Binary Logical Operators C, C++ && || Logical AND Logical OR Unary Logical Operators C, C++ ! Not Evaluated left to right Arguments of logical operators have values of true or false © Janice Regan, CMPT 128, 2007-2013 25