0% found this document useful (0 votes)
4 views5 pages

Unit-3 1 030000

The document explains control flow statements, particularly iterative statements including for loops, while loops, and do while loops, along with their syntax and examples. It also compares while and do while loops, highlighting their entry and exit characteristics. Additionally, it covers break and continue statements with their syntax and examples.

Uploaded by

Mohammad Hisham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Unit-3 1 030000

The document explains control flow statements, particularly iterative statements including for loops, while loops, and do while loops, along with their syntax and examples. It also compares while and do while loops, highlighting their entry and exit characteristics. Additionally, it covers break and continue statements with their syntax and examples.

Uploaded by

Mohammad Hisham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1|Page Unit-3

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”);
}
}

Prepared By: Mr. Ashish Kr. Gupta,


Assistant Professor, Dept. of CSE,
I.T.S Engineering College, Greater Noida
2|Page Unit-3

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);

Prepared By: Mr. Ashish Kr. Gupta,


Assistant Professor, Dept. of CSE,
I.T.S Engineering College, Greater Noida
4|Page Unit-3

Question-3 Write short notes on following with example:


1. break statement
2. continue statement
Answer-3
break statement:
When a break is encountered inside any loop, control automatically passes
to the first statement out of the loop. A break is usually associated with an if
or switch case statement.
Syntax: break;
Example:
#include<stdio.h>
void main( )
{
char c;
for( ; ; )
{
printf( "Press any key, Q to quit: " );
scanf("%c", &c);
if (c == 'Q' )
break;
else
{
printf(“%d”, i );
i++ ;
}
}
}

continue statement:

Prepared By: Mr. Ashish Kr. Gupta,


Assistant Professor, Dept. of CSE,
I.T.S Engineering College, Greater Noida
5|Page Unit-3

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);
}
}
}

Prepared By: Mr. Ashish Kr. Gupta,


Assistant Professor, Dept. of CSE,
I.T.S Engineering College, Greater Noida

You might also like