W2 3 Flow of Control
W2 3 Flow of Control
Chapter 2
C++ Basics
◼ Flow of control
◼ The order in which statements are executed
◼ Branching
◼ Lets program choose between two alternatives
◼ if (boolean expression)
true statement
else
false statement
◼ When the boolean expression is true
◼ Only the true statement is executed
◼ Example: if ( ( x = = 1) | | ( x = = y) )
◼ True if x contains 1
◼ !(x = = y)
◼ True if x is NOT equal to y
NOT
if ( x < y < z )
◼ Example: x = 3;
◼ ’== ' is the equality operator
◼ Used to compare values
◼ Example: if ( x == 3)
◼ The compiler will accept this error:
if (x = 3)
but stores 3 in x instead of comparing x and 3
◼ Since the result is 3 (non-zero), the expression is
always True
◼ Can you
◼ Write an if-else statement that outputs the word
High if the value of the variable score is greater
than 100 and Low if the value of score is at most
100? The variables are of type int.
◼ easier to correct
◼ easier to change
if (x = = 0)
statement;
◼ Braces {} create groups
◼ Indent within braces to make the group clear
meaning
◼ Allow us to change all occurrences simply by
Display 2.17
Copyright © 2018 Pearson Addison-Wesley. All rights reserved.
Slide 2- 23
Section 2.5 Conclusion
◼ Can you
◼ Create a named constant of type double?