Professional Documents
Culture Documents
Ch3 Loops Decisions
Ch3 Loops Decisions
2. Selection - one of two paths are executed based on a condition
(true/false expression).
.
.
.
Boolean OK;
.
.
.
OK = TRUE;
.
.
.
OK = FALSE;
= = Equal
! = Not equal
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
Examples:
x != y
x>y
y == 0
ch >= 'A'
x<y-z
x y x && y x || y !x
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Examples:
c <= d || e == f
Precedence of Operators:
Examples:
dataInvalid = inputVal == 0;
dataInvalid = (inputVal == 0);
OK = x * 5 < y + 6;
done = !OK && x < y;
Relational operators and floating point types. Consider the following:
outputs
0.9999999
Syntax template
Example:
else
Example:
if (n >= 2)
{
alpha = 5;
beta = 15;
}
else
{
alpha = 23;
beta = 199;
Example:
if (n >= 2)
{
alpha = 5;
beta = 15;
}
Example:
cin >> n;
if (n = 3)
cout << "n equals 3";
else
cout << "n doesn't equal 3";
What actually happens in this example?
In the if statement, n is assigned the value 3. The assignment
operator also results in a value (3) which is then tested for
true/false. Since 3 is not 0, the result is true, and the message
n equals 3
is output.
To avoid such problems, you can write any test that includes a
constant as follows:
cin >> n;
if (3 == n)
if (x < 0)
if (y != 4)
z = y * x;
else
z = y / x;
else
if (y > 4)
z = y + x;
else
z = y - x;
if (day == 1)
if (day == 2)
if (day == 4)
if (day == 5)
if (day == 6)
if (day == 7)
if (day == 1)
else
if (day == 2)
else
if (day == 3)
else
if (day == 4)
else
if (day == 5)
cout << "Thursday";
else
if (day == 6)
else
if (day == 7)
if (day == 1)
else if (day == 2)
else if (day == 3)
else if (day == 4)
else if (day == 5)
else if (day == 6)
else if (day == 7)
else
else
else;
else
if (grade = 'A')
gradePt = 4.0;
gradePt = 3.0;
gradePt = 2.0;
gradePt = 1.0;
gradePt = 0.0;
else
The C++ switch statement provides a control structure for this type of
situation:
switch (grade)
{
case 'A' :
case 'B' :
case 'D :
case 'F' :
default :
If the value of the switch expression does not match any of the case
constants, control is transfered to the default case. If there is no default
case, control is passed to the statement following the brace at the end of
the switch statement.
3.2 Loops
3.2.1 The While Statement
A Loop is a control structure that causes a sequence of statements to be
executed repeatedly.
The While Statement tests a condition and repeats a block of one or more
statments as long as the condidition is true.
While statement syntax diagram:
Examples:
while (inputVal != 25)
x = 0;
while (x < 10)
{
}
Phases of While Loop Execution
o Initialization occurs prior to the while statement.
o Loop entry occurs the moment that the flow of control reaches the first
statement inside the loop body.
o Each time the body of the loop is executed, a pass is made through the
loop. This pass is called an iteration.
o Before each iteration, the loop test is made at the beginning of the
loop.
o When the last iteration is complete and flow of control has passed to
the first statement after the loop, the program has exited the loop.
o The condition that causes the loop to exit is called the exit condition.
Count-Controlled Loops
.
.
.
loopCount++;
}
// Loop 10 times (0, 1, ... 9)
loopCount = 0;
while (loopCount < 10)
{
.
.
.
loopCount++;
.
.
.
loopCount--;
Event-Controlled Loops
Examples:
sum += data;
sum += data;
cin >> data;
The while loop that we have been using tests its condition at the beginning of the
loop:
There are some applications where a loop that tests its condition at the
bottom of the loop is better. The do ... while statement provides such a
loop:
The Statement in the do ... while can be a single statement or a block of
statements enclosed in braces. The statement will always be executed at
least once:
Examples:
3.2.3 For Statement
A lot of loops are count controlled loops. The for statement makes it easy
to build such loops:
The for loop tests at the bottom of the loop just as the while loop does:
Examples:
is equivalent to:
count = 1;
while (count <= n)
{
cout << count << endl;
count++;
}
0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
and
for ( ; ; ; )
{
break;
is equivalent to:
while (1)
{
break;
The break statement is used to exit from the innermost switch or loop
control structure.
loopCount = 1;
while (TRUE)
{
break;
break;
}
break;
if (inputVal < 0)
continue;
dataCount++;
}
Note that when a negative value is read, the cout statement and the
dataCount++; statement are skipped, but the loop continues by reading
another value.