Tuesday 1 October 2013

Loops in C

if else                                                                                                                             Switch case


Loops:- if we want to print any statement several  times  we use the concepts of loops.
in loop first we provide the condition after this we check the condition and increment the value.

1) while loop:- in the while loop we provide the condition.

Ex:-
int i=0; // initialization
while(i<10) // condition checking
{
printf("%d",i);
i++;   // increment
}

output:-0 1 2 3 4 5 6 7 8 9 
using one printf statement we prints 10 values it is happened by using loops.


2) for loop:- for loop contains three arguments
  •      initialization
  •      condition checking
  •     increment\decrement
Ex:-
for(int i=0;i<10;i++)
{
printf("%d",i);
}

output:-0 1 2 3 4 5 6 7 8 9


3) do while loop:- this loop executes at least one time  because we give condition after the loop body.


Ex:-
int i=0;   // initialization
do
{
printf("%d",i);
i++        //increment
}while(i<10);  // condition checking

printf statement executes at least  one time because we check the condition at last.
output:- 0 1 2 3 4 5 6 7  8 9 






No comments :

Post a Comment