- 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.
- zero (0) evaluate to false .
- if 0 is used as an expression in while loop , the condition is false and loop does not executed.
- If 0 is used as an expression in do while the condition is false and loop execute at least once.
- 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.
- 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.
- break statement used only with switch statement and loop statement.
- Terminated with semicolon(;) in for loop is infinite loop , and in while loop it is infinite and in do...while loop it is compulsory.
- Dropping {} in for loop is not infinite , in while it is infinite but in do while it is necessary and infinite.
- the frequent variable only used with loop constant.
- use register keyword in frequent used of variable .
for example : register int i;
for(i=0;i<=100;i++)
printf(“%d”,i);
- for loop is pre tested , while is pre tested where as do…while is post tested .for an example: for (;8<2;)>
- there are three important factors are necessary for any type of loop .
a. start value
b. stop value
c. step value
- if we drop the step value it is an infinite loop.
- if the loop counter value is beyond of its range then that loop is infinite loop.
- function recursion is the replacement of loop.