Facebook

adsense

Wednesday 15 April 2015

Loops

What is a Loop :

A loop means to cause a section of  program to be repeated a certain number of times.The repetition continues while a condition is true. When a condition becomes false , the loop ends and control passes to the statements following the loop.

Three types of loops:-
     1) For loop
     2) While loop
     3) Do-while loop

let us talk about each loop one by one


1) For loop:

    for( initialization ;  condition ; increment/decrement)
          for                              --> is a keyword
          initialization              --> starting value
          condition                   --> how much time loop execute
increment/decrement --> increase in value





'for' is a also known as counter controlled loop (we know how many time the loop execute before program run)

Example :

        #include<stdio.h>
int main(){
int a;
for( a=0 ;  a<=9 ; a++)
printf(“%d\n”,a);
return 0;
 }


 In this program the
    initial value --> 0
    Condition  a<=9 --> tells that loop will run 10 time   
    Increment/decrement--> a++ =show that there is increment of 1

Actually what happen :
At first run :
Program see the initial value of 'a'(i.e: 0) and check the condition (0<=9) whish is true so execute the statement and then make increment of '1'.

Second run --> last run :
Then again check the condition, know a=1(due to increment in ‘a’) as condition is true(1<=9) so again the loops body execute and increment of 1.and it will going on until the condition is true and when condition fail (i.e: 10<=9) then the loop terminate.




2) While loop :


Known as sentinel controlled loop(we don’t know how many times a loop execute),But it can also be counter controlled we see later how.

Sentinel value is the value, which is used to terminate the loop.

while(condition)
{
statement-1;
statement-2;

}

The “while loop” is like a simplified version of the “for loop”. It contains a test expression(condition) but no initialization or increment expression

Example:
             #include<stdio.h>
              int  main(){
int n = 0;
printf("enter no for square or enter 0 to exit = ");
scanf("%d",&n);
              while(n!=0){
printf("Square of %d = %d\n",n,n*n);
printf("enter no for square or enter 0 to exit = ");
scanf("%d",&n);
}
              return 0;
              }

In this program the ‘0’ is called the sentinel value,so if user enter value other than ‘0’ the condition become true and it will print square of number user enter,and program will continue untill user enter '0' but  when user enter the ‘0’condition become false and the loop terminte.

Actually what happen :

At start the compiler check condition  if condition is false, it will not enter into the loop and start executing the statement next to loop body and if condition is true the compiler enter into the body of loop and execute all the statement within the loop ,after execution of all the statement within the loop it will again check the condition if the condition is true it will again enter into the loop and Start executing statement and if condition is false, it will not enter into the loop and start executing the statement next to loop body.  

Tips :
1) If user enter ‘0’ at start, loop will not execute(i.e;it will not enter into the loop). and program terminate or move to statement next to the loop. 

 2) The statement you write above the condition should also be place at end of loop body as above;


3)      Do-while loop :-

Same as while loop some differences are there.The “do/while” structure tests the loop continuation condition after the loop body is performed. Therefore the loop body will be executed at least once.

        do{
Statement;
}while (condition);

 Example :
         #include<stdio.h>
         int main(){
    int n;
do{
    printf("Please enter number”);                    
                    scanf("%d",&n);
              printf("Number= %d \n" , n);
            printf("Square %d \n", n*n);
}while (n<=10);
return 0;
}


No comments:

Post a Comment