Member-only story

While Loop in C with Example

Bipsmedium
2 min readJul 25, 2022

--

While Loop in C with Example

A while loop in C is used to execute and repeat a block of statements based on a certain condition.

Syntex:-
while()
{
<statement Block>
}

Also Read, C Program To Find Factorial of a Number with Example

  • while is the relational or logical expression that will have the value of true or false.
  • When this statement is executed, the computer will evaluate the value of the condition.
  • If the evaluated value is true, then the block of statement is executed and is repeated until the value of the condition is false.

There must be a statement written inside the statement block to change the value of the condition, the loop will be in the infinite state.

Print natural numbers from 1 to n using while loop in C language

Complete Code:-

#include <stdio.h> 
main()
{
int i, n;
printf("Enter the value to n:");
scanf("%d",&n);
//while loop to print the numbers
while(i<=n)
{
i++;
printf("\n %d",i);
}
}

After running the above program, the user has to enter the nth value and press the ENTER key.

--

--

Bipsmedium
Bipsmedium

Written by Bipsmedium

Hi, This is Biplab and I am a PHP laravel developer and other open source technologies. I am here to share my experience with the community.

No responses yet