CH 2
CH 2
College of Engineering
Dept. of Electricity
Chapter Two
Control Statements
Ex 2-1: Write a C++ program to find the greater of two integer numbers.
11
University of Misan
College of Engineering
Dept. of Electricity
Note:
1. The if statemen:t
compares the values of variables a and b. If the value of a is greater or equal than b, the
cout statement displays a line of text indicating that the greater is a.
2. You can add more than one statement inside if statement, for example:
12
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-2: Write a C++ program to arrange three integer numbers in a descending order.
13
University of Misan
College of Engineering
Dept. of Electricity
Note: To include several statements in the body of an if or in either part of an if…else, enclose the
statements in braces ({ and }). A set of statements contained within a pair of braces is called
a compound statement or a block, for example:
14
University of Misan
College of Engineering
Dept. of Electricity
Note: Programmer can use more than one condition within if statement by using logical operators,
as shown below:
15
University of Misan
College of Engineering
Dept. of Electricity
As an example of C++’s while repetition statement, consider a program segment designed to find
the first power of 3 larger than 100. Suppose the integer variable product has been initialized to 3.
When the following while repetition statement finishes executing, product contains the result:
When the while statement begins execution, product’s value is 3. Each repetition multiplies product
by 3, so product takes on the values 9, 27, 81 and 243 successively. When product becomes 243,
the while statement condition (product <= 100) becomes false. This terminates the repetition, so the
final value of product is 243. At this point, program execution continues with the next statement
after the while statement.
Ex 2-4: A class of ten students toke a quiz, write a C++ program read these ten grades to
calculate the average.
16
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-5: Re-change the above program’s code to calculate the average of quiz grades. Program
terminated when -1 entered.
17
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-7: Use do…while repetition statement to write a C++ program printing the following table:
18
University of Misan
College of Engineering
Dept. of Electricity
When the for statement begins executing, the control variable counter is declared and initialized to
1. Then, the loop-continuation condition counter <= 10 is checked. The initial value of counter is
1, so the condition is satisfied and the body statements (cout << counter; and cout << endl;) prints
the value of counter, namely 1. Then, the expression ++counter increments control variable
counter and the loop begins again with the loop-continuation test. The control variable is now equal
to 2, so the final value is not exceeded and the program performs the body statement again. This
process continues until the loop body has executed 10 times and the control variable counter is
incremented to 11, this causes the loop-continuation test to fail and repetition to terminate.
19
University of Misan
College of Engineering
Dept. of Electricity
Note: if the control variable is declared at the initialization part of for statement, then it will be
used only inside the body of for statement.
Ex 2-8:
a) Vary the control variable from 1 to 100 in increments of 1.
for ( int i = 1; i <= 100; i++ )
e) Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17.
for ( int n = 2; n <= 17; n += 3 )
f) Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55.
for ( int r = 99; r >= 55; r -= 11 )
Ex 2-9: Write a C++ program to print the even integers from 2 to 100 with their sum.
20
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-10: A person invests $1000.00 in a savings account yielding 5 percent interest. Assuming that
all interest is left on deposit in the account, calculate and print the amount of money in the
account at the end of each year for 10 years. Use the following formula for determining
these amounts:
amount = principle ( 1 + rate )year
where principle is the original amount invested , rate is the annual interest rate, year is the number
of years and amount is the amount on deposit at the end of the nth year.
Use the standard library function pow, the function pow(x, y) calculates the value of x raised to the
yth power.
21
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-12: Write a C++ program to find the largest number from ten integer numbers.
Ex 2-13: Write a C++ program to read a square length and then draw the square using *.
22
University of Misan
College of Engineering
Dept. of Electricity
( 6 )10 = ( 110 )2 0 6
1 3
1 1
0
23
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-15: Write a C++ program read a student grade (-1 to end) and print:
24
University of Misan
College of Engineering
Dept. of Electricity
Note:
1. Forgetting a break statement when one is needed in a switch statement is a logic error.
2. If no match occurs between the controlling expression’s value and a case label, the default
case executes.
3. The last case in a switch statement (default) does not require a break statement
25
University of Misan
College of Engineering
Dept. of Electricity
Ex 2-16: A mail order house sells five different products whose retail prices are: product 1- $2.98,
product 2- $4.50, product 3- $9.98, product 4- $4.49 and product 5- $6.87. Write a C++
program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
The program should use a switch statement to determine the retail price for each product, the
program stop when -1 entered.
26
University of Misan
College of Engineering
Dept. of Electricity
When the if statement detects that count is 5, the break statement executes. This terminates the for
statement, and the program proceeds to the first statement after the for statement, which displays a
message indicating the control variable value that terminated the loop. The for statement fully
executes its body only four times instead of 10. The control variable count is defined outside the for
statement header, so that we can use the control variable both in the loop’s body and after the loop
completes its execution.
27
University of Misan
College of Engineering
Dept. of Electricity
continue Statement:
The continue statement, when executed in a while, for or do…while statement, skips the remaining
statements in the body of that statement and proceeds with the next iteration of the loop. In while
and do…while statements, the loop-continuation test evaluates immediately after the continue
statement executes. In the for statement, the increment expression executes, then the loop-
continuation test evaluates.
The example below uses the continue statement in a for statement to skip the output statement
when the nested if determines that the value of count is 5. When the continue statement executes,
program control continues with the increment of the control variable in the for header and loops
five more times.
28