Chapter 10 Control Statements1
Chapter 10 Control Statements1
Chapter 10 Control Statements1
Chapter-10
CONTROL STATEMENTS
Introduction
Control statements are statements that alter the sequence of flow of instructions.
Any single input statement, assignment and output statement is simple statement.
A group of statement that are separated by semicolon and enclosed within curled braces { and } is
called a block or compound statement.
The order in which statements are executed in a program is called flow of control.
if statement :
This is the simplest form of if statement.
This statement is also called as one-way branching.
This statement is used to decide whether a statement or set of statements should be executed or
not.
The decision is based on a condition which can be evaluated to TRUE or FALSE.
The general form of simple – if statement is:
Statement 1;
Statement 2;
Here, the test condition is tested which results in either a TRUE or
FALSE value. If the result of the test condition is TRUE then the
Statement 1 is executed. Otherwise, Statement 2 is executed.
Ex: if( amount > = 5000 )
discount = amount * (10/100);
net-amount = amount – discount;
#include<iostream.h>
#include<conio.h>
void main( )
{
int a, b, c;
int largest, smallest, seclargest;
clrscr( );
cout<<”Enter the three numbers”<<endl;
cin>>a>>b>>c;
largest = a; //Assume first number as largest
if(b>largest)
largest = b;
if(c>largest)
largest = c;
#include<iostream.h>
#include<conio.h>
void main( )
{
float TAmount, discount, FAmount ;
clrscr( );
cout<<”Enter the Total Amount”<<endl;
cin>>TAmount;
Practical Program 7: Write a C++ program to check whether a given year is a leap year not, Using if
- else statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
int year ;
clrscr( );
cout<<”Enter the Year in the form YYYY”<<endl;
cin>>year;
if(year%4 ==0 && year%100!=0 || year%400 ==0)
cout<<year<<” is a leap year”<<endl;
else
cout<<year<<” is not leap year”<<endl;
getch( );
}
Practical Program 8: Write a C++ program to accept a character. Determine whether the character is
a lower-case or upper-case letter.
#include<iostream.h>
#include<conio.h>
void main( )
{
char ch ;
clrscr( );
cout<<”Enter the Character”<<endl;
cin>>ch;
if(ch>= ‘A’ && ch <=’Z’)
cout<<ch<<” is an Upper-Case Character”<<endl;
else
if(ch>= ‘a’ && ch <=’z’)
cout<<ch<<” is an Lower-Case Character”<<endl;
else
cout<<ch<<” is not an alphabet”<<endl;
getch( );
}
Nested if statement :
If the statement of an if statement is another if statement then such an if statement is called as
Nested-if Statement.
Nested-if statement contains an if statement within another if statement.
There are two forms of nested if statements.
if – else - if statement :
This structure helps the programmer to decide the execution of a statement from multiple
statements based on a condition.
4 Mrs. SOWJANYA, Computer Science Lecturer
Malnad PU College, Shivamogga
Chapter 10- Control Statements I PUC, Malnad PU College, Shimoga
Practical Program 9: Write a C++ program to input the number of units of electricity consumed in a
house and calculate the final amount using nested-if statement. Use the following data for calculation.
#include<iostream.h>
#include<conio.h>
void main( )
{
int units ;
float Billamount;
clrscr( );
cout<<”Enter the number of units consumed”<<endl;
cin>>units;
if(units < 30)
Billamount = units * 3.50 ;
else
if(units < 50)
Billamount = 29 * 3.50 + (units – 29) * 4.25 ;
else
if(units < 100)
Billamount = 29 * 3.50 + 20 * 4.25 + (units – 49) * 5.25 ;
else
Billamount = 29 * 3.50 + 20 * 4.25 + 50 * 5.25 + (units – 99) * 5.85 ;
Switch Statement :
C++ has built in multiple-branch selection statement i.e. switch.
If there are more than two alternatives to be selected, multiple selection construct is used.
The general form of Switch statement is:
Switch ( Expression )
{
Case Label-1: Statement 1;
Break;
Case Label-2: Statement 1;
Break;
…………..
Case Label-N: Statement N;
Break;
Default : Default- Statement;
}
Ex: To find the name of the day given the day number
switch ( dayno )
{
Case 1: cout<< “Sunday”<<endl;
break;
Case 2: cout<< “Monday” <<endl;
break;
Case 3: cout<< “Tuesday” <<endl;
break;
Case 4: cout<< “Wednesday” <<endl;
break;
Case 5: cout<< “Thursday” <<endl;
break;
Case 6: cout<< “Friday” <<endl;
break;
Case 7: cout<< “Saturday” <<endl;
break;
default: cout<< “Invalid Day Number” <<endl;
}
The switch statement is a bit peculiar within the C++ language because it uses labels instead of
blocks.
This force up to put break statements after the group of statements that we want to execute for a
specific condition.
Otherwise the remainder statements including those corresponding to other labels also are
executed until the end of the switch selective block or a break statement is reached.
Practical Program 10: Write a C++ program to input the marks of four subjects. Calculate the total
percentage and output the result as either “First Class” or “Second Class” or “Pass Class” or “Fail”
using switch statement.
Class Range (%)
First Class Between 60% to 100%
Second Class Between 50% to 59%
Pass Class Between 40% to 49%
Fail Less than 40%
#include<iostream.h>
#include<conio.h>
void main( )
{
int m1, m2, m3, m4, total, choice;
float per;
clrscr( );
cout<<”Enter the First subject marks”<<endl;
cin>>m1;
cout<<”Enter the Second subject marks”<<endl;
cin>>m2;
cout<<”Enter the Third subject marks”<<endl;
cin>>m3;
cout<<”Enter the Fourth subject marks”<<endl;
cin>>m4;
total = m1 + m2 + m3 + m4;
per = (total / 400) * 100;
cout<<”Percentage = “<<per<<endl;
o while loop
o do while loop
o for loop
while loop:
This is a pre-tested loop structure.
This structure checks the condition at the beginning of the structure.
The set of statements are executed again and again until the condition is true.
When the condition becomes false, control is transferred out of the structure.
The general form of while structure is
while ( Test Condition)
{
Statement 1
Statement 2
……..
Statement N
}End of While
Example:
n = 10;
While ( n > 0)
{
cout<<n<<”\t”;
- - n;
}
cout<<”End of while loop \n”;
Output: 10 9 8 7 6 5 4 3 2 1 End of while loop
Practical Program 11: Write a C++ program to find sum of all the digits of a number using while
statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, sum, rem;
clrscr( );
cout<<”Enter the Number”<<endl;
cin>>num;
sum = 0;
while(num!=0)
{
rem = num % 10;
sum = sum + rem;
num = num/10;
}
cout<<”Sum of the digits is “<<sum<<endl;
getch( );
}
Practical Program 12: Write a C++ program to input principal amount, rate of interest and time
period. Calculate compound interest using while statement.
(Hint: Amount = P * (1 + R/100)T, Compound Interest = Amount – P)
#include<iostream.h>
#include<conio.h>
void main( )
{
float pri, amt, priamt, rate, ci;
int time, year;
clrscr( );
cout<<”Enter the Principal amount, rate of interest and time”<<endl;
cin>>pri>>rate>>time;
year = 1;
priamt = pri;
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, m, flag;
clrscr( );
cout<<”Enter the Number”<<endl;
cin>>num;
m = num;
flag = 1;
while(num>2)
if(num % 2 ==1)
{
flag = 0;
break;
}
else
num = num/2;
if(flag)
cout<<m<<” is power of 2 “<<endl;
else
cout<<m<<” is not power of 2 “<<endl;
getch( );
}
do while statements:
This is a post-tested loop structure.
This structure checks the condition at the end of the structure.
The set of statements are executed again and again until the condition is true.
When the condition becomes false, control is transferred out of the structure.
The general form of while structure is
do
{
Statement 1
Statement 2
……..
Statement N
} while ( Test Condition);
Example:
i = 2;
do
{
cout<<i<<”\t”;
i = i + 2;
}
while ( i < = 25);
Practical Program 14: Write a C++ program to check whether the given number is an Armstrong
Number using do-while statement. (Hint: 153 = 13 + 53 + 33)
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, rem, sum, temp;
clrscr( );
cout<<”Enter the three digit number”<<endl;
cin>>num;
temp = num;
sum = 0;
do
{
rem = temp % 10;
sum = sum + rem * rem * rem;
temp = temp / 10;
}while(temp!=0);
if(sum == num)
cout<<num<<” is an Armstrong Number “<<endl;
else
cout<<num<<” is not an Armstrong Number “<<endl;
getch( );
}
Difference between while and do while loop:
while do while
This is pre- tested looping structure This is post tested looping structure
It tests the given condition at initial point It tests the given condition at the last of
of looping structure looping structure.
Minimum execution of loop is zero Minimum execution of loop is once.
Syntax: Syntax:
while ( Test condition ) do
{ {
statement 1; statement 1;
statement 2; statement 2;
…………….; statement n;
statement n; }
} while ( Test condition);
Semi colon is not used. Semi colon is used.
for statement:
This structure is the fixed execution structure.
This structure is usually used when we know in advance exactly how many times asset of
statements is to be repeatedly executed again and again.
This structure can be used as increment looping or decrement looping structure.
The general form of for structure is as follows:
for ( Expression 1; Expression 2; Expression 3)
{
Statement 1;
Statement 2;
Statement N;
}
Where, Expression 1 represents Initialization
Expression 2 represents Condition
Expression 3 represents Increment/Decrement
Example:
sum = 0;
for ( i=1; i<=10; i++)
sum = sum + i;
Practical Program 15: Write a C++ program to find the factorial of a number using for statement.
(Hint: 5! = 5 * 4 * 3 * 2 * 1 = 120)
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, fact, i;
clrscr( );
cout<<”Enter the number”<<endl;
cin>>num;
fact = 1;
for( i = 1; i<= num; i++)
fact = fact * i;
cout<<” The factorial of a ”<<num<<”! is: “<<fact<<endl;
getch( );
}
Practical Program 16: Write a C++ program to generate the Fibonacci sequence up to a limit using
for statement. (Hint: 5 = 0 1 1 2 3)
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, first, second, count, third;
clrscr( );
cout<<”Enter the number limit for Fibonacci Series”<<endl;
cin>>num;
first = 0;
second = 1;
cout<<first<<”\t”<<second;
third = first + second;
for( count = 2; third<=num; count++)
{
cout<<”\t”<<third
first = second;
second = third;
third = first + second;
}
cout<<”\n Total terms = “<<count<<endl;
getch( );
}
break statement
The break statement has two uses
o You can use it to terminate a case in the switch statement.
o You can also use it to force immediate termination of a loop like while, do-while and for, by
passing the normal loop conditional test.
When the break statement is encountered inside a loop, the loop is immediately terminated and
program control resumes at the next statement.
The general form of break statement is:
break;
Example:
for (n=0; n<100; n++)
{
cout<<n;
if(n==10) break;
}
Program: To test whether a given number is prime or not using break statement.
#include<iostream.h>
#include,conio.h>
void main( )
{
int n, i, status;
clrscr( );
cout<<”Enter the number”;
cin>>n;
status=1;
for(i=2; i<=n/2; i++)
{
if(n % i ==0)
{
status=0
cout<<”It is not a prime”<<endl;
break;
}
}
if(status)
cout<<”It is a prime number”<<endl;
getch( );
}
exit( ) function:
This function causes immediate termination of the entire program, forcing a return to the operating
system.
In effect, exit( ) function acts as if it were breaking out of the entire program.
The general form of the exit( ) function is:
exit( ); or void exit( int return_code);
The return code is used by the operating system and may be used by calling programs.
An exit code of 0 means that the program finished normally and any other value means that some
error or unexpected results happened.
Program: To test whether a given number is prime or not using exit( ) statement.
#include<iostream.h>
#include,conio.h>
void main( )
{
int n, i;
clrscr( );
cout<<”Enter the number”;
cin>>n;
for(i=2; i<=n/2; i++)
{
if(n % i ==0)
{
cout<<”It is not a prime”<<endl;
exit(0);
}
}
cout<<”It is a prime number”<<endl;
getch( );
}
continue statement:
The continue statement causes the program to skip the rest of the loop in the current iteration as if
end of the statement block has been reached, causing it to jump to start of the following iteration.
The continue statement works somewhat like break statement.
Instead of forcing termination, however continue forces the next iteration of the loop to take place,
skipping any code in between.
The general form of the continue statement is:
continue;
Example:
for (n=10; n<0; n--)
{
if(n==10) continue;
cout<<n;
}
goto statement:
The goto allows to makes an absolute jump to another point in the program.
This statement execution causes an unconditional jump or transfer of control from one statement to
the other statement with in a program ignoring any type of nesting limitations.
The destination is identified by a label, which is then used as an argument for the goto statement.
A label is made of a valid identifier followed by a colon (:).
The general form of goto statement is:
statement1;
statement2;
goto label_name;
statement 3;
statement4;
label_name: statement5;
statement6;
Program: To print from 10 to 1 using goto statements.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int n=10;
loop: cout<<”\t”<<n;
n--;
if(n>0) goto loop;
cout<<”End of loop”;
getch( );
}