While Loop in C Programming
How while loop works in C programming is explained in this story with example.
While Loop in C programming :-
While loop is entry controlled loop, first the condition is checked and then only the code inside the loop is executed.
Syntax :-
//initial value of loop outside the loop
while(/*condtion*/)
{
//code
//increment or decrement statement
}
In while the code will be executed if the condition is true.
- The initial value is the starting value for the while loop.
- The condition inside the brackets of while loop is checked every time and if the condition is true the code inside the while loop is executed else if the condition is false it will come out of loop or break out of the loop.
- The code written inside the while loop is the core logic you want to execute if the condition is true.
- The increment or decrement statement that is written inside the while loop is used to change the value of initial value so that we can check the different value against the condition and execute the while loop accordingly.
Example of while loop :-
Write a C program to print the multiples of 6 in between numbers 1 - 70.
Output should be like this :- 6,12,18,24….,66\
Code Snippet :-
#include<stdio.h>
void main()
{
int x=1;
while(x<=70)
{
if(x%6==0)
{
printf("%d\n",x);
}
x++;//x=x+1
}
}
This code will print number that are multiple of 6 between 1 to 70.
Explanation :-
- We want to print the number that are multiple of 6 in between 1–70, so the starting value or initial value will obviously be 1(‘x=1’).
- The initial value is one and we want the numbers till 70 so the condition inside the circle brackets of while says that if the value ‘x’ is less than or equal to 70 then only the loop should be executed. Because we don’t want multiples of 6 that are greater than 70.
- The if condition inside the loop is used to check the remainder when the value ‘x’ is divided by 6. If the remainder is 0 then the number is multiple by 6 and we want to print the number that we have written inside the if statement.
- Then the last one is increment statement where we increment value of ‘x’ so we can check the different values of ‘x’ from initial value 1 to 70 against the condition and execute the loop.
- We write this increment statement to change the initial value of ‘x’ every time so that we don’t check the same value of ‘x’.
All the numbers that are multiple of 6 between 1 to 70 will be printed using this program.
Exercise of While loop :-
I have given some questions that you need to solve and understand while loop in C programming.
- Write a program to print all the odd numbers in between 1 to 50 using while loop in c
- Output should be like :- 1, 3, 5, 7 ….., 49.
- Hint :- You need to change the condition and write a if statement inside the loop to check weather a number is odd or not.
2. Write a program to print all odd and even numbers from 1 to 50 using while loop.
- In Last example we have already printed odd numbers now you need to print both odd and even numbers, this program would be similar but you just to make some changes.
- Output should be like :- 1 is odd number, 2 is even number, 3 is odd number ………. 50 is even number.
- Hint :- In this program you need to change condition because in this program we want the output till 50th number only and you need to add a if else condition to check the odd or even number.
Youtube video on While loop :-
You can also check out the video of while loop in C programming.
“Life is about learning something new every day, and our path would cross each other in the upcoming new articles”
ADIL WADHWANIA :)