DO WHILE LOOP:
- The most applicable illustration of an exit control loop is the do-while loop .
- The while loop has been modified to become the do-while loop .
- The do-while loop is the most appropriate loop when it is necessary to run the program and do certain activities at least once.
- The do-while loop cannot be executed again if the evaluation of the termination condition or test expression returns false.
- Since the do-while loop analyzes the termination condition at the conclusion, it is the most important exit control loop and will therefore initially execute unconditionally for the first time.
- The do-while loop will probably be ended if the examination of the termination condition for the exit loop yields true.
- If this doesn't happen, it will be put into practise once more.
Syntax:
The do-while loop's syntax is provided below:
Example
do
{
// main body of the do-while loop
}
while (termination condition/ test expression);
while (termination condition/ test expression);
The subsequent enumeration outlines the main variances between the while loop and the do-while loop:
Do-while loop:
- An exit control loop is the do-while loop .
- The termination condition/test expression is assessed at exit in the do-while loop .
- The usage of the semicolon , which designates the end of the do-while loop , at the conclusion of the termination condition in the do-while loop is a requirement.
Syntax:
Example
do
{
// loop body
} while(condition);
Example:
Example
do
{
printf("\n%d",i);
i++;
} while(i<=6) ;
While loop:
- An entrance control loop is the while loop .
- The while loop evaluates the test expression and termination condition as soon as it enters the loop.
- The while loop does not require anything to be executed after the termination condition.
Syntax:
Example
while(condition)
{
// loop body
}
Example:
Example
while(i<=6)
{
printf("\n%d",i);
i++;
}
Understanding the functionality of the do-while loop is facilitated through the demonstration of a C program. By employing addition, subtraction, multiplication, and division, this code exemplifies the execution of fundamental arithmetic calculations.
Example
// program starts
#include <stdio.h> // standard program line for input and output
#include <conio.h> // libraries
void main()
{
float x, y, answer; // declaration of float datatype variables
int choice; // naming variables with the int datatype
do
{
printf("\n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n 5.Exit");
// by giving the user an increasing number of options
printf("\n Enter Your Choice: "); // using the user's input
scanf("%d", &choice); // putting a user-entered value into a variable
if(choice!= 5)
{
printf("\n Enter X : " ); // using the user's input
scanf("%f", &x); // putting a user-entered value into a variable
printf("\n Enter Y : " ); // using the user's input
scanf("%f", &y); // putting a user-entered value into a variable
}
switch(choice)
{
case 1: // addition case
answer = x + y; // basic operations in addition
printf("\n the addition of %f and %f is : %f", x, y, answer);
//answer will be in float datatype
break;
case 2: // subtraction case
answer = x - y; // basic operations in substraction
printf("\n the subtraction of %f and %f is : %f", x, y, answer);
//answer will be in float datatype
break;
case 3: // multiplication case
answer = x * y; // basic operations in multiplication
printf("\n the multiplication of %f and %f is : %f", x, y, answer);
//answer will be in float datatype
break;
case 4: // division case
answer = x / y; // basic operations in division
printf("\n the division of %f and %f is : %f", x, y, answer);
//answer will be in float datatype
break;
case 5:
exit(0); // exit operation
default: // program line to handle user-input errors
printf("\n sorry, you have entered the invalid input!!!");
}
getch();
} while(choice!= 5);
}
Output:
Output
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter Your Choice: 3
Enter X : 5
Enter Y : 6
the multiplication of 5.000000 and 6.000000 is : 30.000000
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter Your Choice:
Conclusion:
In summary, the exit control loop plays a vital role in the C programming language, providing a rapid and efficient method for controlling loop execution.