C While/do-while Loops
In this tutorial, we will learn about the C loops, while/do-while loop, nested while/do-while loop and infinite while/do-while loop and its working with the help of some examples.
C Loops
In computer programming, loops are used to repeat a block of code. Loops in Programming come into use when we need to repeatedly a block of statements.
For example, let's say we want to show a message 100 times. Then instead of writing the print statement 100 times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by making effective use of loops.
In programming, a loop is a sequence of instructions that is repeated until a certain condition is reached.
Types of Loops
There are 3 types of loops in C++:
1) for
loop
2) while
loop
3) do...while
loop
In privious tutorial we discuss about C For
Loop & In this tutorial focuses on C while
loop & do-while
loop.
while Loop
- while Loop is a Entry Controlled Loop or we can say repetition control structure.
- The while loop is used in situations where we do not know the exact number of iterations of loop beforehand.
- The loop execution is terminated on the basic of the test condition.
Syntax :
while(condition)
{
// statements inside the body of the loop
}
while loop Flowchart
How while loop works?
- A while loop evaluates the condition.
- If the condition evaluates to true, the code inside the while loop is executed.
- The condition is evaluated again.
- This process continues until the condition is false.
- When the condition evaluates to false, the loop terminates.
To learn more about test expression (when the test expression is evaluated to true and false), check out relational and logical operators.
Example : while loop
// Program to print a statement 3 times
#include <stdio.h>
int main() {
int i = 1;
while (i <= 3)
{
printf("Hello world \n");
i++;
}
return 0;
}
Output
Hello World
Hello world
Hello world
Working of while loop in above example :
Iteration | Variable | condition | Action |
---|---|---|---|
1st | i = 1 |
true |
Hello World is printed and i is increased to 2 . |
2nd | i = 2 |
true |
Hello World is printed 2nd time and i is increased to
3 . |
3rd | i = 3 |
true |
Hello World is printed 3rd time and i is increased to
4 |
4th | i = 4 |
false |
The loop is terminated |
Nested while Loop
A while loop within another while loop is called Nested while loop.
Syntax :
while (condition)
{
while (condition)
{
// body of inner while-loop
}
// body of outer while-loop
}
Example: Nested while loop
// Program to display a triangular pattern
#include<stdio.h>
int main() {
int rows, i = 1;
printf("Enter the number of rows : ");
scanf("%d", &rows);
while (i <= rows) {
int j = 1;
while(j <= i) {
printf("* ");
j++;
}
printf("\n");
i++;
}
return 0;
}
Output
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
In this program, the outer loop iterates from 1
to rows.
The inner loop iterates from 1
to i. Inside the inner loop, we print the character '*'
.
- Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.
- If you don't understand above example, We recommended you to know little more about Nested Loop.
do...while Loop
- Like while loop the do-while loop execution is also terminated on the basic of test condition.
- The do-while loop is exit-controlled loop.
- In do-while loop the loop body will execute at least once irrespective of test condition.
Syntax :
do {
// body of loop;
}
while (condition);
while loop Flowchart
How do-while loop works?
- The body of the loop is executed at first. Then the
condition
is evaluated. - If the
condition
evaluates totrue
, the body of the loop inside thedo
statement is executed again. - The
condition
is evaluated once again. - If the
condition
evaluates totrue
, the body of the loop inside thedo
statement is executed again. - This process continues until the
condition
evaluates tofalse
. Then the loop terminates.
Example : Do-while Loop
// Program to print numbers from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ",i);
++i;
}
while (i <= 5);
return 0;
}
Output
1 2 3 4 5
Working of while loop in above example :
Iteration | Variable | Condition | Action |
---|---|---|---|
i = 1 |
not checked | 1 is printed and i is increased to 2 |
|
1st | i = 2 |
true |
2 is printed and i is increased to 3 |
2nd | i = 3 |
true |
3 is printed and i is increased to 4 |
3rd | i = 4 |
true |
4 is printed and i is increased to 5 |
4th | i = 5 |
true |
5 is printed and i is increased to 6 |
5th | i = 6 |
false |
The loop is terminated |
Nested do-while Loop
A do-while loop within another do-while loop is called Nested do-while loop.
Syntax :
do {
do {
// body of inner do-while-loop
}while (condition);
// body of outer do-while-loop
}while (condition);
Example:
// C program to display a triangular pattern
#include <stdio.h>
int main() {
int rows, i,j;
i = 1;
printf("Enter the number of rows : ");
scanf("%d", &rows);
do {
j = 1;
do{
printf("* ");
j++;
} while(j <= i);
printf("\n");
i++;
} while(i <= rows);
return 0;
}
Output
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
Note : To know the difference of for loop and while loop, Click Here.
To know the difference of while and do-while loop, Click Here.
Loop Control Statements
These control statements change the execution of the loop from its normal execution. The loop control structures are –
1. break statement – It is used to end the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
2. continue statement – It skip some statements according to the given condition.
3. goto statement – It transfer control to the labeled statement.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of while/do-while Loop in C.
Keep Learning : )
In the next tutorial, you'll learn about C break & Continue
Statement.