The while Loop
In the last tutorial, we learned the significance of loops in programming. I have also introduced you to the three types of loops provided in C programming. Now, let’s learn the while loop in detail.
In the last tutorial, we learned the significance of loops in programming. I have also introduced you to the three types of loops provided in C programming. Now, let’s learn the while loop in detail.
The while loop in C is one of the easiest and the fundamental loop type in C that allows us to repeat a block of code based on the condition it checks. Following is the syntax of the while loop in C:
while (condition) statement;
Let’s understand the syntax properly:
As long as the condition remains true, the statement following the closing parentheses will execute repeatedly.
The syntax allows us to add only one statement, but most often we may want to execute a bunch of statements repeatedly. For this, we can do a trick. We can use what is called a compound statement in C.
A compound statement is a set of statements enclosed within curly braces. In C language, a group of statements enclosed within curly braces are considered as a single statement. So, technically, the above syntax can be replaced by the following:
while (condition)
{
// Bunch of statements to execute repeatedly
}
Now, we can execute a couple of statements repeatedly within the loop body.
There are three things involved in the successful execution of the while loop:
So, in a nutshell
If the process is still unclear, the following flowchart will make it crystal clear:
#include <stdio.h>
int main() {
int counter = 1; // counter variable initialization
while (counter <= 10) { // condition checking
printf("%d\n", counter);
counter++; // incrementing the counter variable
}
return 0;
}
In this example program:
#include <stdio.h>
int main() {
int sum = 0, number;
printf("Enter the number (0 to stop): ");
scanf("%d", &number);
while (number != 0) {
sum += number;
printf("Enter the number (0 to stop): ");
scanf("%d", &number);
}
printf("The sum is %d", sum);
return 0;
}
Output:
Enter the number (0 to stop): 10
Enter the number (0 to stop): 20
Enter the number (0 to stop): 30
Enter the number (0 to stop): 40
Enter the number (0 to stop): 50
Enter the number (0 to stop): 0
The sum is 150
In this example,
First, we will discuss what are some of the most common mistakes people make in the while loop and then we will proceed with some of the best practices which every programmer should follow.
What happens if the loop condition is always true?
The loop will continue indefinitely, creating an infinite loop.
Can the loop body be empty?
Yes, but it is generally not useful. When the loop body is empty, it will continue to run without doing anything. It does not make sense to write such a loop.
Consider the following while loop:
while (n > 0) {
d = n % 10;
s += d;
n /= 10;
}
What would be the final value of the variable s
if n = 1234
? (Assume that variables are declared already and the variable “s” has been initialized to 0)
The while loop calculates the sum of the digits of the number 1234. The while loop works as follows:
1. Initially, n is greater than 0, therefore the loop condition is satisfied and we will enter inside the loop.
2. n % 10 gives the last digit of the number which is 4. This number is now stored in the variable d.
3. Now, d is added to s, so the new value of s is 4.
4. n = n / 10 gives the quotient of the number 1234 which 123. This will be the new value of n.
5. Again, the condition is checked and satisfies.
6. This time d will hold the remainder of the number 123 which is 3 and s will hold the sum of 4 and 3 which is 7.
7. In this way, all the digits of the number 1234 are added and the final value of sum will be 10. Hence, option (a).
Consider the following code snippet:
num = 1;
while (num < 10) {
printf("%d ", num);
num++;
}
Does the above code snippet correctly produce numbers from 1 to 10?
No. This example demonstrates the off-by-one error concept which we have discussed earlier. In place of less than (<) operator in the while condition, we need less than or equal to (<=) operator to correctly produce numbers from 1 to 10.
Determine the output of the following program:
#include <stdio.h>
int main()
{
int x = 0;
while (++x < 5) {
printf("%d", x);
}
}
The pre-increment operator increments x before checking the condition. Initially, when x is 0, x is incremented to 1 and then the condition 1 < 5
is checked. As the condition is true, the printf()
function will execute and print 1. The loop will continue to run until x becomes 5. At this point, printf()
will not be evaluated, and hence the output will be 1234.
Your score is out of
Leave a comment