How for Loop used in C Programs
The for loop is C’s form of an unconditional loop. The basic syntax of the for statement is:
for (initialization expression; test expr; increment expr) program statement;
Here is an example
sum=10;
for (i=0; i<6; ++i)
sum = sum+i;
The operation for the loop is as follows
The initialization expression is evaluated.
The test expression is evaluated. If it is TRUE, body of for loop is executed, If expression becomes FALSE exit for loop.
Assume test expression is TRUE. Execute the program statements making up the body of the loop.
Evaluate the increment expression and return to step 2.
When test expression is FALSE, exit loop and move on to next line of code.
#include <stdio.h>
int main(void)
{
int i;
for(i= 1; i<=10; ++i)
{
printf("This is for loop programn");
}
}
while Loop
The while loop provides a mechanism for repeating C statements while a condition is true.
Its format is while(control expression) program statement;
The while statement works as follows:
Control expression is evaluated (“entry condition”)
If it is FALSE, skip over the loop.
If it is TRUE, loop body is executed.
Go back to step 1
Example program for while loop
#include<stdio.h>
int void main()
{
int I, factorial;
i=1; factorial=1;
while (i<=n)
{
factorial *= i;
i=i+1;
}
printf(“Factorial is: %dn”, factorial)
}
do while Loop
The do while looping statement is a variant of the while statement in which the condition test is performed at the “bottom” of the loop. This guarantees that the loop is executed at least once. The syntax of the do while statement is:
do
program statement;
while (control expression);
and it works as follows
The body of the loop is executed.
The control expression is evaluated (“exit condition”).
If it is TRUE, go back to step 1. If it is FALSE, exit loop.
A common use of the do while statement print a message or name 10 times. A simple form is shown here
#include <stdio.h>
void main()
{
int n;
n = 1;
do {
printf("n Any message");
}
while (n<=10);
n=n+1;
The for loop is C’s form of an unconditional loop. The basic syntax of the for statement is:
for (initialization expression; test expr; increment expr) program statement;
Here is an example
sum=10;
for (i=0; i<6; ++i)
sum = sum+i;
The operation for the loop is as follows
The initialization expression is evaluated.
The test expression is evaluated. If it is TRUE, body of for loop is executed, If expression becomes FALSE exit for loop.
Assume test expression is TRUE. Execute the program statements making up the body of the loop.
Evaluate the increment expression and return to step 2.
When test expression is FALSE, exit loop and move on to next line of code.
#include <stdio.h>
int main(void)
{
int i;
for(i= 1; i<=10; ++i)
{
printf("This is for loop programn");
}
}
while Loop
The while loop provides a mechanism for repeating C statements while a condition is true.
Its format is while(control expression) program statement;
The while statement works as follows:
Control expression is evaluated (“entry condition”)
If it is FALSE, skip over the loop.
If it is TRUE, loop body is executed.
Go back to step 1
Example program for while loop
#include<stdio.h>
int void main()
{
int I, factorial;
i=1; factorial=1;
while (i<=n)
{
factorial *= i;
i=i+1;
}
printf(“Factorial is: %dn”, factorial)
}
do while Loop
The do while looping statement is a variant of the while statement in which the condition test is performed at the “bottom” of the loop. This guarantees that the loop is executed at least once. The syntax of the do while statement is:
do
program statement;
while (control expression);
and it works as follows
The body of the loop is executed.
The control expression is evaluated (“exit condition”).
If it is TRUE, go back to step 1. If it is FALSE, exit loop.
A common use of the do while statement print a message or name 10 times. A simple form is shown here
#include <stdio.h>
void main()
{
int n;
n = 1;
do {
printf("n Any message");
}
while (n<=10);
n=n+1;
No comments:
Post a Comment