Ad Code

Loop in C/C++ programming language.

 What is loop?

Looping statements are used to perform the same type of task repeatedly. The task has been assigned inside the loop body and a checking/ testing condition has been applied to control the execution of the loop. Hence, the loop will execute until the testing condition will be true. After the condition becomes false, compiler will transfer the control of the statement outside the loop.

Simple syntax of loop:-

for(initialization; testing condition; modification)
{
  statement;
}


Loop are classified into two types:-

1. Entry controlled Loop/ Deterministic Loop
2. Exit controlled Loop/ Non-deterministic Loop

1. Entry controlled Loop:-

In these kinds of loop, the testing condition has been provided at the beginning of the loop. The body of the loop will only execute in the situation when the testing condition will be true otherwise the control will transfer outside the loop. Here the user will know the number of execution in advance.

For example: for loop, while loop.

2. Exit controlled Loop:-

In these kinds of loop, the testing condition has been provided at the exit point of the loop. The body of the loop will only execute in the situation when the testing condition will be true otherwise the control will transfer outside the loop.
* Note- In this type of controlled loop, the body of the loop will execute at least one time whether the condition is true or not.

For example: do while loop.

1. For loop:-

Syntax:

for(initialization; testing condition; modification)
{
  statement;
}
 
       OR

for(i=0; i<=10; i++)
{
  printf("%d", i);
}

First of all, the initialization is done as i=0, we apply the testing condition (i<=10). If the condition become true, the control of execution will enter inside the loop and execute the statement, i.e, printf("%d", i); means the value of i=0 will be print as output. After the completion of the execution inside the loop, control will return to the modification (i++) means i+1 and then again it will test the condition whether the value of i is true or not and again it will enter inside the loop.
The execution will repeat again and again until the condition becomes false.

Let's understand with an example.


#include<stdio.h>
#include<conio.h>
void main()
{
int i ;
clrscr();
for(i=1;i<=100;i++)
{
printf(“%d”,i);
printf(“\n”);
}
getch();
}


Output:-


2. While loop:-

Syntax:

initialization;
while(testing condition)
{
statements;
modification;
}

Let's take an example to understand the loop.



#include<stdio.h>
#include<conio.h>
void main()
{
int i ;
clrscr();
i=1;
while(i<=100)
{
printf(“%d”,i);
printf(“\n”);
i++;
}
getch();
}

Output:-




3. Do while loop:-

Syntax:

initialization;
do
{
statements;
modification;
}
while(testing condition);

Let's take an example to understand the code.


#include<stdio.h>
#include<conio.h>
void main()
{
int i ;
clrscr();
i=1;
do
{
printf(“%d”,i);
printf(“\n”);
i++;
}
while(i<=100);
getch();
}

Output:- 










Post a Comment

0 Comments

Ad Code