Looping control statements

In this section I am going to discuss about looping control statements in C.Looping in C is the part of programming code. Looping is used whenever we need to repeat some part of codes for number of times. Suppose you have to print your name 10 times. For printing your name 10 times you can write printf statement 10 times and you will get the desire output. But think if you have to print the numbers between 1 to 100 what will you do, will you write printf statement 100 time. Your answer will be surely NO. Then what to do?.This problem can be solved by using loops.

looping in C are of following type:

  • for loop
  • while loop
  • do while loop

for loop

syntax:

for loop contains three expressions inside it.

Expression1=initialization

Expression2=condition

Expression3=increment/decrement

First of all the value of expression1 is initialised. After the initialisation the condition will be checked,if condition will be true then only the statement(s) written inside loop will be executed otherwise simply it will come out of loop.after executing all the statements the increment/decrement statement will execute and it will continue in similar way until the condition becomes false.

Example:

OUTPUT:

1.Arvind Kumar

2.Arvind Kumar

3.Arvind Kumar

4.Arvind Kumar

5.Arvind Kumar

6.Arvind Kumar

7.Arvind Kumar

8.Arvind Kumar

9.Arvind Kumar

10.Arvind Kumar

for loop checks the condition at entry time hence it is also known as entry control loop.

Declaration of variable inside for loop is prohibited in C if you do so it will give error.

Declaration and initialisation can be done before the for loop.

By using comma operator inside for loop we can use multiple initialisation, conditions and increment/decrement.

OUTPUT:

While loop:

Syntax:

While loop also check the condition at entry time in loop.the statements inside the loop will execute until the expression is not equal to zero.when the value of expression will be zero the loop will terminate. Basically while loop is used where no initialization condition is required inside loop.

Example

OUTPUT:

do while loop:

Syntax:

OUTPUT:

do while loop check the condition at last. First of all the statements inside do while loop executed ones then the condition or expression is checked. If the expression returns 0 the loop immediately terminates otherwise it executes continuously. It is mostly used in menu driven program. Mostly programmers prefer to use while and for loop as comparison to do while loop.
for programs related to loop click here.

 

 

 

Leave a Reply