Loop

 LOOPING AND BRANCHING STATEMENTS

Loops are used to execute statement or block of statements more than language, it means to do a particular task again and again till a particular condition is true or satisfied.In ‘C’ we have three types of loops. They are known as:

1.   While
2.    Do while
3.    For
In mostly every loop we have to identify 3 main parts of the loop.a)   Initialization: starting point of the loop.
b)   Condition: ending point of the loop.
c)     Increment/decrement: how will we reach from starting to end point of the loop.
1.   While:- it executes the block of statements till the value of the conditional expression is true. In while if condition is true, then the body of the loop is executed. After execution of the body , the test condition is again tested and if it is true, the body is executed again. Thi process is repeated till the test is true. 

   Program to print series 1 to 10 with while loop in c.


#include<stdio.h>

#include<conio.h>

void main()

{

int a=1;

clrscr();

while(a<=10)

{

printf(“\n%d”,a);

a++;

}

getch();

}





Program to print series 10 to 1 with while loop in c.


#include<stdio.h>

#include<conio.h>

void main()

{

int a=10;

clrscr();

while(a>=1)

{

printf(“\n%d”,a);

a--;

}

getch();

}

Program to print table with while loop in c.


#include<stdio.h>

#include<conio.h>

void main()

{

int a=2;

clrscr();

while(a<=20)

{

printf(“\n%d”,a);

a=a+2;

}

getch();

}

Program to print table any number with while loop in c.


#include<stdio.h>

#include<conio.h>

void main()

{

int a=1,b;

clrscr();

printf("enter any number for table print");
scanf("%d",&b);

while(a<=10)

{

printf(“\n%d”,a*b);

a++;

}

getch();

}






Write a program to print sum of digits of a number.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,s=0;

clrscr();

printf(“enter a no”);

scanf(“%d”,&n);  //let n=1234

while(n>0)

{

i=n%10;

s=s+i;

n=n/10;

}

printf(“sum of digits=%d”,s);

getch();

}

 




 

 

 

Write a program to print reverse of a number.

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,s=0;

clrscr();

printf(“enter a no”);

scanf(“%d”,&n);

while(n>0)

{

i=n%10;

s=s*10+i;

n=n/10;

}

printf(“reverse of a number=%d”,s);

getch();

}






Nested while loop program in c

One while loop inside another  while loop,  its called nested while loop.

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i=1,j=1;
                clrscr();
                while(i<=5)
                {
                        j=1;
                        while (j<=5)
                        {
                                printf("%d",j);
                                j++;
                        }
                       printf("\n");
                       i++;
                }
                getch();
        }
        Output:=
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5



Write a program print rectangle using with nested while loop in c language


#include<stdio.h>

#include<conio.h>

void main()

{

       int i=1,j=1,k=2,n=1,m=1;

       clrscr();

       while(i<=3)

       {

              j=1;

              while(j<=k)

              {

                     printf(" ");

                     j++;

              }

              m=1;

              while(m<=n)

              {

                     printf("*");

                     m++;

              }

              printf("\n");

              k--;

              n=n+2;

              i++;

       }

       i=3,k=0,n=3,j=0,m=1;

       while(i>1)

       {

              j=0;

              while(j<=k)      

              {

                     printf(" ");

                     j++;

              }

              m=1;   

              while(m<=n)

              {

                     printf("*");

                     m++;

              }

              printf("\n");

              n=n-2;

              k++;     

              i--;

       }

       getch();

}










Write a program to find sum of 1 to 10 numbers in c

#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,s=0;
clrscr();
while(a<=10)
{
s=s+a;
a++;
}
printf(“%d”,s);
getch();
}





Write a program using while loop print 2,4,8,16…..



#include<stdio.h>
#include<conio.h>
void main()
{
int a=2;
clrscr();
while(a<=100)
{
printf(“%d”,a);
a=a*2;
}
getch();
}



DO-WHILE LOOP :-

It checks the condition after execution the statements so it executes the statements at least once even if the expression is false. Condition is checked at end of the do while loop. Mostly this loop is used for software testing (means whether the condition is true or false, loop will be execute once).
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,num;
clrscr();
printf(“enter number”);
scanf(“%d”,&num);
do
{
printf(“hello”);
i++;
}while(i<=num);
getch();
}
                                                



Nested do-while loop in c  program

One do-while inside in other do-while,  it is called nested do-while

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i=1,j=1;
                clrscr();
                do
                {
                        j=1;
                        do
                        {
                                printf("%d",j);
                                j++;
                        }
                        while(j<=5);
                        i++;
                        printf("\n");
                } while(i<=5);
             
                getch();
        }
        Output:=
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5
                               1 2 3 4 5






Difference between do-while loop and while loop in c

In both case the condition is checked. In case of while, condition is checked before the execution of statements are executed at least once whether the condition is true or false i.e. condition is checked  at the end of the do-while block.
a=11;
while(a<=10)
{
printf(“%d”,a);  
a++;
}

Output:- [ BLANK SCREEN]

a=11;
do
{
printf(“%d”,a);
a++;
}while(a<=10);


Output :- 11


FOR LOOP IN C

