do-while loop

Syntax of do-while loop

initialization;
do {
    statement1;
    statement2;
} while(condition);

Syntax Explained

  1. In this loop similar to other loops, At first, initialization is performed.
  2. After initialization, as against the other loops, directly body of the loop executed without checking any condition.
  3. After executing the body of a loop, condition check performed and if condition true the loop is repeated again and executed body of the loop followed by the condition.
  4. Loop continues executing until condition becomes false, as the condition becomes false loop terminates and control jumps out of the loop.

Key points about do-while loop

  1. As seen above, for the first time no condition check performed and condition check at the end to the loop so this loop is called exit control loop or bottom tested loop.
  2. Due to the exit control loop, the body of the loop executes at least once.
  3. If loop rotates n time then condition check n times in the case of the do-while loop.

Entry control V/S exit control loop

Entry control loopExit Control loop
If loop rotates n time, then condition check n + 1 times.If loop rotates n times then condition check n times.
If the condition becomes false for the first time, then no statement executes or body of the loop doesn't execute.If the condition becomes false for the first time, the body of the loop executes at least once.
Ex: for loop, while loopEx: do-while loop 

Examples of do-while loop

i = 1;
do {
    printf("%d ",i);
    i++;
} while(i <= 10);
//prints 1 2 3 4 5 .... 10
i = 1;
do {
    printf("%d ",i);
    i++;
} while(i > 10);
//prints 1, becuase condition false in the first time and body executes only once.

Odd loop using while loop

Sometimes it is desired that after each round of loop we want to ask from the user that he wants to go for the next time and based on the response loop rotates next time or terminates.

If user response is yes, then loop rotates for next time otherwise terminates, this type of loop construct is known as odd loop.

An odd loop can be written by using any loop while loop, for loop and do-while loop.

Odd loop example using while loop

#include
int main() {
	int n;
	char ch = 'Y';
	
	while(ch == 'Y') {	
		printf("\nEnter any number: ");
		scanf("%d",&n);
		
		printf("Square of %d = %d",n,n*n);
		
		printf("\nDo you want to go for next time (y/n): ");
		ch = getche();
	}
	return 0;
}

Output

Enter any number: 5
Square of 5 = 25
Do you want to go for next time (y/n): Y
Enter any number: 6
Square of 6 = 36
Do you want to go for next time (y/n): Y
Enter any number: 4
Square of 4 = 16
Do you want to go for next time (y/n): n

Explanation

  1. As you see in the above output that after entering a number for the first time square is calculated and printed.
  2. After each iteration, we prompt a message from the user to ask that he wants to go for the next time, and if the response is 'Y' then the loop rotates for next time and if a response is 'N' then loop terminates.
  3. Observe line-4 in the above program, we have initialized ch with 'Y', this is mandatory because each variable contains garbage value by default, but because we want to enter in the loop for the first time, so we explicitly initialize ch with 'Y'.

Odd loop using do-while loop

#include
int main() {
	int n;
	char ch;
	do {	
		printf("\nEnter any number: ");
		scanf("%d",&n);
		
		printf("Square of %d = %d",n,n*n);
		
		printf("\nDo you want to go for next time (y/n): ");
		ch = getche();
	} while(ch == 'y');
	return 0;
}

Explanation

The only difference b/w the above program and in this program is that in this program we need not initialize ch with 'Y' because the do-while loop at least executes once that is best suited for this scenario.

do-while loop | Video Tutorial in Hindi