Loop Statement in C

tutorial and example
tutorial and example

Loop statement is used to execute one or more statement repeatedly multiple times.

There are three types of loops in C language.

Why to use loop?

We can use loop because it execute a block of code multiple times and it reduce the length of thecode.

Example:  Let suppose we have to print counting 1to 50 then we have to use 50 times lines of code but if we use loop then print it by 2 or 5 lines of code.

  • for loop
  • while loop
  • do while loop

Difference between conditional and looping statement:

Conditional statement executes only once in the program but looping statements executes repeatedly several number of times.

The For Loop

For loop statement is used to execute the code until condition is false. In this section there is 3 parts initialization, condition and increment or Decrements.

 The following syntax of For Loop:

 Example1

Example 2

//To print any childish table of any number

While Loop

While loop is an entry-controlled loop statement. The basic format of the while statement is:

Syntax:

Or

Assignment;

while(condition)

{

Statements;

……

Increment/decrements (++ or –);

}

 

Note: While loop is used when the number is uncertain or unknown.

 Example:

Do while Loop

Do while loop is used to execute the program. In do while loop, the first part of the execution is body then evaluate the text condition.If the condition is true, the body of the loop is executed again and again until the condition become false.

Syntax:

Let us consider the flow chart of C program

Let us consider an example: