All About While Loop in C
Have you ever wondered how a while loop works in C programming? It's a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop starts by evaluating the condition. If the condition is true, the code inside the loop is executed, and the process repeats itself until the condition becomes false. Let's understand more!
The while loop allows code to be executed iteratively until a given condition is true. We will learn how to implement while loop in C programs. So, without further ado, let us begin!
While Loop in C
The while loop in C is used to evaluate a test condition and iterate over the loop body until the condition returns True. The loop ends when the condition returns False.
This loop is also known as a pre-tested loop because it is commonly used when the number of iterations is unknown to the user ahead of time.
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
While Loop Syntax in C
Hereโs the syntax for the while loop in C
while (condition) {
// Code to execute as long as the condition is true
}
Here,
- while: This is the keyword that begins the while loop.
- condition: This is a Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the loop continues; if it is false, the loop ends.
- The code inside the curly braces {} is the body of the loop. This code is executed repeatedly as long as the condition remains true.
To begin with, the while loop in C checks the test condition and executes the body of the loop when the condition is True. This means that the condition is evaluated iteratively. The loop is active while the condition is true. This procedure is repeated until the condition returns False. The loop is terminated as soon as this occurs.
Take a look at the while loop flowchart below
While Loop Implementation Example in C
Example 1: Take a look at the C program that calculates the sum of n-natural numbers
#include <stdio.h>
int main() { int num, i, sum = 0;
printf("Enter a positive integer: "); scanf("%d", &num);
i = 1;
while (i <= num) { sum = sum + i; i++; }
printf("Sum: %d", sum);
return 0;}
Output
Enter a positive integer: 5
Sum: 15
Example 2: Write a C program to determine if a number entered by the user is a prime number or not.
#include <stdio.h>
int main() { int num, i = 2; int isPrime = 1; // Flag to determine if num is prime. 1 means prime, 0 means not prime.
// Ask user for input printf("Enter a positive integer to check if it's prime: "); scanf("%d", &num);
// Edge cases for 0, 1, and negative numbers if (num <= 1) { isPrime = 0; }
// Check for prime number using a while loop while (i <= num / 2 && isPrime) { if (num % i == 0) { isPrime = 0; } i++; }
// Output if (isPrime) { printf("%d is a prime number.\n", num); } else { printf("%d is not a prime number.\n", num); }
return 0;}
Output
Enter a positive integer to check if it's prime: 3
3 is a prime number.
Explore free C courses.
What have we done here?
- Two variables, n and sum = 0, have been declared in the aforementioned code. The user enters a value for the variable n. The integer up to which we want to find the sum of natural numbers will be this value.
- Following that, we employ the while loop with the test condition n > 0.
- The body of the loop will get executed if the input integer n is larger than 0.
- With each iteration, the integer n is added to the sum, and then the value of n decreases by 1.
- When n drops below 0, the while loop ends, the condition returns False, and the sum of natural numbers is displayed.
Endnotes
I hope this post helped you grasp on while loop and shed light on how it works. Explore our articles on the C programming language to find out more about it and to reinforce your knowledge of the fundamentals.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio