Let’s explore different loops in C Programming. The programs we have developed so far in this series of C Programming. These programs use either a sequential or decision control statements to solve task. The real beauty of computer programs lies in its ability to execute block of code repeatedly. This involve repeating some portion of code the program either specified number of times or until particular condition met. These kind of repetitive actions can be performed using loops in C Programming. There are three types of loops in C Language: For loop, While loop, Do-while loop.
Table of Contents
For Loop in C Programming
for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it an open ended loop.
Syntax of For Loop[crayon-5c6abca6be104478785892/] In for loop we have two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. for loop allows only one condition.
Example Program: for loop
In for loop, loop is executed until condition becomes false.
[crayon-5c6abca6be11c740285274/] The output of this program is given below:
While Loop in C Programming
while loop can be addressed as an entry control loop. It is completed in 3 steps.
- Variable Initialization. (e.g. int x=0)
- Condition ( e.g. while(x<=10) )
- Variable Increment or Decrement ( x++ or x– or x=x+2 )
Syntax of while Loop[crayon-5c6abca6be125483066222/] Some basic rule of while loop: The statements within the while loop would keep getting executed till the condition being tested remains true. When the condition becomes false, the control passes to the first statement that follows the body of the while loop. The condition being tested may use relational or logical operators as shown in the following examples: while(x<=20), while(x>=20 && x<=50), while(x>=10 || (i>=10 && i<=20) || y==15).
Almost always, the while must test a condition that will eventually become false, otherwise the loop would be executed forever, indefinitely. It is not necessary that a loop counter must only be an int. It can even be a float.
Example Program: while loop[crayon-5c6abca6be12c362343500/] The output of this program is given below:
Do-While Loop in C Programming
do..while loop in C Programming is useful for things that want to loop at least once. Here is Syntax:
[crayon-5c6abca6be137637172267/] In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement.
Example Program: do..while loop[crayon-5c6abca6be13e857623144/] The output of this program is given below:
In above program, the printf() would be executed once, since first the body of loop is executed and then the condition tested, 5<1 condition false so loop terminate and go to next statement. Always make sure and don’t forget about semicolon at the end of while. Lets have a look at one more example:
[crayon-5c6abca6be145183397376/] Now we know how loops in C Programming being used to perform repetitive task. We will find loops in most of our future lessons. When and which loop among for, while, do-while to be used in program will depend on individuals requirements/choice. In next lesson we will learn about control statements i.e. breaks and continue in C Programming Language.