While Loop in C
Use of while loop, conditions
The while loop represents the simplest loop structure in C programming. A while loop is a control structure that allows you to repeat a task a certain number of times. It is also called the entry controlled loop statement.
Syntax of while loop
- First of all loop condition is checked, if the condition is true then the scope of the loop will run otherwise not.
- The condition of while loop can be a relational expression or any expression which will return true ( any nonzero value) or false.
- After executing the loop scope, the condition will be checked again, and if it is true then the scope will run again, This process will continue until the condition becomes false.
- Keyword while is case-sensitive.
- The while loop scope may not execute a single time if the condition is already false.
Block Diagram of while Loop
See the block diagram of the while loop, later we will learn more with the help of examples.
Initialization & Increment/Decrement
There is no fixed rule for initialization, increment or decrement for the while loop but it is best practice to initialize variables before while loop and perform increment/decrement operation in the scope of while loop. See the below example
Output
Explanation
- Variable i get Initialized by 1, Now the while condition will be checked, the while condition is i<=5
- In first-round i=1 and condition is i<=5, the condition is true, so loop scope will run.
- Loop scope prints the value of i which is 1 and then i=i+1 calculated, so now the new value of i is 2
- Again while condition i<=5 checked, currently the value of i is 2, so the condition is again true, the scope will run. This will continue until the condition becomes false.
- After false condition, the next line after while loop runs, which prints a message.
Example
What is the output of below program...?
Output
Example
Write a program in C using while loop to find out the sum of all numbers from 1 to 100
Output
Example
Write a program to check whether the entered number is palindrome or not, use while loop to solve the problem.
Output
Nested while loop
Just like for loop, C programming also allows the nested form of while loop. Nested while loop means while loop within another while loop.
- C allows multiple levels nested form of while loop.
- In the below, Syntax of nested while loop:
Working
- For one true outer while condition, inner while loop executes for all true conditions.
- Nested while loop executes for all true conditions of the outer while loop.
- If no condition of the outer while loop is true then, inner while loop condition will not execute.
- Nesting of a while loop can be at any level.
- In a single program, there can be many while loop or nested while loop.
- Let's take a few examples to understand the concept of the nested while loop.
Example
Write a program to print the table of number 1 to 5:
Output
Example
Output
Infinite while loop
If the condition of a while loop never becomes false then the while loop never gets terminated, this kind of while loop is called an infinite while loop.
Infinite while Loop Syntax
Example
Output
The above loop is the infinite while loop, this loop will never get terminated. In programming, we rarely use an infinite while loop but in case of requirement, we are free to use an infinite while loop.