of 18
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
1/18
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
ENGR 1200U Introduction to Programming
Lecture 9
Control Structures: Selection (Chapter 3)
Dr. Eyhab Al-Masri
ENGR 1200U
Winter 2013 - UOIT
An algorithm is a sequence of steps for solving aproblem.
Engineering problem solutions to real worldproblems require complex algorithms.
Development of a good algorithm increases the
quality and maintainability of a solution, andreduces the overall time required to implementa correct solution.
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
2/18
ENGR 1200U
Winter 2013 - UOIT
Top-down design begins with a "big picture"description of a problem solution in sequentialsteps.
The sequential steps are refined until the stepsare detailed enough to translate to languagestatements.
The refined steps, or algorithm, can be describedusing pseudo code or flowcharts.
ENGR 1200U
Winter 2013 - UOIT
Copyright 2012 Pearson Education,
Inc.
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
3/18
ENGR 1200U
Winter 2013 - UOIT
A structured program is written using simplecontrol structures, including:
Sequence steps are performed one after another. Selection one set of statements is executed if a given
condition is true, a different set of statements, or nostatements at all, is executed if the condition is false.
Repetition A set of statements is executed repeatedlyas long as a given condition is true.
ENGR 1200U
Winter 2013 - UOIT
Sequence
Selection
Repetition
?truefalse
?true
false
? => conditional
expression
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
4/18
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
5/18
ENGR 1200U
Winter 2013 - UOIT
Assignment versus Equality In C++, = is an assignment operator
Example:
int c
c = 5; //assign value of 5 to c
The == operator denotes equality testing
Example:
int c
c = 5; //assign value of 5 to c
if ( c == 5) // test whether c equals 5
If (name == Harry) //compare strings
Use == inside testsUse = outside test
ENGR 1200U
Winter 2013 - UOITSyntax 3.2: C++ for Everyone by Cay Horstman
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
6/18
ENGR 1200U
Winter 2013 - UOITTable3..2:C++ forEveryone byCay Horstman
ENGR 1200U
Winter 2013 - UOIT
Value of x Value of y Expression Result
12 5 x + 3 >= y * 3 true
10 1 x + 3 y * 10 true
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
7/18
ENGR 1200U
Winter 2013 - UOIT
Simplified Syntaxif (boolean_expression)
statement;
if (boolean_expression){
statement1;
statement_n;}
Examples//statement executed if x>0if (x>0)
++k;
//statement block executed if x>0if (x>0)
{ x = sqrt(x);++k;}
ENGR 1200U
Winter 2013 - UOIT
If we wish to execute several statements (or asequence structure) when the condition is true,we use a statement block
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
8/18
ENGR 1200U
Winter 2013 - UOIT
ENGR 1200U
Winter 2013 - UOIT
Syntaxif (boolean_expression)
statement;[else
statement;]
if (boolean_expression){
}[else{
} ]
Example
if (x>0) { //statement block executed if x>0
} else { //statement block executed if x
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
9/18
ENGR 1200U
Winter 2013 - UOIT
ENGR 1200U
Winter 2013 - UOIT
C++ allows a conditional operator to be used inplace of a simple if/else statement
Syntax: condition ? value1 : value2
if (age>65){
retired=true;}
else
{retired=false;
}
retired=age>65?true :false;
cout
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
10/181
ENGR 1200U
Winter 2013 - UOIT
It is a common error to use the assignmentoperator (=) instead of the relational operator(==)
Consider the following program
int x(4),y(5);if (x=y){
cout
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
11/181
ENGR 1200U
Winter 2013 - UOIT
We will model a person entering agrade by getting input from the user:
intgrade;coutgrade;
ENGR 1200U
Winter 2013 - UOIT
If the user inputs any grade valuegreater than or equal to 50 The program must set the class status to 1
Otherwise We simply set the class status to 0
int class_status;
if (grade
>
49){class_status=1;}else
{class_status=0;}
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
12/181
ENGR 1200U
Winter 2013 - UOIT
if (grade>49){
class_status=1;}
else
{class_status=0;
}
A condition that is true or false.Often uses relational operators
== != < >=Braces are not required if
the branch contains a
single statement, but it is
a good practice to include
them.
If the condition is true,
the statement(s) in this
branch are executed in
sequence; if the condition
is false, they are skipped.
If the condition is false,
the statement(s) in this
branch are executed in
sequence; if the condition
is true, they are skipped.
Omit the else
branch if there isnothing to do
Lining up braces isa good practice
Dont put a semicolon here
ENGR 1200U
Winter 2013 - UOIT
grade > 49?
class_status = 1 class_status = 0
Condition
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
13/181
ENGR 1200U
Winter 2013 - UOIT
#include
using namespace std;
int main()
{
int grade;
coutgrade;int class_status;
if (grade>49){
class_status=1;cout
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
14/181
ENGR 1200U
Winter 2013 - UOIT
To avoid confusion and possible errors whenusing if/else statements, you should use {} toclearly define statement blocks.
Do not use == with real values Instead of x==3.14, use
fabs(x-3.14)str2)
cout
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
15/181
ENGR 1200U
Winter 2013 - UOIT
String ordering rules: All uppercase letters come before the lowercase letters
Example: Z comes before a
Space character comes before all printable characters
Numbers come before letters
Punctuation marks are ordered
ENGR 1200U
Winter 2013 - UOIT
Expression Value Result
word1 == word2 falseBoth words are not equal in thefirst character
word1 > word2 trueT comes after S inlexicographical ordering
word1 < Tremble FalseFifth characters do not match, andb comes before e
Word2 == Small True They are equal
stringword1,word2;word1="Tremendous";word2="Small";
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
16/181
ENGR 1200U
Winter 2013 - UOIT
bool Data Type A built-in data type consisting of jus two values: true
or false
bool (a reserved C++ keyword) is short for Boolean Boolean data is used for testing conditions in a
program
Each variable of type bool can contain one of twovalues: true or false
ENGR 1200U
Winter 2013 - UOIT
In C++, assertions take the form of logicalexpressions (also called Boolean expressions)Just as an arithmetic expression consists of
numeric values and operations, a logicalexpression is made up of logical values andoperations
Every logical expression has one of two values: true orfalse
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
17/181
ENGR 1200U
Winter 2013 - UOIT
By combining logical operators with relationaloperators, we can make more complexassertions
Example:
grade >= 50.0 && grade
8/14/2019 Lecture 9 - Control Structures pt. 1.pdf
18/18
ENGR 1200U
Winter 2013 - UOIT
Consider the following logical expression x == 1 && y > 2
Some programming languages use full evaluationof logical expressions Computer first evaluates both subexpressions (both x
== 1 and y > 2) before applying && operator toproduce the final result
In contrast, C++ uses short-circuit (orconditional evaluation
ENGR 1200U
Winter 2013 - UOIT
Evaluation proceeds from left to right, and thecomputer stops evaluating subexpressions assoon as possible (i.e. as it knows the Booleanvalue of the entire expression)
Examples E.g. if A is false, A && B is always false, regardless of the
value of B. E.g. if A is true, A || B is always true, regardless of the
value of B.