The while loop in c is an entry controlled loop (Entry Controlled Loops are used when checking of a test condition is mandatory before executing the loop body). It is generally used where we don’t know the number of iterations.
initialization;
while(condition)
{
//code to be executed
updation;
}
#include <stdio.h>
int main() {
int n,sum=0;
scanf("\n%d",&n);
int temp=n;
while(temp>=1)
{
sum=sum+temp;
temp--;
}
printf("%d",sum);
return 0;
}
5
15
Here, we are calculating the sum of n natural numbers using the while loop.
while(1){
//statement
}
If the expression passed in the while loop results in any non-zero value, then the loop will run an infinite number of times.
Note: also read about Loops in C & Loops and its examples
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…