In for loop the initialization , condition  and increment or decrement  can be done in one line . It executed the statement till the condition is true. Mostly this loop is used in complex programming.
Syntax
for(initialization;condition;increment/decrement)
{
Statement;
Statement;
}
First initialization the variable
After initialization , condition is checked if condition is true then statement is executed.
After the increment /decrement the variable according to condition

Simple example of for loop in c program:-


#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
for(a=1;a<=10;a++)
{
printf("%d",a);
}
getch();

}
Output:-
1 2 3 4 5 6 7 8 9 10


Write a program to find sum 1 to 10 numbers using for loop in c

#include<stdio.h>
#include<conio.h>
void main()
{
int a,s=0;
clrscr();
for(a=1;a<=10;a++)
{
s=s+a;
}
printf(“%d”,s);
getch();
}



NESTED FOR LOOP IN C:

Nested loop means nesting or loops means within loop.In nested loop the first loop is called outer loop & second loop is called inner loop. In this the condition of outer loopis true then the condition of inner loop is checked.If it is true then inner loop will execute. The inner loop would act as a part of the body for the outer loop.Therefore,execution of inner loop will depend on the execution of the outer loop.

Write a program to print:-

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i,j;
                clrscr();
                for(i=1;i<=5;i++)
                {
                        for(j=1;j<=5;j++)
                               {
                                        printf("%d",j);
                               }
                        printf("\n");
                }
                getch();
        }
        Output:                       12345
                       12345
                       12345
                       12345
                       12345

Write a program to print:-

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i,j;
                clrscr();
                for(i=1;i<=5;i++)
                {
                        for(j=1;j<=i;j++)
                               {
                                        printf("%d",j);
                               }
                        printf("\n");
                }
                getch();
        }
        Output:       1
                       12
                       123
                       1234
                       12345

Write a program nested for loop in c:-

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i,j;
                clrscr();
                for(i=1;i<=5;i++)
                {
                        for(j=1;j<=i;j++)
                               {
                                        printf("%d",i);
                               }
                        printf("\n");
                }
                getch();
        }
        Output:        1
                       22
                       333
                       4444
                       55555

Write a program to print:-

        #include< stdio.h >
        #include< conio.h >
        void main()
        {
                int i,j;
                clrscr();
                for(i=5;i>=1;i--)
                {
                        for(j=1;j<=i;j++)
                               {
                                        printf("%d",j);
                               }
                        printf("\n");
                }
                getch();
        }
        Output:       12345
                       1234
                       123
                       12
                       1





       

Branching statements in c

       

There are another kind of control statements that can change the execution of loop or execution of the program.Branching statements are further divided into three types.
       a. Break
       b. countinue
       c. goto

       

Break:-


Break statement is used to nexit from the loop or switch statement.Whenever break statement is encountered the rest of the statements inside the loop are ignored and the control go to the next statements after the loop.It can also be used with a while, a do while, a for or a switch statement.

       

EXAMPLE of BREAK STATEMENT IN C
       #include< stdio.h >
       #include< conio.h >
       void main()
       {
           int i;
           clrscr();
           for(i=1; i<=10;i++)
           {
               if(i==5)
               {
                   break;
               }
               printf(“\n%d”,i);
           }
       getch();
       }
       Output:- 1,2,3,4



       

       

Countinue in c:-

The countinue statement is used to skip the remaining statement of the body of the loop where it defined, it dose not terinate the loop but the control is transferred to the start of the next loop iteration. Continue statement is used to only break the current iteration.After continue statement the control returns to the top of loop test conditions.
       

EXAMPLE of continue STATEMENT
       #include< stdio.h >
       #include< conio.h >
       void main()
       {
           int i;
           clrscr();
           for(i=1;i<=10;i++)
           {
               if(i==5)
               {
                   continue;
               }
               printf(“%d”,i);
           }
           getch();
       }
       Output:-1 2 3 4 6 7 8 9 10

       

       

Goto in c:-

The goto statement is control statement that cause the control to jump toa different location in the program with or without checking any condition.It is always used with the label.It increase the readable complexity of the program so programmers avoid to use this. The goto statement can only be used within the body of a function definition.
       

There are two types of goto:-
       1) Unconditional jump
       2) Conditional jump
       
1.UNCONDITION JUMP:-
In this control is transferred from one statement to another without any condition.
       
EXAMPLE of UNCONDITION GOTO STATEMENT
       #include< stdio.h >
       #include< conio.h >
       void main()
       {
           clrscr();
           printf(“\none”);
           printf(“\nTwo);
           printf (“\nThree”);
           goto abc;
           printf ("\Four”);
           printf(“\nFive”);
           printf(“\nsix”);
           abc:
           printf(“\nSeven”);
           printf(“\nEight”);
           getch();
       }
        Output:-
              one
              Two
              Three
              Seven
              Eight
       
2. CONDITION JUMP:-
in this control is transferred from one statement to another according to the condition.
       
EXAMPLE OF CONDITION GOTO STATMENT

       #include< stdio.h >
       #include< conio.h >
       void main()
       {
           int a;
           abc:
           printf(“Enter no greater than 10”);
           scanf(“%d”,&a);
           if(a<=10)
               goto abc;
           printf(“you enter number greater than 10”);
           getch();
       }
       In this program control gose back to Abc label until user not enter number greater than 10.
















1 comments: