Ahmad Idrees
§ Control Structures
§ Relational Operators
§ Logical (Boolean) Operators
§ Logical Expressions
§ Selection and
Structures
§ The Function
3
§ Statements can be
executed in sequence
§ One right after the other
§ No deviation from the
specified sequence
4
§ A selection
structure can be
used
§ Which statement
is executed is
selected by
whether the
expression is true
or false
5
§ Statements can be
repeated
§ The number of
repetitions depends
on when the
expression turns false
6
§ The expressions which determine
• Selection and
• Repetition are usually comparisons
§ Comparisons are done with relational
operators
Beware of
mistaking the
assignment = for
the equality ==
7
Examples:
Expression Meaning Value
8 < 15 8 is less than 15 true
6 != 6 6 is not equal to 6 false
2.5 > 5.8 2.5 is greater than 5.8 false
5.9 <= 7.5 5.9 is less than or
equal to 7.5 true
8
Given
string str1 = "Hello"; string str2 = "Hi";
string str3 = "Air"; string str4 = "Bill";
string str5 = "Big";
Determine the values of
these comparisons using
variables
9
§ Logical or Boolean operators enable you to
combine logical expressions
A unary operator
Binary operators
§ Operands must be logical values
§ The results are logical values (true or false)
10
§ The operator (logical and)
• If both operands are true, the result is true
• If either or both operands is false, the comparison
is false
§ The operator (logical or)
• If either or both of the operands are true, the
comparison is true
• The comparison is false only if both operands are
false
§ The operator (logical not)
• The not operator reverses the logical value of the
one operand
11
§ We must know the order in which to
apply the operators
Highest
View
Sample
Program
Lowest
12
§ Consider
§ If the first condition is false, the program
could crash when it tried to divide by zero
• but if the first condition is false, the whole
expression is false
• no need to go on
§ When C++ evaluates an expression, realizes
that fact and does not even make the second
comparison
§ Called "short circuit" evaluation
13
§ C++ has two versions of if statements
§ In this version, the condition is checked
• If the expression
is true, the
statement is
executed
• If it is false,
nothing happens
14
§ Syntax
Note parentheses
§ Example around the condition
Note there is no "then"
as part of the syntax
15
§ Also possible to make two way selection
§ If the expression is
true, statement1 is
executed
§ Otherwise statement2
is executed
16
§ Syntax
View sample
program
§ Example
17
§ Consider the need for multiple
statements to be controlled by the
§ This is called
a compound
statement Statement1;
Statement2;
§ Group the Statement3;
statements in
curly brackets
18
§ Example
The compound
statement
§ Note the use of indenting and white
space in the source code for readability.
19
IF
20
§ Syntax calls for a “statement” after the
if ( … )
§ That statement can be any kind of
statement
cout
• (List statements we know about) cin
§ It can be an if statement assignment
if
if (x < 7)
if (y > 5)
cout << “hi mom”;
21
§ How to determine which the
goes with
§ Example:
if (abs (x - 7))
if (x < 7) cout << “x approaches 7 from left”;
? else
cout << “x approaches 7 from the right”;
? else
cout << “x not close to 7”;
Rule : An else goes with the closest unmatched if
22
§ Rule : an goes with the closest
unmatched
if (x < y)
if (y > 3) cout << “message about y > 3”;
else cout << “message about x and y”;
§ Consider … how do you force an to go
with a previous ?Use { curly brackets } to
nest the statements
if (x < y)
{ if (y > 3) cout << “message about y > 3”; }
else cout << “message about x and y”;
23
§ Consider determining a letter grade
based on a score
• Cut off points for A, B, C, and D are 90, 80,
70, and 60 respectively
§ We check the score against each of
these values
§ See source code
24
§ Contrast
• A sequence of
statements
• A sequence of separate statements
§ What happens in each case when it is
the first if condition that is true?
sequence will jump out of
the structure whenever match is found
• sequence of separate 's – each is
checked, no mater where the match is
25
§ Recall the current branching capability
provided by the
if ( … ) statement
§ Only branches two
ways
§ We desire a more
eloquent way to do
multiway branching
26
§ C++ provides the switch statement
switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }
27
§ Value of the switch expression matched
with one of the labels attached to a
branch switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }
§ The statement(s) with the match get
executed
28
§ Switch expression => the expression in
parentheses whose value determines which
switch label is selected
• cannot be floating point
• usually is int or char
§ Identifiers
following switch
case 1
(choice) {
: do_option_one(); break;
must be constants case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }
29
§ The causes
control to be shifted
switch (choice) {
case 1 : do_option_one(); to first statement
break; after the switch
case 2 :
case 3 : do_2_3_a ();
statement
do_2_3_b (); § the
break;
default : do_something_else (); statement is
} executed if the value
// next statement of the switch
expression is NOT
found among switch
labels
30
§ The name of the input stream (used by
itself) returns a value
• returns a 0 if it is NOT successful
• it returns a NON zero value if it IS
successful
31
§ When reading a file (a named input
stream) we wish to know when it
reaches the end
§ Since the name returns a 0 or non-0, this
can be used as a Boolean value
§ Used to control program sequencing,
control a file reading loop
32
§ Some statements will compile and run
fine in normal situations
§ Certain values may cause a statement to
crash the program
• What might happen in this statement?
The program will crash if it tries to take the
square root of a negative number
33
§ C++ provides a function which can check
specified conditions
• If the condition is true the program continues
• If the condition is false, it will cleanly terminate the
program
• It displays a message as to what condition caused
the termination
§ Syntax
§ Note, you must
34
§ Example:
§ At run time the assertion condition is checked
• If true program continues
• If false, the halts the program
§ Good for debugging stage of your program
• Shows you places where you have not written the
code to keep things from happening
• Once fully tested, s might be commented
out