Loops

LOOPS:
A loop means one rotation or one cycle of operation.Loops are used to perform the repititive task .in general there are two types of loops .They are
  • Determinate loops-for loop
  • Indeterminate loops-while loop,do-while loop
Steps:
  1. loops variable intilization
  2. Increment /decrement loop variable 
  3. condition for terminating from loop
Why loop concept?
        ex: print 1 to 100 numbers
  • we need to write 100 printf statements
  • to avoid this we use looping concept
  • single printf statement for all 100 numbers
While loop:(entry level verification)
    verification will be done at entry level.

Syntax:
while (condition)
{
    statement 1;
increment/decrement variable;
}
statement 2;
Note:
Unless the condition become false the statement will be exiceuted as per the loop.

Example:
print 1 to100 natural numbers using while loop
    step 1:loop variable intilisation (int i=100)
    step 2:increment \decrement loop variable (i++)
    step 3:condition for the end (i<=100)

Note:if condition fails at beginning the loop block executes 0 times.

int i=0;
while(i<=100)      →  ( here i=0 it is < 100
                                   so '0' is printed)
{
    printf("%d",i);     →   ( i=1 <100 1 is printed)

i++;                →    ( upto 100 =100 so 100 will be printed)
}

DO WHILE LOOP:
  • verification will be done at exit level
  • atleast 1 block statement will be printed.
  • post level loop
Syntax:
initialisation
do{
    statement;
    updation;
}while(condition)

Example:
i=0
do
{
printf("%d",i);
i++;
}
while(i<=100); 

For loop:
Syntax:
main()
{
    for(initialisation;condition;updation)
{
        statement;

}

Note: semicolon shouldn't used at end of the updation.

Example:
#include<stdio.h>
    main()
{
int i;
for(i=0;i<=100;i++)
{
    printf("%d",&i);
}


   GOTO:

  • In forward jump some set of statements will be ignored.
  • In backward jump some set of statements will be repeated.
  • so it is used as alternate for looping.
Note: At label(:) is used.
Examples:

CONTINUE:

It skips the current interation and further continues.The interation unless the condition become false.
while(condition)                   Do
{                                            {
    printf(statement1);                printf(statement1);
.........                                      .........  
........                                       .........
    if(condition)                            if(condition)
    continue;                            ........
.......                                        }.......
}                                               while(condition)

For (initst;condition;increment)
{
        statement1;
........
if(condition)
    {
        statement2;
        .......
        continue;
}
statement3;
    ,,,,,,
       
Example: 1              Example:2
#include<stdio.h>                       #include<stdio.h>
    int main()                                       int main()
{                                                   {
    int i=0;                                            int i;
for(i<=10;i++)                                for(i=0;i<=10;i++)
{                                                        {
    printf("%d",i)                                    if(i==3)
    if(i==3)                                          { continue;
{                                                          }
break;                                                printf("%d",i)
}                                                        }
}                                                           return 0;
    return 0;                                          }              
Output                        Output
1                                                        1 2 3 4 5 6 7 8 9 10
2
Jump statements:
            Break statement used to terminate at any point of the loop.
 Break in three cases:

                                                                

Comments