
while Loop in C: Usage and Examples
Table of Content:
- Syntax while loop
- Program
- While Loop with no body
- Another Example of while loop:
- Single Statement Inside while Loop Body
- Example of while loop:
- C Infinite While Loop
- Example 1:
- Example 2:
- Common Error in Java Program
- off-by-one error
- Related Assignment
- Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
Syntax while loop
The while loop is C’s most fundamental loop statement. while loop executes statements repeatedly while the condition is true.
while(expression) { .............. program statement .............. }
Expresion or loop continuation condition
The expression specified inside the parentheses is evaluated. If the result of the expression evaluation is true. The program statement that immediately follows is executed. After execution of this statement (or statements,if enclosed in braces), expression is again evaluated. If the result of the evaluation is true, the program statement is again executed. This process continues until expression finally evaluates false, at which point the loop is terminated. Execution of the program then continues with the statement that follows program statement.
The condition may be any expression, and true is any nonzero value.
program statement or Loop body:
The program statement is either an empty statement, a single statement, or a block of statements.
Braces { }:
The curly braces are unnecessary if only a single statement is being repeated.
Extra touch in syntax
i = initialValue; // Initialize loop control variable while (i < endValue) { // Loop body ... i++; // Adjust loop control variable }
Program
As an example of its use,the following program sets up a while loop,which merely counts from 1 to 5.
// This program introduces the while statement #includevoid main() { int count = 1; // initialization part while(count <= 5 ) { // expresson part or conditional part printf("%i \n",count); ++count; // increment part } }
Output
1 2 3 4 5 Press any key to continue . . .
Explanation
The program initially sets the value of count to 1; execution of the while loop then begins. Because the value of count is less than or equal to 5, the statement that immediately follows is executed. The braces define both the print statement and the statement that increments count as the body of the while loop. From the output of the program, you can see that this loop is executed five times or until the value of count reaches 5 .
While Loop with no body
#includevoid main() { int i, j; i = 10; j = 20; printf("%d\n",i); printf("%d",j); while(i < j); // no body in this loop }
Output
10 20
Another Example of while loop:
#includevoid main() { int n=1; while(n<=10){ printf("%d\n",n); n++; } }
Output
1 2 3 4 5 6 7 8 9 10 Press any key to continue . . .
Single Statement Inside while Loop Body
Single Statement is a part of While Loop as there is no opening and closing curly braces. If we want to write multiple statements as a part of while loop body then we have to put all the statements in a block. i.e the { }
while(true) //code to be executed or statement(single);
Example of while loop:
#includevoid main() { int count = 1; while (count <= 11) printf("Number Count : %d\n",count++); }
Output
Number Count : 1 Number Count : 2 Number Count : 3 Number Count : 4 Number Count : 5 Number Count : 6 Number Count : 7 Number Count : 8 Number Count : 9 Number Count : 10 Number Count : 11 Press any key to continue . . .
Note: Make sure that the loop-continuation-condition eventually becomes false so that the loop will terminate. A common programming error involves infinite loops (i. e., the loop runs forever). If your program takes an unusually long time to run and does not stop, it may have an infinite loop. If you are running the program from the command window, press CTRL+C to stop it.
C Infinite While Loop
If you pass true in the while loop, it will be an infinitive while loop.
while(true){ //code to be executed or statement(s); }
Example 1:
#includevoid main() { while(1==1){ printf("infinite while loop \n"); } }
Output
infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop .................. .................. .................. .................. upto infinite
Example 2:
#includevoid main() { while(22 > 1){ printf("infinite while loop \n"); } }
Output
infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop infinite while loop .................. .................. .................. .................. infinite time it will print like this
Common Error in Java Program
off-by-one error
Programmers often make the mistake of executing a loop one more or less time. This is commonly known as the off-by-one error.Example:
#includevoid main() { int number = 0; while (number <= 100 ) { printf(" Hello Dhinchak Pooja! \t Selfie mayne la liya!! - %d\n",number); number++; } }
Output
Hello Dhinchak Pooja! Selfie mayne la liya!! - 0 Hello Dhinchak Pooja! Selfie mayne la liya!! - 1 Hello Dhinchak Pooja! Selfie mayne la liya!! - 2 Hello Dhinchak Pooja! Selfie mayne la liya!! - 3 Hello Dhinchak Pooja! Selfie mayne la liya!! - 4 Hello Dhinchak Pooja! Selfie mayne la liya!! - 5 Hello Dhinchak Pooja! Selfie mayne la liya!! - 6 Hello Dhinchak Pooja! Selfie mayne la liya!! - 7 Hello Dhinchak Pooja! Selfie mayne la liya!! - 8 Hello Dhinchak Pooja! Selfie mayne la liya!! - 9 Hello Dhinchak Pooja! Selfie mayne la liya!! - 10 Hello Dhinchak Pooja! Selfie mayne la liya!! - 11 Hello Dhinchak Pooja! Selfie mayne la liya!! - 12 Hello Dhinchak Pooja! Selfie mayne la liya!! - 13 Hello Dhinchak Pooja! Selfie mayne la liya!! - 14 Hello Dhinchak Pooja! Selfie mayne la liya!! - 15 Hello Dhinchak Pooja! Selfie mayne la liya!! - 16 Hello Dhinchak Pooja! Selfie mayne la liya!! - 17 Hello Dhinchak Pooja! Selfie mayne la liya!! - 18 Hello Dhinchak Pooja! Selfie mayne la liya!! - 19 Hello Dhinchak Pooja! Selfie mayne la liya!! - 20 Hello Dhinchak Pooja! Selfie mayne la liya!! - 21 Hello Dhinchak Pooja! Selfie mayne la liya!! - 22 Hello Dhinchak Pooja! Selfie mayne la liya!! - 23 Hello Dhinchak Pooja! Selfie mayne la liya!! - 24 Hello Dhinchak Pooja! Selfie mayne la liya!! - 25 Hello Dhinchak Pooja! Selfie mayne la liya!! - 26 Hello Dhinchak Pooja! Selfie mayne la liya!! - 27 Hello Dhinchak Pooja! Selfie mayne la liya!! - 28 Hello Dhinchak Pooja! Selfie mayne la liya!! - 29 Hello Dhinchak Pooja! Selfie mayne la liya!! - 30 Hello Dhinchak Pooja! Selfie mayne la liya!! - 31 Hello Dhinchak Pooja! Selfie mayne la liya!! - 32 Hello Dhinchak Pooja! Selfie mayne la liya!! - 33 Hello Dhinchak Pooja! Selfie mayne la liya!! - 34 Hello Dhinchak Pooja! Selfie mayne la liya!! - 35 Hello Dhinchak Pooja! Selfie mayne la liya!! - 36 Hello Dhinchak Pooja! Selfie mayne la liya!! - 37 Hello Dhinchak Pooja! Selfie mayne la liya!! - 38 Hello Dhinchak Pooja! Selfie mayne la liya!! - 39 Hello Dhinchak Pooja! Selfie mayne la liya!! - 40 Hello Dhinchak Pooja! Selfie mayne la liya!! - 41 Hello Dhinchak Pooja! Selfie mayne la liya!! - 42 Hello Dhinchak Pooja! Selfie mayne la liya!! - 43 Hello Dhinchak Pooja! Selfie mayne la liya!! - 44 Hello Dhinchak Pooja! Selfie mayne la liya!! - 45 Hello Dhinchak Pooja! Selfie mayne la liya!! - 46 Hello Dhinchak Pooja! Selfie mayne la liya!! - 47 Hello Dhinchak Pooja! Selfie mayne la liya!! - 48 Hello Dhinchak Pooja! Selfie mayne la liya!! - 49 Hello Dhinchak Pooja! Selfie mayne la liya!! - 50 Hello Dhinchak Pooja! Selfie mayne la liya!! - 51 Hello Dhinchak Pooja! Selfie mayne la liya!! - 52 Hello Dhinchak Pooja! Selfie mayne la liya!! - 53 Hello Dhinchak Pooja! Selfie mayne la liya!! - 54 Hello Dhinchak Pooja! Selfie mayne la liya!! - 55 Hello Dhinchak Pooja! Selfie mayne la liya!! - 56 Hello Dhinchak Pooja! Selfie mayne la liya!! - 57 Hello Dhinchak Pooja! Selfie mayne la liya!! - 58 Hello Dhinchak Pooja! Selfie mayne la liya!! - 59 Hello Dhinchak Pooja! Selfie mayne la liya!! - 60 Hello Dhinchak Pooja! Selfie mayne la liya!! - 61 Hello Dhinchak Pooja! Selfie mayne la liya!! - 62 Hello Dhinchak Pooja! Selfie mayne la liya!! - 63 Hello Dhinchak Pooja! Selfie mayne la liya!! - 64 Hello Dhinchak Pooja! Selfie mayne la liya!! - 65 Hello Dhinchak Pooja! Selfie mayne la liya!! - 66 Hello Dhinchak Pooja! Selfie mayne la liya!! - 67 Hello Dhinchak Pooja! Selfie mayne la liya!! - 68 Hello Dhinchak Pooja! Selfie mayne la liya!! - 69 Hello Dhinchak Pooja! Selfie mayne la liya!! - 70 Hello Dhinchak Pooja! Selfie mayne la liya!! - 71 Hello Dhinchak Pooja! Selfie mayne la liya!! - 72 Hello Dhinchak Pooja! Selfie mayne la liya!! - 73 Hello Dhinchak Pooja! Selfie mayne la liya!! - 74 Hello Dhinchak Pooja! Selfie mayne la liya!! - 75 Hello Dhinchak Pooja! Selfie mayne la liya!! - 76 Hello Dhinchak Pooja! Selfie mayne la liya!! - 77 Hello Dhinchak Pooja! Selfie mayne la liya!! - 78 Hello Dhinchak Pooja! Selfie mayne la liya!! - 79 Hello Dhinchak Pooja! Selfie mayne la liya!! - 80 Hello Dhinchak Pooja! Selfie mayne la liya!! - 81 Hello Dhinchak Pooja! Selfie mayne la liya!! - 82 Hello Dhinchak Pooja! Selfie mayne la liya!! - 83 Hello Dhinchak Pooja! Selfie mayne la liya!! - 84 Hello Dhinchak Pooja! Selfie mayne la liya!! - 85 Hello Dhinchak Pooja! Selfie mayne la liya!! - 86 Hello Dhinchak Pooja! Selfie mayne la liya!! - 87 Hello Dhinchak Pooja! Selfie mayne la liya!! - 88 Hello Dhinchak Pooja! Selfie mayne la liya!! - 89 Hello Dhinchak Pooja! Selfie mayne la liya!! - 90 Hello Dhinchak Pooja! Selfie mayne la liya!! - 91 Hello Dhinchak Pooja! Selfie mayne la liya!! - 92 Hello Dhinchak Pooja! Selfie mayne la liya!! - 93 Hello Dhinchak Pooja! Selfie mayne la liya!! - 94 Hello Dhinchak Pooja! Selfie mayne la liya!! - 95 Hello Dhinchak Pooja! Selfie mayne la liya!! - 96 Hello Dhinchak Pooja! Selfie mayne la liya!! - 97 Hello Dhinchak Pooja! Selfie mayne la liya!! - 98 Hello Dhinchak Pooja! Selfie mayne la liya!! - 99 Hello Dhinchak Pooja! Selfie mayne la liya!! - 100 Press any key to continue . . .
Explanation:
The following while loop displays Hello Dhinchak Pooja! Selfie mayne la liya!! 101 times rather than 100 times. Please see the out it is started form 0 to 100. i.e 101 times. The error lies in the condition, which
should be number < 100
rather thannumber <= 100
- Assignment 1: Program to reverse a given number
- Assignment 2: while loop in C Programming
- Assignment 3: print 1 to 4 using while loop in C Programming
- Assignment 4: Infinite while loop Example 1
- Assignment 5: Infinite while loop Example 2
- Assignment 6: Infinite while loop Example 3
- Assignment 7: Example of while loop using logical operator
- Assignment 8: Write a program that prints the sum of digits of a number.
- Assignment 9: Nested while Loops in C Programming Language
- Assignment 10: C program to print the multiplication table of 2 from 1 to 10 using while loop.
- Assignment 11: C program to print the multiplication table of 3 from 1 to 10 using while loop.
- Assignment 12: Program to calculate the sum of first n natural numbers using while loop
- Assignment 13: C program to print the number pattern.
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
- Assignment 14: write a program that accepts an integer from a user and determine wheather it is an odd or even number. The program should validate the input and display an appropriate message to the user if the input is of wrong type, in which case the user should re-enter the integer until the input is correct.
- Assignment 15: Write a program that illustrates the run time field width and precision adjustment while using printf( ).
- Assignment 16: Write a program that asks the user to enter some numbers and then find their average using counter-based loop.
- Assignment 17: Write a program to solve the following problem- Euler's number e is used as the base of natural logarithms. It may be approximated using the following formula: where n is sufficiently large. Write a program that approximates e using a loop that terminates when the difference between the two successive values of e is less than 0.0000001.
- Assignment 18: Write a program that reads a list of test scores and calculate their average. An input of -99 for a score denotes end-of-data for the user
- Assignment 19: Write a program that reads a list of test scores and calculate their average. An input of EOF(by pressing CTRL+Z) for a score denotes end-of-data for the user
- Assignment 20: C Program - Write a program to check whether a number is a prime number or not.(Using While loop)
- Assignment 21: Write a program to compute the square root of a given number (without using sqrt() function of the math library).
- Assignment 22: break statement in while loop
- Assignment 23: continue statement in for loop
- Assignment 24: C Program to Check whether the Given Number is a Palindromic
- Assignment 25: Armstrong Numbers Between Two Integers using c program