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:
- For Loop
- While Loop
- Do while Loop
- Nested for Loop
- Each Loop statement requires three things:
- Initialization
- Test Condition
- 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:
{
------
body of for loop;
------
}
2. While Loop:
- While Loop is use to per to perform specific task repeatedly.
- Syntax:
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:
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 )
{
------
body part of inner for loop;
}
-------
body part of outer for loop;
}
- In this syntax there are two for loop:
- Inner For Loop
- Outer For Loop
Comments
Post a Comment