LOOPING STATEMENTS


Looping Statements / Iterative Statements :

'A loop' is a part of code of a program which is executed repeatedly.
A loop is used using condition. The repetition is done until condition becomes condition true.
A loop declaration and execution can be done in following ways.
  • Check condition to start a loop
  • Initialize loop with declaring a variable.
  • Executing statements inside loop.
  • Increment or decrement of value of a variable.

* TYPES OF LOOPING STATEMENTS :

Basically, the types of looping statements depends on the condition checking mode. Condition checking can be made in two ways as : Before loop and after loop. So, there are 2(two) types of looping statements.
  • Entry controlled loop
  • Exit controlled loop
1. Entry controlled loop :
In such type of loop, the test condition is checked first before the loop is executed.
Some common examples of this looping statements are :
  • while loop 

  • For loop                    
 Required Example are Given Below For Above   "WHILE LOOP ,  FOR LOOP" .                          

While loop :

This is an entry controlled looping statement. It is used to repeat a block of statements until condition becomes true.
Syntax:

while(condition)
{
 statements;
 increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.

Program :


/*  Program to demonstrate while loop.

Creation Date : 30 MAR 2012 04:45:01 AM
Author : HARISH */

#include <stdio.h>
#include <conio.h>
void main()
{
 int a;
 clrscr();
 a=1;
 while(a<=5)
 {
  printf("\n TechnoExam");
  a+=1      // i.e. a = a + 1
 }
 getch();
}

Output :

 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam_



For loop :

This is an entry controlled looping statement.
In this loop structure, more than one variable can be initilized. One of the most important feature of this loop is that the three actions can be taken at a time like variable initilisation, condition checking and increment/decrement. The for loop can be more concise and flexible than that of while and do-while loops.
Syntax:

for(initialisation; test-condition; incre/decre)
{
 statements;
}

In above syntax, the given three expressions are seperated by ';' (Semicolon)
Features :
  • More concise
  • Easy to use
  • Highly flexible
  • More than one variable can be initilized.
  • More than one increments can be applied.
  • More than two conditions can be used.

Program :


/*  Program to demonstrate for loop.

Creation Date : 30 MAR 2012 04:52:31 PM

Author :HARISH */

#include <stdio.h>
#include <conio.h>
void main()
{
 int a;
 clrscr();
 for(i=0; i<5; i++)
 {
  printf("\n\t TechnoExam");  // 5 times
 }
 getch();
}

Output :

 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam_

2. Exit controlled loop :
In such type of loop, the loop is executed first. Then condition is checked after block of statements are executed. The loop executed atleat one time compulsarily.
Some common example of this looping statement is :
  • do-while loop


Do-While loop :

This is an exit controlled looping statement.
Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In this, block of statements are executed first and then condition is checked.
Syntax:

do
{
 statements;
 (increment/decrement);
}while(condition);

In above syntax, the first the block of statements are executed. At the end of loop, while statement is executed. If the resultant condition is true then program control goes to evaluate the body of a loop once again. This process continues till condition becomes true. When it becomes false, then the loop terminates.
Note: The while statement should be terminated with ; (semicolon).

Program :


/*  Program to demonstrate do while loop.

Creation Date : 30 MAR 2012 04:55:01 AM

Author :HARISH */

#include <stdio.h>
#include <conio.h>
void main()
{
 int a;
 clrscr();
 a=1;
 do
 {
  printf("\n\t TechnoExam");  // 5 times
  a+=1;      // i.e. a = a + 1
 }while(a<=5);
 a=6;
 do
 {
  printf("\n\n\t Technowell");  // 1 time
  a+=1;      // i.e. a = a + 1
 }while(a<=5);
 getch();
}

Output : 


 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam
 TechnoExam
 
 Technowell_

No comments: