Lesson 14
Lesson 14
Programming in C++
14
In the previous lesson you have learnt about general concepts of OOP. Now
you will learn about control statements which help you to control movements
of data and also help you to make decisions based on the conditions. The normal
flow of execution in a high level language is sequential, i.e., each statement is
executed in the order of its appearance in the program. However, depending
on the requirements of a problem it might be required to alter the normal
sequence of execution in a program. The statements which specify the order
of execution of statements are called control flow statements. There are many
control flow statements provided in C++ that will be discussed in this lesson.
OBJECTIVES
After reading this lesson, you will be able to:
z differentiate between a statement and compound statement;
z learn about the conditional statements and their use;
z familiarize with the loop statement;
z differentiate between while and do-while loop;
z identify jump statements;
z explain how to exit from the program.
14.1 STATEMENTS
Statements are the instructions given to the computer to perform any kind of
action. Action may be in the form of data movement, decision making etc.
Statements form the smallest executable unit within a C++ program, which are
always terminated by semicolon.
Writing only a semicolon indicates a null statement. Thus ‘;’ is a null or empty
statement. This is quite useful when the syntax of the language needs to specify
a statement but the logic of the program does not need any statement. This
statement is generally used in for and while looping statements.
From the Fig 14.2, it is clear that the given condition is evaluated first. If the
condition is true, statement1 is executed, followed by statement 3. If the
condition is false, statement2 is executed, followed by statement 3. It should
be kept in mind that statement1 and statement2 can be single or compound
statement.
Example 1
Write a program that will print the greatest of two numbers.
# include <iostream.h>
void main( )
{
int x, y;
cout << “Enter two numbers” <<“\n”;
cin>> x >> y ;
if ( x > y) // if condition
cout << “The bigger number is ” << x; // if true this statement will execute
else // if false this statement will execute
cout << “The bigger number is” <<y;
}
The output of the above program is:
Notes
The Fig 14.6 indicates that a condition is first evaluated. If the condition is true,
then the loop body will be executed and the condition will be re-evaluated.
Hence, the loop body is executed repeatedly as long as the condition remains
true. As soon as the condition becomes false, it comes out of the loop and goes
to the statement next to the ‘while’ loop.
Write a program to find the sum of first ten natural numbers i.e. 1, 2, 3, ........10.
# include <iostream.h>
void main( )
{ int n, total = 0 ;
Notes n=1;
while (n < = 10) // while condition
{
total + = n ;
n++;
}
cout << “sum of first ten natural number is” << total :
}
The variable n is called a loop control variable since its value is used to control
loop repetition. Normally, the three operations listed below must be performed
on the loop control variable.
Operation (i) must be performed before the loop is entered. Operation (ii) must
be performed before each execution of the loop body, depending on the result
of this test, the loop will either be repeated or make an exit. Operation (iii) must
be included as part of the loop body. Unless the loop control variable is updated
in the loop body, its value will not change and loop exit will never occur.
Notes
Fig. 14.7: do – while loop
The Fig 14.7 indicates that after each execution of the loop body, the condition
will be checked. If the condition is true, then the loop body will be executed
again. If the condition evaluates to false, loop exit occurs and the next program
statement is executed.
Note : The loop body is always executed at least once in the do-while loop.
One important difference between while loop and do-while loop is the relative
ordering of the conditional test and loop body execution. In the while loop, the
loop repetition test is performed before each execution of the loop body; the
loop body is not executed at all if the initial test fails. In the do-while loop, the
loop termination test is performed after each execution of the loop body; hence,
the loop body is always executed at least once.
Example 8
Write a program to find the sum of the first N natural number.
# include < iostream.h>
void main( )
{
int N, number, sum;
cin >> N;
sum = 0;
number = 1;
do
{
sum + = number;
number + + ;
} while (number < = N) ;
cout << sum;
}
The Fig 14.8 indicates that in for loop three operations take place:
(i) Initialization of loop control variable
(ii) Testing of loop control variable
(iii) Update the loop control variable either by incrementing or decrementing.
Notes
Notes
(b) Break statement
The break statement can be used in a switch statement and in any of the loops.
It causes program execution to pass to the next statement following the switch
or the loop.
Syntax of break statement.
while (condition)
The difference between the break and
{
continue statement is that break
statement 1; statement comes out of the loop and
if (condition) executes the next statement after the
break; loop. Contine statement skips rest of
the loop and starts new iteration of the
statement 2; same loop.
}
statement 3;
The break statement skips rest of the loop and goes out of the loop.
(c) Continue statement
The continue statement is used in loops and causes a program to skip the rest
of the body of the loop.
while (condition)
{
statement 1;
if (condition)
continue;
statement 2;
}
statement 3;
The continue statement skips rest of the loop body and starts a new iteration.
Notes }
cout << “\n” <<count ;
}
6 What will be the output of the above program ?
# include < iostream.h>
# include < stdio.h>
void main ( )
{
char ch = ‘A’ ;
while (ch < = ‘F’ )
{
switch (ch)
{
case ‘A’ :
case ‘B’ :
case ‘C’ :
case ‘D’ : ch + + ; continue ;
case ‘E’ :
case ‘F’ : ch + + ; }
putchar (ch) ;
}
}
a) ABCDEF b) FG c) EFG d) None of the above
z In the while loop, the loop repetition test is performed before each execution
of the loop body; the loop body is not executed at all if the initial test fails.
z In the do-while loop, the loop termination test is performed after each
execution of the loop body. Hence the loop body is always executed at least
once.
z Continue statement is used in loops and causes a program to skip the rest
of the body of the loop.
z The execution of the program can be stopped at any point with exit()
statement.
TERMINAL EXERCISE
1. Write a program to display the following menu and accept a choice number.
If an invalid choice is entered, then appropriate error message must be
displayed. Otherwise the choice number entered must be displayed.
MENU
1. Create a data file
4. Exit
Choice :
3. Write a program that will print sum of N integers entered by the user.
6. Write a program that will print the table of any number upto any number.
input :
Enter the table of which number : 4
Enter upto which number : 5
Output
4×1=4
4×2=8
4 × 3 = 12
4 × 4 = 16
4 × 5 = 20
Continue (Y/N) ?
8. Write a program to print all the tables between 2 and 20 upto 10.
Example : 3, 4, 5
52 = 32 + 42
25 = 9 + 16 = 25
10. Write a program to find out the sum of the series 1 + x + x2 + x2 ........... xn
14.1
1 a) # include <iostream.h>
void main( )
{
int a, b, max;
cin >> b >>a;
if (a > b) max = a;
}
b) # include <iostream.h>
void main ( )
{
int x, y;
cin >> x;
for (y = 0; y < 10 ; y++)
if (x = = y)
2. a) (i) x is not 10
(ii) x is 10
Notes
b) (i) Leap
(ii) No output
c) (i) Leap
(ii) Not century year
3. c) 285
4. c) i = 5, k = 4
5. Hello
6. b) RED error
14.2
1. a) 5 times b) endless
2. a) output = 2 7 3 6 6 8 7 b) 2 2 1 1
3. d) 12
4. b) 2
5. b) Display integers from 0 to 10
6. a) 49
14.3
1. (ii) a label
2. (iv) from the innermost loop or switch
3. the decision statement of loop
4. process.h
5. 9
6. (b) FG