fastread homefastrread library fastread menu

C : C Loops

Tutorial by:      Date: 2016-04-27 04:48:08

❰ Previous Next ❱

The loops in C language are used to execute a block of code or a part of the program several times.

In other words, it iterates a code or group of code many times.

Why use loops in C language?

Suppose that you have to print table of 2, then you need to write 10 lines of code.

By using the loop statement, you can do it by 2 or 3 lines of code only.

Advantage of loops in C

1) It saves code.

2) It helps to traverse the elements of array (which is covered in next pages).


Types of C Loops

There are three types of loops in C language that is given below:

  1. do while
  2. while
  3. for

do-while loop in C

It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.

It is better if you have to execute the code at least once.

The syntax of do-while loop in c language is given below:

 
  1. do{  
  2. //code to be executed  
  3. }while(condition);  

Flowchart and Example of do-while loop


while loop in C

Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.

It is better if number of iteration is not known by the user.

The syntax of while loop in c language is given below:

 
  1. while(condition){  
  2. //code to be executed  
  3. }  

Flowchart and Example of while loop


for loop in C

Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.

It is good if number of iteration is known by the user.

The syntax of for loop in c language is given below:

 
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  

❰ Previous Next ❱


C

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website