Unit-3 1 030000
Unit-3 1 030000
Question-1
What do you mean by control flow statements? Describe all the types of
control flow statements with proper syntax and example. Or,
What do you mean by iterative statements? Give all the types of iterative
statements. Or,
What do you mean by loop? What are the different types of loops? Give
syntax of all the types of loops. Or,
Write the syntax of following: (a) for loop (b) while loop (c) do while loop
Answer-1
Iterative or control flow statements are used whenever we have requirement
to repeat a particular block of statements till the given condition satisfies.
These are of following types-
1. for loop or Universal loop
2. while loop
3. do while loop
for loop
Syntax Flowchart
for(initialize counter ; test counter ; iteration counter)
{ ----------------------
---------------------- Block of statements
----------------------
}
Example: Program to print “HELLO WORLD” five
times.
#include<stdio.h>
void main( )
{
int i ;
for(i=1; i<=5; i++)
{
printf (“HELLO WORLD”);
}
}
while loop
Syntax Flowchart
initialize counter ;
while(test counter)
{
----------------------
---------------------- Block of statements
----------------------
iteration counter;
}
Example: Program to print “HELLO WORLD” five
times.
#include<stdio.h>
void main( )
{
int i ;
i = 1;
while(i<=5)
{
printf (“HELLO WORLD”);
i++ ;
}
}
do while loop
Syntax Flowchart
initialize counter ;
do
{
----------------------
---------------------- Block of statements
----------------------
iteration counter;
} while(test counter);
Example: Program to print “HELLO WORLD” five
times.
#include<stdio.h>
void main( )
{
int i = 1 ;
do
{
printf (“HELLO WORLD”);
i++ ;
} while(i<=5);
}
Prepared By: Mr. Ashish Kr. Gupta,
Assistant Professor, Dept. of CSE,
I.T.S Engineering College, Greater Noida
3|Page Unit-3
Question-2 What are the differences between while and do while loop?
Answer-2
while loop do while loop
It is an Entry level loop It is an exit level loop
It cannot be evaluated even a single It must be evaluated at least a
time, if condition fails at starting single time, even if condition fails at
starting
Syntax: Syntax:
Initialize counter; Initialize counter;
while(Test counter) do
{ {
-------------- --------------
-------------- Block of statements -------------- Block of statements
-------------- Iteration counter; -------------- Iteration counter;
} } while(Test counter);
Example: Example:
int i=1; int i=1;
while(i<=5) do
{ {
printf(“Hello World”); printf(“Hello World”);
i++ ; i++ ;
} } while(i<=5);
continue statement:
When continue statement is encountered inside any loop, it skips all the
remaining statements after the continue statement and goes to the next
iteration of loop.
Syntax: continue;
Example:
#include<stdio.h>
void main( )
{
int i, j;
for(i=1; i<=2; i++)
{
for(j=1; j<=2; j++)
{
if(i==j)
{
continue;
}
printf(“\n%d %d”,i ,j);
}
}
}