Download presentation
Presentation is loading. Please wait.
Published byEthel Owen Modified over 9 years ago
1
© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching
2
© Janice Regan, CMPT 128, 2007-2013 1 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
3
© Janice Regan, CMPT 128, 2007-2013 2 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)
4
Branching One way selection Two way selection Multiple way selection © Janice Regan, CMPT 128, 2007-2013 3
5
4 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
6
© Janice Regan, CMPT 128, 2007-2013 5 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
7
© Janice Regan, CMPT 128, 2007-2013 6 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 The sequence of statements to be executed if the condition is true is placed in the box condition Statement 1; Statement n; T F ⋮ C and C++ implementation if statement
8
© Janice Regan, CMPT 128, 2007-2013 7 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
9
© Janice Regan, CMPT 128, 2007-2013 8 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
10
© Janice Regan, CMPT 128, 2007-2013 9 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”;
11
© Janice Regan, CMPT 128, 2007-2013 10 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
12
© Janice Regan, CMPT 128, 2007-2013 11 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 condition Statement 1; Statement n; T F Statement 1; Statement n; ⋮ ⋮ Implemented in C and C++ as an if-else statement
13
© Janice Regan, CMPT 128, 2007-2013 12 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
14
© Janice Regan, CMPT 128, 2007-2013 13 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; }
15
© Janice Regan, CMPT 128, 2007-2013 14 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;
16
© Janice Regan, CMPT 128, 2007-2013 15 Truth Table && The && (And) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 && EXPRESSION2 T T T T F F F T F F F F
17
© Janice Regan, CMPT 128, 2007-2013 16 Multiple-way selection If condition1 is true the 1 st series of actions is completed If condition1 is false and condition2 is true the 2 nd series of actions is completed If condition1 and condition2 are false and condition3 is true the 3 rd 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
18
© Janice Regan, CMPT 128, 2007-2013 17 Flowchart for multiple selection condition2 Statement 1; Statement n; T F Statement 1; Statement n; condition F Statement 1; Statement n; T ⋮ ⋮ ⋮
19
© Janice Regan, CMPT 128, 2007-2013 18 Example of Multiple selection Example: if (examScore > 80) myCourseGrade = “A”; else if (examScore > 70) myCourseGrade = “B ”; else myCourseGrade = “FAIL”;
20
© Janice Regan, CMPT 128, 2007-2013 19 Example of Multiple selection Example: always use { } to create blocks if ( X > 80 || Y < 100) { cout 80, Y any value) OR (X any value, Y<100)” ; } else if (X < 80) { cout = 100” ; } else { cout = 100”; }
21
© Janice Regan, CMPT 128, 2007-2013 20 Truth Tables || The || (Inclusive Or) operator EXPRESSION1 EXPRESSION2 EXPRESSION1 || EXPRESSION2 T TT T FT F TT F F F
22
© Janice Regan, CMPT 128, 2007-2013 21 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
23
© Janice Regan, CMPT 128, 2007-2013 22 Binary Relational Operators C, C++ <less than <=less than or equal to > greater than >= greater than or equal to == equal to !=not equal to Evaluated left to right Binary Equality Operators in C, C++
24
© Janice Regan, CMPT 128, 2007-2013 23 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
25
© Janice Regan, CMPT 128, 2007-2013 24 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
26
© Janice Regan, CMPT 128, 2007-2013 25 Binary Logical Operators C, C++ &&Logical AND ||Logical OR !Not Evaluated left to right Arguments of logical operators have values of true or false Unary Logical Operators C, C++
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.