Do While Loop in C with example

Do While Loop in C with example
  • When the above statement is executed, the computer will execute the statement block without checking the value of the condition.
  • After the statement block completes its execution once, the condition is evaluated.
  • If the value of the condition is true, the statement block is executed again and is repeated until the condition is false.

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

#include <stdio.h> 
main()
{
int i, n;
printf("Enter the value to n:");
scanf("%d",&n);
//do while loop to print the numbers
i=1;
do
{
i++;
printf("\n%d",i);
}
while(i<=n); }
  • At first, the initial value of i is incremented once and it becomes 2 i.e. the value of i is now 2.
  • Now 2 is compared with 5 and if it is less than 5 then the value of i is incremented and it becomes 3.
  • Now 3 is compared with 5 and if it is less than 5 then the value of i is incremented and it becomes 4.
  • Now 4 is compared with 5 and if it is less than 5 then the value of i is incremented and it becomes 5.
  • Now 5 is compared with 5 and if it is less than or equal to 5 then the value of i is incremented and it becomes 6

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Bipsmedium

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.