Loop

Loop is an important part or feature of any programming language .loops are designed to perform a particular task multiple times in order to
perform a particular task multiple times in order to perform a loop we require following three parts.
  1. Assignment operator
  2. Condition operator
  3. Increament/decreament operator



Assignment Operator-:
Assignment is a simple statement where we assign value to a variable. For Example a=5.
Condition-:
It is a conditional statement that checks for continuation or termination of loop for example a<=100.
Increament/decreament-:
These are the operators that increase or decrease the values of the looping variables for example a++.

Note-:Assignment is often called as base value because loop starts from this value and condition is calle peak value because loop teminates here.

Following are the three standard loops use in C language.
  1. The for loop
  2. The while loop
  3. The do while loop


    For Loop

    The for loop is one of the simplest loop this is because all the three parts of a loop are written along side,seperated by semicolan(;).

    Syntax-: For(assignment ;condition;increament/decreament)
    {
    ______________
    ______________
    ______________
    ______________
    }


    A program to print name 5 times.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i;
    for(i=1;i<=5;i++)
    {
    printf("xyz");
    }
    getch();
    }

    Program to print a count from 100 to 1.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i;
    for(i=100;i>=1;i--)
    {
    printf("%d",i);
    }
    getch();
    }


    Program that prints all odd numbers between 20 to 77.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i;
    for(i=20;i<=77;i++)
    {
    if(i%2==1)
    {
    printf("%d",i);
    }
    }
    getch();
    }


    Program to print the table of the entered number.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,n,s;
    for(i=1;i<=10;i++)
    {
    s=n*i;
    printf("%d",s);
    }
    getch();
    }

    Program to print the following series.
    1,3,11,43________n

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,n,s;
    for(i=1;i<=500;i=(i*4-1))
    {
    printf("%d",i);
    }
    getch();
    }

    Program that inputs three numbers and print their squares.


    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,n;
    for(i=1;i<=3;i++)
    {
    printf("enter a number");
    scanf("%d",&n);
    printf("square is%d",n*n);
    }
    getch();
    }

    Program to check and count the number of people below 18 between 18-60 and above 60.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,c1=0,c2=0,c3=0,age;
    for(i=1;i<=5;i++)
    {
    printf("enter the age");
    scanf("%d",&age);
    if(age<18)
    {
    c1=c1+1;
    }
    elseif(age>=18&&age<60)
    {
    c2=c2+1;
    }
    elseif(age>=60)
    {
    c3=c3+1;
    }
    }
    printf("below 18 is%d",c1);
    printf("between 18-60 is%d",c2);
    printf("above 18 is%d",c3);
    getch();
    }

    Program to claculate factorial of a number.
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,num,fact=1;
    printf("enter the number");
    scanf("%d",&num);
    for(i=num;i>=1;i--)
    {
    fact=i*fact;
    } printf("factorial is %d",fact);
    }
    getch();
    }

    An infinite loop


    A loop is called infinite when its condition is unable to break the loop. In C following are the two best methods to make an infinite loop.

    Method 1-:

    for(i=1;i>=1;i++)
    {
    __________________
    __________________
    __________________
    }

    Method 2-:

    for( ; ; )
    {
    ___________
    ___________
    ___________
    }

    Break statement


    The break statement ends the loop pre maturely. We often use it inside of infinite loop.

    Example -:
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i;
    for(i=1;i<=10;i++)
    {
    printf("*");
    if(i==5)
    {
    break;
    }
    }
    getch();
    }

    The continue statement


    The continue statement forces the loop to move up to the next iteration (cycle or loop ) even if therewhere multiple statements to be executed in the last iteration

    Example -:
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i;
    for(i=1;i<=10;i++)
    {
    printf("*");
    if(i==5)
    {
    continue;
    }
    printf("#"); }
    getch();
    }

    Output of the above program-:

    *# *# *# *# * *# *# *# *# *#

    Program to print the following series.
    1234
    1234
    1234

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

    Program to print the following series.
    1111
    2222
    3333

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

    Program to print the following series.
    4321
    4321
    4321

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=1;i<=3;i++)
    {
    for(j=4;j>=1;j--)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    getch();
    }
    Program to print the following series.
    DCBA
    DCBA
    DCBA

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=1;i<=3;i++)
    {
    for(j=68;j>=65;j--)
    {
    printf("%c",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    DDDD
    CCCC
    BBBB

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=68;i>=66;i++)
    {
    for(j=1;j<=4;j++)
    {
    printf("%c",i);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    ABCD
    EFGH
    IJKL

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,n=68;
    for(i=1;i<=3;i++)
    {
    for(j=i;j<=num;j++)
    {
    printf("%d",j);
    }
    n=n+4;
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *
    **
    ***
    ****
    *****
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *****
    ****
    ***
    **
    *

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    1
    12
    123
    1234
    12345

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

    Program to print the following series.
    1
    22
    333
    4444
    55555

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

    Program to print the following series.
    12345
    1234
    123
    12
    1
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=5;i<=1;i++)
    {
    for(j=1;j<=i;j++)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    55555
    4444
    333
    22
    1

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

    Program to print the following series.
    A
    AB
    ABC
    ABCD
    ABCDE

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=65;i<=69;i++)
    {
    for(j=65;j<=i;j++)
    {
    printf("%c",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    ABCDE
    ABCD
    ABC
    AB
    A
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=69;i>=65;i--)
    {
    for(j=65;j<=i;j++)
    {
    printf("%c",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    EDCBA
    EDCB
    EDC
    ED
    E

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=65;i<=69;i++)
    {
    for(j=69;j>=i;j--)
    {
    printf("%c",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    E
    ED
    EDC
    EDCB
    EDCBA
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=69;i>=65;i--)
    {
    for(j=69;j>=i;j--)
    {
    printf("%c",j);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *
    **
    ***
    ****
    *****

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=4;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=sp;j++)
    {
    printf(" ");
    }
    sp--;
    for(k=1,k<=i;k++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *****
    ****
    ***
    **
    *


    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,n=1;
    for(i=1;i<=5;i++)
    {
    for(j=1;j {
    printf(" ");
    }
    n++;
    for(k=5,k>=i;k--)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    EDCBA
    EDCB
    EDC
    ED
    E

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,n=1;
    for(i=65;i<=69;i++)
    {
    for(j=1;j {
    printf(" ");
    }
    n++;
    for(k=69,k>=i;k--)
    {
    printf("%c",k);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    1
    12
    123
    1234


    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=3;
    for(i=1;i<=4;i++)
    {
    for(j=1;j<=sp;j++)
    {
    printf(" ");
    }
    sp--;
    for(k=1;k<=i;k++)
    {
    printf("%d",k);
    }
    printf("\n");
    }
    getch();
    }


    Program to print the following series.
    a
    ab
    abc
    abc
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=3;
    for(i=65;i<=68;i++)
    {
    for(j=1;j<=sp;j++)
    {
    printf(" ");
    }
    sp--;
    for(k=65;k<=i;k++)
    {
    printf("%c",k);
    }
    printf("\n");
    }
    getch();
    }
    Program to print the following series.
    1234
    123
    12
    1


    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=3;
    for(i=4;i>=1;i--)
    {
    for(j=1;j {
    printf(" ");
    }
    sp++;
    for(k=1;k<=i;k++)
    {
    printf("%d",k);
    }
    printf("\n");
    }
    getch();
    }



    Program to print the following series.
    ABCD
    ABC
    AB
    A


    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=1;
    for(i=68;i>=65;i--)
    {
    for(j=1;j {
    printf(" ");
    }
    sp++;
    for(k=1;k<=i;k++)
    {
    printf("%c",k);
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *
    ***
    *****
    *******

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=3;
    for(i=1;i<=7;i=i+2)
    {
    for(j=1;j<=sp;j++)
    {
    printf(" ");
    }
    sp--;
    for(k=1;k<=i;k++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *******
    *****
    ***
    *

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j,sp=1;
    for(i=7;i?>=1;i=i-2)
    {
    for(j=1;j<=sp;j++)
    {
    printf(" ");
    }
    sp++;
    for(k=1;k<=i;k++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    *
    * *
    * * *
    * * * *

    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=1;i<=4;i=i+2)
    {
    for(j=3;j>=i;j--)
    {
    printf(" ");
    }
    for(k=1;k<=i;k++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

    Program to print the following series.
    * * * * *
    * * * *
    * * *
    * *
    *
    #include< stdio.h>
    #include< conio.h>
    void main()
    {
    int i,j;
    for(i=5;i>=1;i--)
    {
    for(j=5;j>i;j--)
    {
    printf(" ");
    }
    for(k=1;k<=i;k++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }



    The while loop

    While is a loop in C language that puts every three part of the loop on different places first the assignment then the condition and finally the increament or decreament operator.


    Syntax-:

    Assignment;
    while(condition)
    {
    ______________
    ______________
    ______________
    incre/decre;
    }

    Example-:

    #include< stdio.h>
    #include< conio.h>

    void main()
    {
    int i=1;
    while(i<=10)
    {
    printf("hello");
    i++;
    }
    getch();
    }




    Nesting of while loop.


    Syntax-:

    Assignment of first loop
    Assignment of second loop
    _________________________________
    | while(condition 1)
    | {
    __________________________________
    while(condition2)
    {
    __________
    __________
    __________
    __________
    increament/decreament
    }
    __________________________________
    _____________
    _____________
    _____________
    increament/decreament
    }



    The do while loop

    A do while loop is very similar to the while loop but with a major difference in do while loop the condition is checcked after the execution of loop body

    Syntax-: Assignment; do
    {
    ____________
    ____________
    ____________
    ____________
    increament/decreament
    while(condition)
    }



Nesting of do while


Assignment1;
do
{
________
assignment 2;
{
_________
_________
_________
increament/decreament;
}while(condition2);
increament/decreament;
}while(condition1);