do while loop in C

Previous
Next

do-while loop:

  • While : Check the condition and execute the block. Repeat loop as long as the condition is valid.
  • Do-while: Execute the block for the first time without checking the condition. The Loop repetition continuous only if the condition is valid.
  • The block executes at least once in do-while even though the specified condition is false.
#include<stdio.h>
int main()
{
	do
	{
		printf("Minimum once \n");
	}while(0);
			
	return 0;	
}

Nested Do while loop:

  • Writing do-while loop inside another do-while loop
  • The execution terminates only when outer condition is false.
#include<stdio.h>
int main()
{
	int x=1;
	do
	{
		int y=1;
		printf("x val : %d \n", x);
		do
		{
			printf("\ty val : %d \n", y);
		}while(++y <= 3);
	}while(++x <= 3);
			
	return 0;	
}

Find Biggest of 3 numbers iteratively using do-while loop:

#include<stdio.h>
int main()
{
	int a,b,c;
	int repeat=1;
	do
	{
		printf("Enter 3 numbers : \n");
		scanf("%d%d%d", &a, &b, &c);
		
		a>b && a>c ? printf("a is big \n") : b>c ? printf("b is big \n") : printf("c is big \n");
		
		printf("To continue(press 1) and to stop(press 0) : ");
		scanf("%d", &repeat);
	}while(repeat);
	return 0;	
}
Previous
Next

Add Comment

Courses Enquiry Form