Looping in C Programming Language

 Looping in C Programming Language

  • Use: When we want to perform specific task repeatedly then we can use looping.
    • In C Language there are three type of loop:
      1. For Loop
      2. While Loop
      3. Do while Loop
      4. Nested for Loop
    • Each Loop statement requires three things:
      1. Initialization 
      2. Test Condition
      3. Increment/Decrement Operation

1. For Loop:

  • For Loop is a repetition control structure which allow us to write a loop that is executed a specific number of time.
  • Syntax:
            for( initialization ; test condition; increment/decrement operation) 
            {
                ------
                body of for loop;
                ------
            }

2. While Loop:

  • While Loop is use to per to perform specific task repeatedly.
  • Syntax:
            initialization
            while( test condition )
            {
                --------
                body of loop;
                -------
                increment/decrement;
            }
  • In while loop initialization before while loop.
  • After that condition is checked in while statement.
  • while loop will be executed when condition is true.
  • Increment or Decrement is done in body part of while loop.
  • while loop is terminated when condition is false.

3. Do While Loop:

  • Do while loop is use to perform specific task repeatedly.
  • Syntax:
        <initialization>
        do
        {
            -------
            body of loop
            ------
            <increment/decrement>
        } while( test condition );
        --------
        --------
  • do while is a exit control loop because test condition is check after body of loop.
  • do keyword is used at beginning of loop.
  • Initialization is done before loop.
  • Initialization/Decrement is used in do while loop.
  • Initially for the first time body part of do while loop is executed whether the condition is true or false.
  • do while loop is executed until the condition is true.
  • do while loop is terminated when condition is false.

4. Nested For Loop:

  • Nested For Loop is for loop in another for loop.
  • Syntax:
            for( initialization ; test condition ; increment/decrement )
            {
                for( initialization ; test condition ; increment/decrement )
                {
                    ------
                    body part of inner for loop;
                }
                -------
                body part of outer for loop;
            }
  • In this syntax there are two for loop:
    1. Inner For Loop
    2. Outer For Loop

Comments

Popular posts from this blog

Data Types in C Program

Different Modules to solve the program/problem

Operators in C Program