Ads block

Banner 728x90px

C Loops

In the C language, loops play an important role in reducing the complexity of program. It is used in that situation when we have to execute same block of code multiple times untill the specific condition satisfies.

If we don't use the loop then our program become large and difficult to debugging. A big advantage to using loops is that we don't need to write the same block of code again and again. For the purpose of executing same code finite number of times, we use three types of loop.

  • for loop
  • while loop
  • do-while loop

Flowchart


Advantages of Using Loop

  • In loops, the same block of code is executed repeatedly, and repetition occurs when the block of code is finished or a continue statement is encountered.
  • The variables inside the loop is modified automatically using some update statements.
  • It reduces the complexity of the program.
  • Using of loops are easier to understand because the code of loop is shorter and clearer.
  • Loop is faster and require less memory as compare to recursion.

Loop Control Statements

Once the loop is started then it is difficult to control the loop until the specific condition become false. In the C language, there are some loop control statements by which we can control the loop for the specific purpose.

StatementPurpose
break;It is used to stop/break the loop.
continue;It is used to continue the loop.
goto;It is used to transfer the control from one part to another part of the program.


In the upcoming pages, we will learn about the use of these three loops.