Wednesday, September 2, 2009

Quick Tips for Loop Statement in C programming

  1. there are three types of loop such as a) for loop b) while loop and c)do-while loop the purpose of each loop are same ( to repeat some part of the program) . but why there are three type of  loop:


a)      for loop : if the loop will continue finite number of times.
b)      While loop: if the loop will continue unknown number of times .
c)      Do..while loop: if the loop will continue at least one.

  1. zero (0) evaluate to false .

  1. if 0 is used as an expression in while loop , the condition is false and loop does not executed.
  2. If 0 is used as an expression in do while the condition is false and loop execute at least once.
  3. If 0 is used as an expression in for loop the condition is false and loop is execute at least once and it is equivalent to do .. while loop.

  1. if we drop all part of the for loop it is infinite loop , and drop all parts of while  and do …while loop is an error.
  2. break statement used only with switch statement and loop statement.
  3. Terminated with semicolon(;) in for loop is infinite loop , and in while loop it is infinite and in do...while loop it is compulsory.
  4. Dropping {} in for loop is not infinite , in while it is infinite but in do while it is necessary and infinite.
  5. the frequent variable only used with loop constant.
  6. use register keyword in frequent used of variable .
     for example : register int i;
                              for(i=0;i<=100;i++)

                                                printf(“%d”,i);
  1.  for loop is pre tested , while is pre tested where as do…while is post tested .for an example: for (;8<2;)>
  2. there are three important factors are necessary for any type of loop .

a.       start value
b.      stop value
c.       step value

  1. if we drop the step value it is an infinite loop.
  2. if the loop counter value is beyond of its range then that loop is infinite loop.
  3. function recursion is the replacement of loop.  

No comments:

Blog List