Loops in C Language| While, Do While, For Loops

Loops in C Language| While, Do While, For Loops with Examples

Loop: A C programming language, a loop is a programming construct that allows you to repeat a block of code multiple times. It is used when you need to execute a statement or a set of statements repeatedly until a certain condition. 


Loops in C Language| While, Do While, For Loops
Loops in C Language| While, Do While, For Loops  

    There are three types of loops in C

    While loop: It is used when you want to repeat a block of code as long as a condition is true.

    For loop: It is used when you know exactly how many times you want to repeat a block of code.

    Do-while loop: It is similar to the while loop, but the block of code is executed at least once, regardless of whether the condition is true or false.

    While Loop in C Programming

    While Loop: To run the body continuously until a required condition is fulfilling is called looping operation.
    when the condition will become false the execution of loop will be stopped.

    C programming language, a while loop is a control structure that allows you to execute a block of code repeatedly as long as a certain condition is true. The basic syntax of a while loop is as follows:

    While Loop Syntax

    while(condition)

    {

     codes;

    }


    While Loop in C Programming Example

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

     int i=1;

    while(i<=10)

    {

    printf("%d ",i);

    i++;

    }

    }


    **********OUTPUT**********

    1 2 3 4 5 6 7 8 9 10


    Do-While Loop in C Programming Examples

    Do While Loop in C Language: To run the body continuously until a required condition is fulfilling is called looping operation. It is used to perform looping operation.
    When the condition will become false the execution of loop will be stopped.

    C programming language, a do-while loop is a control structure that allows you to execute a block of code at least once, and then repeatedly as long as a certain condition is true. The basic syntax of a do-while loop is as follows:


    Do-while Loop Syntax

    while(condition)

    {

     codes;

    }


    Do-While Loop Best Example in C Language

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

     int i=1;

    do

    {

    printf("%d ",i);

    i++;

    }

    while(i<=10);

    }


    **********OUTPUT**********

    1 2 3 4 5 6 7 8 9 10


    For Loop in C Hacker rank Solution

    For Loop in C: To run the body continuously until a required condition is fulfilling is called looping operation. It is used to perform looping operation.
    When the condition will become false the execution of loop will be stopped.

    1. For Loop there are three part initialization,condition and increment/decrement.
    2. initialization part executes only once.
    3. All the three part of for loop are optional.
    C programming language, a for loop is a control structure that allows you to execute a block of code repeatedly for a fixed number of times. The basic syntax of a for loop is as follows:

    For Loop Syntax

    for(initialization;condition; increment/decrement)

    for(int i=0;i<=10;i++)

    {

     codes;

    }


    For Loop Best Example in C Language

    #include<stdio.h>

    #include<conio.h>

    int main()

    {

    for(int i=1;i<=10;i++)

    {

    printf("%d ",i);

    }

    }


    **********OUTPUT**********

    1 2 3 4 5 6 7 8 9 10


    Nested for Loop in C Programming Examples Patterns

    Nested For Loop: A loop inside another loop is called nested loop so one for loop inside another for loop is called nested for loop.
    C programming language, a nested for loop is a loop inside another loop. It allows you to execute a block of code repeatedly for a fixed number of times in a more complex pattern, such as printing a two-dimensional array. The basic syntax of a nested for loop is as follows:

    Nested For Loop Syntax

    for(initialization;condition; increment/decrement)

    for(int i=0;i<=10;i++)

    {

       for(int i=0;i<=10;i++)

    {

     codes;

    }

    codes;

    }


    Nested For Loop Best Example in C Language

    #include <stdio.h>

    #include<conio.h>

    int main() 

    {

        int i, j;

        

        for (i = 0; i < 3; i++) 

         {

            for (j = 0; j < 3; j++) 

             {

                printf("i is:%d, j is:%d\n", i, j);

            }

        }

        

        return 0;

    }


    **********OUTPUT**********

    i is: 0, j is: 0

    i is: 0, j is: 1

    i is: 0, j is: 2

    i is: 1, j is: 0

    i is: 1, j is: 1

    i is: 1, j is: 2

    i is: 2, j is: 0

    i is: 2, j is: 1

    i is: 2, j is: 2


    Post a Comment

    0 Comments