For loop 


For loop is same as while and do while but there is some differences . In for loop with for keyword inside of parenthesis three sections becomes written separated by semicolon variable with initialization,condition, value updated statement .Let us see syntax of for loop-

for(variable with initialization ; condition ; updating statement)
{
statement 1;
statement 2;
….……………..
statement n;
}
statement x;

In case of for loop initialized value firstly check in condition when true then statements inside of loop body executed . After execution all the statements in the loop body value of variable get updated then value becomes check in condition and again all the statements in loop body executed until the condition comes true . But when condition false control jump out of loop body to execute statement x .

Let us see same example to understand.
Write a program to print first 10 natural number.

#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("%d",i);
}
}

Output :- 12345678910

Here in the example  initialized value check in condition and condition is true then statement in loop body executed . After execution of statement in loop body value of variable get updated from 1 to 2 .  And value check in condition again and comes true so statement in loop executed until the condition is true . But when updated value 11 check in condition becomes false loop end and control jump out of loop body to execute statement x .

Do while loop


Do while loop is same as while loop . Only difference  is condition check at the end of loop. Means statement before the condition in the loop execute at least once even condition is false . Keep in mind  while keyword becomes written with semicolon. When condition is false in the loop control jump out of loop body.
Syntax of do while loop :-

do
{
statement  1;
statement  2;
}
while(condition);
statement x;
All the statement written before the condition executed at least one time even condition is false because while keyword written after the statements.In case do updated statement becomes written before the conditional statement . All the statements before updated statement executes first then updated statement update the value of variable and updated value becomes check in condition.When condition if true then loop get reverse and again statement written in loop body executed .Updated statement update the value and condition true loop get reverse again.But when updated value becomes false during checking condition then loop end and control jump out of body.

Let us understand with example -

Write a c program to print first 10 natural number.

#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d",i);
i++;
}
while(i<=10);
return 0;
}
Output :12345678910
Run this code

Here in the program first statement execute print the value of i after that value get updated through updated statement (i++) .After updated value becomes check in condition comes true and loop gets reverse.Updated value(2) print and again value updated through updating statement after that check condition in this way process goes to continue . Process goes to continue still condition false.When value gets updates 11 then condition becomes false and program end control jump out of loop below the the while keyword.

Notes -
1. ) while keyword need write out of do body.
2. Many statements can be written in do body or loop body as you wish.

By following of above rules try to solve to these questions :

1.) write a program to add 10 natural numbers with help of for loop as well as do while loop.

2.)  #include<stdio.h>
    int main()
    {
    int i=1;
   for(i;i<=5;i++)
    {
    printf("%d",i);
    }
    return 0;
    }

3.) #include<stdio.h>
    int main()
    {
    int i=1;
    for(i;i<=5;i++);
    {
    printf("%d",i);
    }
    return 0;
    }

4.) #include<stdio.h>
    int main()
    {
    int i=1;
    while(i<=5);
    {
    printf("%d",i);
    i++;
     }
     return 0;
     }

5.) #include<stdio.h>
    int main()
    {
    int a=5;
    while(a&&1)
    {
    printf("%d",a);
    a--;
    }
    return 0;
    }

<<Prev                                 Next>>