Professional Documents
Culture Documents
UNIT 3
3.1 Introduction
A program is usually not limited to a linear sequence of instructions. During its
process, it may bifurcate, repeat code or take decisions. For that purpose, C++
provides control structures that serve to specify what has to be done by our program,
when and under which circumstances.
With the introduction of control structures, we are going to have to introduce a new
concept: the compound- statement or block. A block is a group of statements which
are separated by semicolons (;) like all C++ statements, but grouped together in a block
enclosed in braces: { }:
Most of the control structures that we will see in this section require a generic
statement, as part of its syntax. A statement can be either a simple statement (a simple
instruction ending with a semicolon) or a compound statement (several instructions
grouped in a block), like the one just described. In the case that we want the statement
to be a simple statement, we do not need to enclose it in braces ({}). But in the case that
we want the statement to be a compound statement it must be enclosed between
braces ({}), forming a block.
1. if statement
It is clear that if the ‘if condition’
is true, statement 1 is executed,
syntax:
otherwise, it is skipped and a
directly statement 2 will be
if (condition) executed. The statement 1 may
{ either be a single or compound
statement.
statement(s);
}
if else’ statement
syntax:
if (condition) It is clear that the given
condition is evaluated first. If
{ the condition is true, statement1
statement1; is executed, followed by
} statement 3. If the condition is
false, statement2 is executed,
else
followed by statement 3. It
{ should be kept in mind that
statement 2; statement1 and statement2 can
} be single or compound
statement 3; statement.
Example 1: Write a program that will print the greatest of two numbers.
#include <iostream.h>
void main( )
int x, y;
cin>> x >> y ;
if ( x > y) // if condition
cout << “The bigger number is ” << x; // if true this statement will execute
Syntax:
switch (var / expression)
{
case constant1 : statement1;
|
|
|
break;
case constant2 : statement2;
|
|
The execution of switch statement begins with the evaluation
| of expression. If the value of the expression matches with the
break; constant then the statements following the expression will be
default : statement3; executed sequentially till the break statement.
Syntax:
do {
} while (condition);
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:
Write a program to find the sum of the first N natural number.
It is a count-controlled loop in the sense that the program knows in advance how
many times the loop has to be executed. Repeats a statement or group of statements
while a given condition is true.
Syntax:
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used
to test whether the condition is true or false. If the condition is true, the program
executes the body of the loop and then the value of loop control variable is updated.
Again, it checks the condition and so on. If the condition is false, it gets out of the
loop.
Use of “for” Statement
The output of the above program is; The output of the above program is;
2 | Computer Programming 1
#include <iostream.h>
int main( )
{
int num = 1;
STEP:do
{
if( num == 5)
{
num = num + 1;
goto STEP;
}
cout << “value of num : “ << num << endl;
num = num + 1;
}while( num < 10 );
return 0;
}
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.
The break statement skips rest of the loop and goes out of the loop.
2 | Computer Programming 1
The continue statement is used in loops and causes a program to skip the rest
of the body of the loop.
while (condition)
{ The continue statement skips rest
statement 1;
of the loop body and starts a new
if (condition)
continue; iteration.
statement 2;
}
statement 3;
The execution of a program can be stopped at any point with exit ( ) and a
status code can be informed to the calling program.
exit (code);
where code is an integer value. The code has a value 0 for correct execution. The value
of the code varies depending upon the operating system. It requires a process.h header
file.
2 | Computer Programming 1
Activity #3:
a. int s = 0, i = 0;
while ( i + + < 5)
s + = i;
b. int s = 0 ; i = 0;
do
s+=i;
while ( i < 5 ) ;
2. What will be the value of counter after the following program is executed?
void main ( )
{
int counter ;
int digit = 0 ;
counter = 1 ;
while (digit < = 10)
{
+ + counter ;
+ + digit ;
}
cout <<counter ;
}
a) 9 b) 10 c) 11 d) 12
{ result * = 10 ;
int digit = number % 10 ;
result + = digit ;
number / = 10 ;
}
while (number) ;
cout << “output =” << result << endl;
}
2 | Computer Programming 1
3.3 References
Malini Devi J. (2014). C++ Programming Language for Beginner.
Chapter 1 pp. 1-4
3.4 Acknowledgment
The syntax, table or images, and information contained in this module
were taken from the references cited above.