While Loop in C

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

While loop syntax in C

while(TestExp)
 {
     Statements;
 }

The TestExp is evaluated, if it is true then the statements of the while loop are executed. Again, TestExp is evaluated and if it is true then the statements of the while loop are again executed. The statements of while loop executed repeatedly until the TestExp is true. When the TestExp becomes false then the execution of the while loop terminated, and the remaining statements after the while loop are executed.

The statements of while loop may be a single statement or multiple statements terminated by a semicolon.

To use the while statement, the test expression should contain a loop control variable. The initialization of the loop control variable has to be done before the loop starts and updating must be included in the body of the loop.

The complete syntax of while is given below,

 initialization expression;
 while(TestExp)
 {
   statements;
   update_expression;
 }

Sample program to demonstrate the while loop

 #include<stdio.h>
 int main()
 {
   int n=1; //declaration and initialization
   printf("While loop in C programming\n");
   
   while(n<5) //condition
   {
     printf("Loop executed %d time\n", n);
     n++; //update expression
   }
   
   printf("Control came out from while loop");
   return 0;
 }

Output:-

While loop in C programming
Loop executed 1 time
Loop executed 2 time
Loop executed 3 time
Loop executed 4 time
Control came out from while loop

The variable n initialized with value 1, and then printf statement executed and displayed the message “While loop in C programming” to the screen.

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Now, while loop execution started. The value of the variable n is 1 so n<5 hence condition becomes true, and statements inside while are executed. The value of the variable n is incremented and now the value of the variable n is 2. It was the first iteration.

Again, test expression is evaluated. This time the value of variable n=2 and it is less than 5 so again condition becomes true. Statements of while loop executed and n value is incremented to 3. It was the 2nd iteration.

For n=3 and n=4 test expression becomes true and statements of while loop executed. In the fourth iteration, the value of the variable n is incremented to 5. Now, in the next iteration n=5 and n<5 so the condition becomes false and control came out from the while loop.

Now, the remaining statements after the while loop are executed.

Flowchart for while loop in C

Flowchart for while loop

While true in C

while(1) or while(non-zero integer value)
 {
     statements;
 }

In C language, all non-zero value is true. If the condition of the while loop is true then the loop executed infinite times.

Generally, we do not use the while true. It executes infinite times so control can’t come out from the while loop. The usages of the CPU increases and it also blocks the code. The stop infinite times of execution we need to close the program manually.

 #include<stdio.h>
 int main()
 {
   while(1)
   {
     printf("I love programming\n");
   }
   return 0;
 }

The above program executes infinite times. In this program, the test expression is 1, and it is always true. Hence the statements inside the while loop executed infinite times.

To stop the execution of infinite times of the above program, we can use the break statement inside the loop.

 #include<stdio.h>
 int main()
 {
   int i=1;
   while(1)
   {
     printf("I love programming\n");
     i++;
     if(i==5) break;
   }
   return 0;
 }

Output:-

I love programming
I love programming
I love programming
I love programming

While false in C

It is the opposite of the while true in C. The while true executes infinite times but while false execute zero times. The condition of while false is always false so it will never be executed.

 #include<stdio.h>
 int main()
 {
   while(0)
   {
     printf("I love programming\n");
   }
   printf("While loop not executed.");
   return 0;
 }

Output:-

While loop not executed.

While loop example in C

Based on the above points, now we can develop small programs using while loop in C programming language.

C program to print N natural numbers using while loop

 #include<stdio.h>
 int main()
 {
   int n, i=1;

   printf("Enter n value: ");
   scanf("%d",&n);

   while(i<=n)
   {
     printf("%d\t",i);
     i++;
   }

   return 0;
 }

Output:-

Enter n value: 10
1 2 3 4 5 6 7 8 9 10

Calculate the power of a number using while loop

 #include<stdio.h>
 int main()
 {
   int base, exponent;
   long result=1;

   printf("Enter base and exponent: ");
   scanf("%d %d", &base, &exponent);

   while(exponent!=0)
   {
     result = result * base;
     exponent--;
   }

   printf("Result = %ld",result);

   return 0;
 }

Output:-

Enter base and exponent: 4 3
Result = 64

Sum and the average value in a range

 #include<stdio.h>
 int main()
 {
   int m, n;
   long sum=0;
   float avg;

   printf("Enter range values: ");
   scanf("%d %d", &m, &n);

   while(m<=n)
   {
     sum = sum + m;
     m++;
   }

   avg = sum/(m-n+1);

   printf("Sum = %ld \n",sum);
   printf("Average = %.2f",avg);

   return 0;
 }

Output:-

Enter range values: 1 10
Sum = 55
Average = 27.00

Nested while loop in C

While loop inside another while loop is called nested while loop. The below program shows the demonstration of the nested while loop.

 #include<stdio.h>
 int main()
 {
   int j, i=1;

   while(i<5)
   {
     printf("%d iteration:\t",i);
     j = 0;

     while(j<=i)
     {
       printf("%d ",j);
       j++;
     }

     printf("\n");
     i++;
   }

   return 0;
 }

Output:-

1 iteration: 0 1
2 iteration: 0 1 2
3 iteration: 0 1 2 3
4 iteration: 0 1 2 3 4

C program to reverse of N numbers using while loop

Program description:- Write a program to find the reverse of a number using the while loop. Users can enter more than one number.

Sample Input and Output for this program,

How many numbers you want to enter: 2
Enter number: 5621
The reverse of 5621 is 1265
Enter number: 892
The reverse of 892 is 298

 #include<stdio.h>
 int main()
 {
   int n, num;
   
   printf("How many numbers you want to enter: ");
   scanf("%d",&n);
   
   while(n>0)
   {
     printf("Enter number: ");
     scanf("%d",&num);
     int temp = num;
     int rem, reverse=0;
     
     while(num!=0)
     {
       rem = num%10;
       reverse = reverse * 10 + rem;
       num = num/10;
     }
     
     printf("The reverse of %d is %d\n",temp,reverse);
     
     n--;
   }

   return 0;
 }

Output:-

How many numbers you want to enter: 3
Enter number: 5648
The reverse of 5648 is 8465
Enter number: 25
The reverse of 25 is 52
Enter number: 0035
The reverse of 35 is 53

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Recommended Posts:

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers
Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *