-->

Learn to code the on-demand coding Languages in an easy way. We help you understand programming concepts and coding Techniques Better.

Wednesday, January 27, 2021

Iterators/Loops in C | for, while and Do-while in C

 Loops in C:


Loop / Iteration: In C and all other programming Languages, Iteration(also called loops) statements allow a set of instructions to be repeatedly executed until a certain condition is reached. 

Types of Loops in C Language are : 

    1. For Loop

    2. While Loop

    3. Do-While Loop

    For Loop: The for loop is used to execute some part of code until the given condition is satisfied. The for loop is used in the case where the number of iterations is known in advance.

    syntax : 

     for (initialization ; condition ; Increment/Decrement)

        {

           // code to be executed

         }

      Initialization: It is an assignment statement that is used to set up a loop control variable.

      condition: The condition is a relational expression that determines when the loop exits.

      incre/decre: Defines the how-to loop control variable changes each time the loop is repeated.

  Flow Diagram : 

                        

    Example: 



    Output:

 

    Explanation: The for loop iterates 5 times. \n is a new line character. To print the message in a new line.

Infinite for Loop: An Infinite Loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an Indefinite Loop or endless loop. It produces continuous output. 

    syntax: for ( ; ; ) 

    Example

    Output:

 

While Loop:  It allows a part of code to be executed multiple times depending upon a given boolean condition. The while loop is used in the case where the number of iterations is not known in advance. The condition is checked at the start of while block.

  syntax: 

        while(condition)

        {

               //code to be executed

        }

   Flow Diagram:

               

    Example:

    Output:



Infinite while Loop: If a non zero value is passed as a condition in the while loop, it will run the loop an infinite number of times.

        syntax: 

                while(value)
                 {
                            //code to be executed
                  }

        Example:
 
     

        Output:

    


Do-While Loop: The Do-while loop is mainly used in the case where we need to execute the loop at least once. The condition in while is checked at the end of the do block.

    syntax:   
                     do
                     {
                            //code to be executed

                      }while(condition) ;
                
     Note: Don't forget to put the semicolon at the end of the while.

     Flow Diagram:
        
                

    Example:



   Output:




Infinite do-while Loop: If a non zero value is passed as a condition in the while loop, it will run the loop an infinite number of times.

       Syntax:   

                        do
                        {
                                //code to be executed
                        }while(value) ;
        
        Example:

   

       Output:

       

    

prev<---control statements                                  next topic---> Jump statements           


No comments:

Post a Comment