Saturday, May 4, 2019

Looping Statement in C

Looping statement are the statements execute one or more statement repeatedly several number of times. In C programming language there are three types of loops; while, for and do-while.

Why use loop ?

When you need to execute a block of code several number of times then you need to use looping concept in C language.

Advantage with looping statement

  • Reduce length of Code
  • Take less memory space.
  • Burden on the developer is reducing.
  • Time consuming process to execute the program is reduced.
loop

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

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.
while Loop

Syntax

Assignment;
while(condition)
{
Statements;
......
Increment/decrements (++ or --);
}
Note: If while loop condition never false then loop become infinite loop.

Example of while loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
while(i<5)
{
printf("\n%d",i);
i++;
}
getch();
}

Output

1
2
3
4

When while loop is use ?

When we do not know about how many times loops are perform or iteration of loop is unknown.

For loop

When you need to execute a block of code several number of times then you need to use looping concept in C language. In C Programming Language for loop is a statement which allows code to be repeatedly executed. It contains 3 parts.
  • Initialization
  • Condition
  • Increment or Decrements

Syntax

for ( initialization; condition; increment )
{
 statement(s);
}
For Loop
  • Initialization: This step is execute first and this is execute only once when we are entering into the loop first time. This step is allow to declare and initialize any loop control variables.
  • Condition: This is next step after initialization step, if it is true, the body of the loop is executed, if it is false then the body of the loop does not execute and flow of control goes outside of the for loop.
  • Increment or Decrements: After completion of Initialization and Condition steps loop body code is executed and then Increment or Decrements steps is execute. This statement allows to update any loop control variables.
Note: In for loop everything is optional but mandatory to place 2 semicolons (; ;)

Example

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

Flow Diagram

For Loop

Control flow of for loop

control flow
  • First initialize the variable, It execute only once when we are entering into the loop first time.
  • In second step check condition
  • In third step control goes inside loop body and execute.
  • At last increase the value of variable
  • Same process is repeat until condition not false.
For Loop

Example of for loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=1;i<5;i++)
{
printf("\n%d",i);
}
getch();
}

Output

1
2
3
4

Important Points

  • In 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; )
  • For loop is repeats in anti lock wise 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
Note: Always execution process of for loop is faster than while loop.


do-while

do-while loop is similar to a while loop, except that a do-while loop is execute at least one time.
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block (in while).

Syntax

do
{
Statements;
........
Increment/decrement (++ or --)
} while();
do while loop

When use do..while Loop

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

Example of do..while loop

#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
i=1;
do
{
printf("\n%d",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.


No comments:

Post a Comment

Why learning C Programming is a must?

C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as...