Looping (or Iteration):
Repeating some portion of the program either for a fixed number of times or as long as the condition is satisfied is called looping or iteration. there are three type of loop control structure in C. They are:1. The For Loop:
2. The While Loop:
3. The Do-While Loop:
1. The For Loop:
Initialization of the loop counter is done first and the specified condition is tested. If the condition is satisfied then the body of the loop is executed. After each execution, the loop counter is incremented and again the condition is evaluated. The process continues until the condition becomes false.The general syntax of for loop is
for(initialization;test condition;increment)
{ statement 1;
statement 2;
...................
statement n;
}
Example: Write a program to print first 25 natural numbers?
Solution:Logic-->Here , we just print first 25 natural number using loop. we print our result 25 times using loop. first we initialize loop counter i=1, which then tested with condition to i<=25.If true print our first output as 1 and goes for increment by i++(i.e. i=i+1) and step again continue with loop counter i=2 and checks condition,prints and increment and vice versa steps continues otherwise terminate program.
Program-->
#include<stdio.h>
#include<conio.h>
void main()
{int i;
for(i=1;i<=25;i++)
{printf("%3d",i);
}
getch();
}
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Note:Here we use "%3d" which means that it print the result in 3 times keyboard spacebar spacing place
2. The While Loop:
The while loop is an array controlled loop structure. The test condition is evaluated first and if the condition is satisfied then only the statement within the body of the loop get executed. The condition is tested after each execution and control is transferred out of the loop when the condition fails. The basic format of the while loop is:initialize loop counter
while(test condition)
{
statement 1;
statement 2;
..........................
statement n;
increment loop counter;
}
Example: Write a program to print first 25 natural numbers?
Solution:Logic--> Here we take all looping process individually and cycling one. First we initialize loop counter i=1 which continue to enter in while loop and checks condition i<=25 if true print result, and go for increment by i=i+1 and then again skip above to starting program and run as loop counter i=2 again until i<=25 fails and finally terminate our program execution.
Program-->
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;
while(i<=25)
{
printf("%3d",i);
i=i+1;
}
getch();
}
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
3. The Do-While Loop:
The do-while loop is called exit control loop because the condition is tested after executing the body of the loop. Hence,in do while loop,the statement with in the loop are executed at least ones even if the condition is initially false. The basic format of do while loop is:initialize loop counter
do{
statement 1;
statement 2;
..................
statement n;
increment loop counter;
}
while(test condition);
Example: Write a program to print first 25 natural numbers?
Solution:Logic-->Here the working condition is same however syntax is different for each loop. Here also we initialize loop counter, check condition, if true print results, increments the loop counter and finally terminate if condition fails.
Program-->
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;
do
{printf("%3d",i);
i=i+1;
}
while(i<=25);
getch();
}
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Well explained bro...thanks
ReplyDeletethanks for keep in touch varun and compliment
ReplyDelete