Control Flow Statement


Prev Tutorial Next Tutorial

Control Flow Statement

Set of instructions given to the compiler to execute set of statements until condition becomes false is called loops. The basic purpose of loop is code repetition that means same code repeated again and again.

Why use Loop

Where need repetition of same code a number of times at that place use Loop in place of writing more than one statements. The way of the repetition will be forms a circle that's why repetition statements are called loops.

loop

Advantage with looping statement

  • Reduce length of Code
  • take less memory space.

Types of Loops.

There are three type of Loops available in 'C' programming language.

  • while loop
  • for loop
  • do-while

Difference Between Conditional and Looping Statement in C++

Conditional statement executes only once in the program where as looping statements executes repeatedly several number of time.

While loop

In while loop First check the condition if condition is true then control goes inside the loop body other wise goes outside the body. while loop will be repeats in clock wise direction.

Syntax

Assignment;
while(condition)
{
Statements;
............
Increment/decrement (++ or --);
}

Note: If while loop condition never false then loop become infinite loop.

Example of while loop

#include<iostream.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
while(i<5)
{
cout<<endl<<i;
i++;
}
getch();
}

Output

1
2
3
4

For loop

For loop contains 3 parts.

  • Initialization
  • Condition
  • Iteration
For Loop For Loop
  • When we are working with for loop always execution process will start from initialization block.
  • After initialization block control will pass to condition block, if condition is evaluated as true then control will pass to statement block.
  • After execution of the statement block control will pass to iteration block, from iteration it will pass back to the condition.
  • Always repetitions will happen beginning condition, statement block and iteration only.
  • Initialization block will be executed only once when we are entering into the loop first time.
  • When we are working with for loop everything is optional but mandatory to place 2 semicolons (; ;)

Example

while()		// Error
for( ; ; )		// valid

Example of for loop

#include<iostream.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=1;i<5;i++)
{
cout<<endl<<i;
}
getch();
}

Output

1
2
3
4
  • when we are working with the for loop if condition part is not given then it will repeats infinite times, because condition part will replace it non-zero. So it is always true like.
    for( ; 1; )
  • Whenever we are working with for loop it repeats in antilock direction.
  • In for loop also rechecking process will be occurred that is before execution of the statement block, condition part will evaluated.

Example

while(0)	// no repetition
for( ; 0; )	// it will repeats 1 time

In for loop whenever the condition part is replace with constant zero then it will repeats 1 st become nember of instance become 1 at the time of compilation.

Example

int i;
i=0;
while(i)	// no repetition

for( ; ; )	// no repetition

Always execution process is faster than while loop when we are working with for.

do-while

In do-while loop post checking of the statement block condition part will be executed.

Syntax

do
{
Statements;
........
Increment/decrement (++ or --)
}
while();
Do While Loop

When use Do-While Loop

When we need to repeat the statement block atleast 1 time then we use do-while loop.

Example of do-while Loop

Example

#include<iostream.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
do
{
cout<<endl<<i;
i++;
}
while(i<5);
getch();
}

Output

1
2
3
4

Nested loop

In Nested loop one loop is place within another loop body.

When we need to repeated loop body itself n number of times use nested loops. Nested loops can be design upto 255 blocks.


Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements