Page 1
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
CONTROL STATEMENTS / CONTROL STRUCTURE:
A Statement is an instruction or group of instructions that tells the computer what to do and how to do. Control
statements break normal sequence of program execution and transfers control to other parts of program.
DECISION MAKING STATEMENTS:
1. if statement
2. if /else statement
3. switch statement
1) if Statement: It is used to test a condition and takes action only when the given condition is true. If the
condition is false then it takes no action. It is also called decision statement
Its syntax is: if (condition)
{
Statement1;
Statement2;
.
.
Statement n;
}
If the block contains a single statement then braces are not required.
Example:
{
int m_obt;
cin >> m_obt;
If (m_obt >=33)
cout << “Passed”;
If (m_obt < 33)
cout << “Failed”;
}
2) if-else Statement:
It is use to test a condition and takes one action when the given condition is true and takes other action when the
condition is false. i.e. it selects a statement for execution from two statement.
Its syntax is :
If (condition)
{
Statement1;
Statement2;
.
.
Statement n;
}
else
{
Statement1;
Statement2;
.
.
Page 2
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
Statement n;
}
If the block contains a single statement then braces are not required.
Example: void main ( )
{
int m_obt;
cout<<”Enter student obtain marks”;
cin >>m_obt;
If (m_obt < 33)
cout << “Failed”;
else
cout <<”Passed”;
getch();
} }
4) switch Statement:
It is also called selection statement because it selects a statement for execution from several available
statements.
Its Syntax is:
Switch (integer expression)
{
case constant1:
statment1;
statement2;
.
break ;
case constant2:
statment1;
statement2;
.
break ;
.
.
.
default:
statement;
}
Example .
void main (void )
{
int a,b,s,ch;
cout<<”Enter two values”;
cin >>a<<b;
clrscr();
cout<<”1. Addition”<<endl;
cout<<”2. Multiplication”<<endl;
Page 3
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
cout<<”3. Division”<<endl;
cout<<”Enter your choice….”<<endl;
cin>>ch;
switch(ch) Output
{ Enter two values
case 1: 6
res=a+b; 2
cout<<”sum=”<<res; 1. Addition
2. Multiplication
break;
3. Division
case 2:
Enter your choice….2
res=a*b; Product = 12
cout<<”product=”<<res;
break;
case 3:
res=a/b;
cout<<”quotient =”<<res;
break;
default:
cout<<”wrong choice.”<<endl;
}
getch ( );
}
goto Statement:
It is unconditional transfer of control statement which transfers control to some remote part of a program
without prior condition.Its syntax is:
goto label;
………
…….
Label: statement;
Example. //This program will print * for infinite times
void main(void)
{
x:
cout<<”*”;
goto x;
}
LOOPING STATEMENTS:
1. for loop/Fixed loop/Counter loop
2. while loop/ Pre-Tested loop
3. do while loop/ Post-Tested loop
Loop:A statement or set of statements that is executed repeatedly is called loop. OR A technique, which execute
a statement or statements again and again. In C++ the following three types of loop statement are used
1) for Loop/ Fixed Loop/ Counter Loop:
It is used to execute a statement or block of statements with a specified number of time. This statement can
only be used when the no. of iterations are known in advance.
Its syntax is: for (initialization condition; inc/dec)
Page 4
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
{
Statement1;
Statement2;
.
.
Statement n;.
}
Example
void main ( ) void main ( )
{ {
int i; int i;
for (i = 1; i < = 5; i++) for (i=5; i>=1; i--)
cout << i <<endl; cout << i << endl;
getch ( ); getch ( );
} }
while Loop/ Pre-Tested Loop: It is used to execute the body of the loop repeatedly as long as the condition
remains true. As soon as the condition becomes false, the control will jump out from the loop. It is another type of
loop statement which executes a statement or block of statements when the no of iterations are known in
advance or not known. It is also called pre-tested
// A program for printing 1 2 3 4 5
It syntax is: ; void main ( )
while (condition) {
{ int i;
Statement 1; i = 1;
Statement 2; while ( i<=5)
. cout<< i<< “\t”;
Statement n; i = i +1;
}
} getch ( );
}
// A program for printing 54321
void main ( )
int i =5;
while ( i >= 1)
{
cout << i << “\t”;
i = i -1;
}
getch ( );
}
do-while Loop: It is similar to while loop except that in the case of do while, the condition is tested at the
end, after executing the body of the loop. It is also called Post-Tested Loop.
In this do while loop the body will must execute at least once in any condition even when the condition is initially
false. On the other hand the body of while loop may be executed zero time. Its syntax is:
Page 5
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
do {
statement 1;
statement 2;
.
.
Statement n;
}
while (condition);
void main ( ) void main ( )
{ {
int i = 1; int i = 5;
do { do {
cout << i << “\t”; cout << i << “\t”
i = i + 1; i = i -1;
} }
while (i<=5) while ( i >=1);
getch ( ); getch ( );
} }
break Statement: It has two uses. The 1st is to terminate a case in the switch statement. The 2 nd use is to
terminate or exit from the loop.it bring us out of the loop as soon as it is executed. When the break statement is
encountered inside a loop, the loop is immediately terminated and program control goes to the next statement
following the loop:
e.g: void main ( ) This program prints the numbers 0 through
{ 10 and then terminates, on conditional test i < =100.
int i;
for (i = 0; i <= 100; i ++)
cout << i << endl;
if ( i = = 10) break;
}
Continue Statement: continue statement is inserted in body of loop and brings us at the beginning of the loop as
soon as it is executed. For example the following program prints even numbers only.
void main ( )
{
int i;
for (i = 0; i <= 100; i ++)
if ( i %2 != 0) continue;
cout << i << endl;
}
Exercise Question
Q. No 5. Write a program using for loop that prints product of all odd numbers from 1 to 10.
#include<iostream.h>
#include<conio.h>
void main (void)
{
Page 6
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
int i,p;
for (i=1;i<=10;i++)
P=p*I;
cout<<”product of odd number 1 to 10 =”<<p<<endl;
getch();
}
Q. No.7 write a program that display the product of a number without repeting them. For
example , if the user enters 24, it will display 24*1, 12*2, 8*3, and 6*4
#include<iostream.h>
#include<conio.h>
void main (void)
{
int i,j,n, p=1;
cout<<"enter any no";
cin>> n;
for(i=n;i>=1;i--)
{
for(j=1;j<=n;j++)
{
p=i*j;
if (p==n)
cout<<i<<'*'<<j<<'='<<p<<endl;
}
}
getch();
}
Q. No 8. Write a C++ program to read the address of a person and exit when the user enter
dot(.) from the keyboard.
#include<iostream.h>
#include<conio.h>
void main (void)
{
char ch;
cout<<"enter address...";
ch=getche();
while (ch !='.')
{
ch=getche();
}
getch();
}
Q. No.9. Write a C++ program to find out the area of rectangle and if any side is zero then
display the message “There is no Rectangle”.
Page 7
Unit No. 4: Control Statements/ Structures (Computer Science 2nd Year)
#include<iostream.h>
#include<conio.h>
void main (void)
{
int len, width,area;
cout<<"enter length of rectangle...";
cin>>len;
cout<<"enter width of rectangle...";
cin>>width;
if(len==0 || width==0)
cout<<"There is no Rectangle"<<endl;
else
{
area=len*width;
cout<<"Area of Rectangle="<<area<<endl;
}
getch();
}
Q. No 10. Write a C++ program to input a character and display the message after testing
whether its vowel or consonant.
#include<iostream.h>
#include<conio.h>
void main (void)
{
char ch;
cout<<”eneter any characher”;
cin>>ch;
If(ch==’a’ || ch==’e’ || ch== ‘i’ || ch==’o’ || ch==’u’)
cout<<”It is a vowel..”<<endl;
else
cout<<”It is a consonant.l..”<<endl;
getch();
}