Looping Statements in C Language

aimtocode

Looping statement

The process of repeatedly executing a statements and is called as looping statement.

The statements may be executed multiple times (from zero to infinite number).

If a loop executing continuous then it is called as Infinite loop.

Looping is also called as iterations. There are three types of looping statement.

Types of Loops:


while loop statement

The while loop is an entry controlled loop statement. The condition is evaluated, if the condition is true then the block of statements or statement block is executed otherwise the block of statement is not executed.

The WHILE Statement is used to carry out looping operations. Hence, the loop is executed until the expression evaluates to TRUE.

The General form of this statements is :

syntax:

Flowchart:

Example:



Output:

              
  1 2 3 4 5
              



do-while loop statement

In do-while loop, first attempt of loop should be executed then it check the condition.

The benefit of do-while loop/statement is that we get entry in loop and then condition will check for very first time.

In while loop, condition will check first and if condition will not satisfied then the loop will not execute any further.

Syntax:

  • Check the test condition.
  • If it is true, execute the body of the loop.
  • If it is false, execute the next statement.

Flowchart:

Example:



Output:

 Value of j variable is: 0
 Value of j variable is: 1
 Value of j variable is: 2
 Value of j variable is: 3

Difference between while and do-while

do-while while
Condition is checked at the end of the loop. Condition is checked in the beginning of the loop.
Set of statements within the loop will be executed atleast once. If the condition is fals, the statements will not be executed even once.


For Loop statement

The for loop statement is one of the most commonly used iteration statement.

for statement is used to execute a statement or a group of statements repeatedly for a number of times.

Syntax:

Flowchart:

Example:



Output:

 1
 2
 3
 4 
 5
aimtocode