Skip to main content

Loops in C programming.

Loops are very important concept in C language and any other programming language. Loops are important to convert the larger program to smaller ones. Loops are 'the sequence of statement which continues to execute until the desired condition is met.'

There are 3 types of loops in C language :

    1. for loop.
    2. while loop.
    3. do while loop.
    

1. for loop
The syntax of for loop is " for(initialization;expression;increment/decrement)". For loop is also called as entry controlled loop, where we initialize statement, give condition, and update statement in a single line. The for loop statement will be executed until the condition evaluation is true. Whenever the condition fails the control skip the for loop.
Flowchart of for loop.

Simple program to print number from 1 to 10 using 'for loop'.
    #include <stdio.h>
    void main()
    {
    int i;
    for(i=0;i<=10;i++)
    {
    printf("%d\n",i);
    }
    }
    
At first we initialize a variable i=0. Until i is less than or equal to 10 for loop is executed,and it will print the numbers from 1 to 10. When i is greater than 10 the condition fails and control skips the for loop.

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


2. while loop
Syntex of while loop is :
    while(condition)
    {________
    _________.....statement
    update statement;
    } 

while loop is also called as entry controlled loop. The statement under while loop is executed till the condition is true. When the condition fails the control leaves the while loop.
Flowchart of while loop.

Simple program of while loop to print 0 to 10.
    #include <stdio.h>
    void main()
    {
    int i;
    
    while(i<=10)
    {
        printf("%d\n",i);
        i++;
    }
    }
    
At first we initialize a variable i. Until i is less than or equal to 10 while loop is executed,and it will print the numbers from 0 to 10. When i is greater than 10 the condition fails and control skips the loop.
Output :
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    



3. do while loop
Syntex of do while loop is :
    
    do
    {
    statement 
    }
    while(condition);
    

do while is sed to repeat the statement for some number of times. In do loop condition is checked at the end of the loop. Even if the condition is false the statement is executed at least once, then the control skips the loop.
Flowchart of do while loop.

Simple program of do while loop to print 0 to 10.
    #include <stdio.h>
    void main()
    {
    int i=0;
    
    do
    {
    printf("%d\n",i);
    i++;
    } while (i<=10);
    }
    
At first we initialize a variable i=0. The we write do loop to print the value of i after printing it will increment the value by 1 until we give while loop i is less than or equal to 10.
Output :
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

Comments

Popular posts from this blog

Basics of C language

Basics of C language C language is the most commonly used programming language. C language is invened by Dennis Ritchie. It is used in wide range of aaplications. Before C language there ware Assembly level language and Basic compiling programming language(BCPL). Assembly level language code is leanthy as compared to C language code, therefore C language is preffered over BCPL and Assembly level language. Basic Structure of C language Documentation Section Link Section Defination Section Global Declaration Section Main function Section Subprogram Section C program always starts with a include file. SYNTAX : #include filename where filename may be stdio.h, conio.h, math.h, string.h, etc. depending upon the operation to be performed in the program. Most commonly used is stdio.h , which stands for standard input output header file. math.h is used to perform mathematical operations in the program, similarly other...

Arithmetic Operations in C language

Arithmetic operations are addition, subtraction, multiplication ,division and modulus. All these operations can be performed in the C programming using arithmetic operators. Arithmetic operators are ( + , - , * , / ,%).Using these operators we can perform different types of operations. Program To add two numbers. #include <stdio.h> int main() { int a,b,c; printf("Enter the value of a :"); scanf("%d",&a); printf("Enter the value of b :"); scanf("%d",&b); c=a+b; printf("Addition of a and b is %d.",c); return 0; } Output : Enter the value of a :5 Enter the value of b :2 Addition of a and b is 7. In these program the first line "#include <stdio.h>" is the header file which is used for basic input and output in the program."int main()" here ...