You can learn about the C program while loop with the help of a few examples. C while loop is very similar to for loop but runs until a given condition evaluates to true. If the program evaluates to false the while loop breaks. The C program while loop is easy to implement and saves time with low chances of errors.
With C loop you can make your codes readable and are hence more preferred over other loops in C programming. In this article, let us understand the workings of the C program while looping and also clear our concepts with the help of programs and examples connected to the C program.
C Program While Loop Syntax
While loops in C programs are important functions used frequently to solve real world problems using C programming. We can easily implement this loop if we are familiar with C programming and loops used in C programming.
while (condition) {
// Code segment to be executed using while Loop } |
These C programs run loops as long as the specified condition is true. In this example, the code in the given loop will run repeatedly as long as the condition evaluates to true.
int a = 0;
while (a < 10 ) { printf (“%d/\n”, a); i++; } |
The given C program while loop will run until the value of a is less than 10 and will come out as long as the condition evaluates to false and becomes greater than 10.
Different Types of C Program While Loop
There are many different versions of while Loop in C programming. However, all these while loops run as long as the condition evaluates to true.
1. Basic While Loop In C Program
These loops are the simplest form of a while loop and execute as long as the condition evaluates to true.
while (condition) {
// Code to execute } |
You can use this while loop-in implementation anywhere you want. For example, if you want to print numbers ranging from 1 to 5 you can use this loop where the condition can be less than or equal to 5 for running a C program while loop.
Also, Check The Basic Programming language Interview Questions for Beginners
2. While Loop (Infinite Condition)
These while loop conditions are given in such a way that the loop never stops until intervened manually by changing the condition or using a break statement in the program.
#include <stdio.h>
int main() { while (1) { //Code to be executed } return 0; } |
The above condition in the loop always evaluates to true and hence the loop runs to infinity without breaking until conditions remain the same.
3. While Loop (Break Statements)
In these loops, the condition does not matter and the loop breaks immediately as soon as the break statement is executed in the program, regardless of the condition used.
#include <stdio.h>
int main() { int i = 1; while (i <= 5) { if (i == 3) { break; } printf(“%d\n”, i); i++; } return 0; } |
We are using the infinite condition for our C program while loop but still the loop will exit as soon as the program value comes to 3 and the break statement gets executed.
While Loop (Continue Statement)
With the continue statement in the C program while loop current iteration is ignored and the program runs normally for the entire condition given for the loop.
#include <stdio.h>
int main() { int i = 1; while (i <= 5) { if (i == 3) { i++; continue; } printf(“%d\n”, i); i++; } return 0; } |
Here, the loop skips the iteration value 3 continues ahead with the infinite loop and prints other statements easily without exiting. The iteration will continue as long as the value of the loop is not 3 in the condition given the condition is set to infinity.
4. Nested While Loop (Nesting)
A nested while loop in C program means a while loop inside another while loop and it goes on until a given number of times. For example, if you want to print a pattern, you have to use a nested loop.
#include <stdio.h>
int main() { int i = 1, j; while (i <= 3) { j = 1; while (j <= 3) { printf(“%d%d “, i, j); j++; } printf(“\n”); i++; } return 0; } |
Do While Loop In C Programming
The Do While Loop in the C program shares a greater similarity with the traditional while loops. The program inside the do while loop is executed at least once before exiting for every block of code. The given expression is evaluated after testing the first condition in the Do While loop. Check the syntax of the C program do while loop below.
do {
//Code to be executed } while (expression/condition) |
Example of C Program Do While Loop
We are using this do while loop to print numbers from 1 to 5.
#include <stdio.h>
int main() { int i = 1; do { printf(“%d\n”, i); i++; // Increment i } while (i <= 5); return 0; } |
C Program While Loop Examples (Complete Code)
Let us understand the C program While loop implementation using a few other examples.
Write a C program to calculate the sum of the first N Natural numbers using the C program while loop where users will specify the numbers to be used.
#include <stdio.h>
int main() { int n, i = 1, sum = 0; printf(“Enter a positive number: “); scanf(“%d”, &n); while (i <= n) { sum += i; i++; } printf(“Sum of first %d natural numbers is: %d\n”, n, sum); return 0; } |
The given number input by the users will run the while loop until the calculation completes and we get the sum stored in a variable. You can then print the sum of the first n natural numbers in the output.
Write a program to reverse a given number using the C program while loop
#include <stdio.h>
int main() { int num, reversed = 0, remainder; printf(“Enter a number: “); scanf(“%d”, &num); while (num != 0) { remainder = num % 10; reversed = reversed * 10 + remainder; num /= 10; } printf(“Reversed number: %d\n”, reversed); return 0; } |
The number can be reversed using the statements with modulus operators and simplification and the while loop will run until the value equals 0 after repeated division.
Write a program to evaluate the factorial of a number using the while loop and input given by the user.
#include <stdio.h>
int main() { int n, fact = 1, i = 1; printf(“Enter a positive number: “); scanf(“%d”, &n); while (i <= n) { fact *= i; i++; } printf(“Factorial of %d is: %d\n”, n, fact); return 0; } |
We can use the while loop to calculate the factorial of a number and then display the output on the screen.
Learn DSA With C++ Program with PW Skills
Prepare for interviews with C programming and DSA with Decode DSA With C++ Course. Master C++ programming with practice exercises, module assignments, real world projects, and certification exams.
Solve real world examples with C++ programs and gain knowledge of programming with C++ with interactive classes and in depth live sessions with dedicated mentors at PW Skills.
C Program While Loop FAQs
Q1. What is the C program While Loop?
Ans: The C program while loop allows a program to run repeatedly till the condition is satisfied. The while loop is executed repeatedly to use the while loop in C programming.
Q2. What are various types of C programs while looping?
Ans: We can use a while loop in a C program with break statements, continue statements, infinity statements, do while loops, nested loops, input with users and more.
Q3. How does the C program while loop work?
Ans: C program while loop starts with checking the condition statement and then the block of statement is executed till the conditions are met. The while loop breaks out after the condition fails and then you can present the output on the screen.
Q4. What happens when the condition in a while loop is always true?
Ans: The loop takes the form of an infinite loop when the condition always evaluates to true. It means the while loop will keep executing the same block of programs forever until intervened by an external condition.