Exit Control Loop in C
×


Exit Control Loop in C

42

An exit control loop in C examines the exit condition at the end of each loop iteration, allowing for at least one iteration.

Control exits the loop if the termination condition evaluates to true, otherwise, it re-enters.

DO WHILE LOOP:

The primary example of an exit control loop is the do-while loop.

This loop type is a modification of the while loop, ensuring at least one iteration.

The do-while loop is suitable for scenarios where you need to perform certain actions at least once.

If the termination condition evaluates to false, the do-while loop cannot be executed again.

As the termination condition is evaluated at the end, the do-while loop always executes at least once unconditionally.

If the termination condition for the exit loop evaluates to true, the loop may terminate; otherwise, it continues execution.

syntax:

do
{
// loop body
}
while (termination condition/ test expression);

Example:

do
{
printf("\n%d",i);
i++;
} while(i<=6) ;

While loop:

The while loop is an entrance control loop.

It evaluates the termination condition immediately upon entry.

No statements are executed after the termination condition.

syntax:

while(condition)
{
// loop body
}

Example:

The do-while loop is demonstrated in the following C program, which performs basic arithmetic operations such as addition, subtraction, multiplication, and division:

// program for do-while loop
#include<stdio.h>
#include<conio.h> 

void main()  
{  
    float s, j, answer;
    int choice;
    do  
    {  
        printf("\n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n 5.Exit");
        printf("\n Enter Your Choice: ");
        scanf("%d", &choice);
        if(choice != 5)  
        {  
            printf("\n Enter X : " );
            scanf("%f", &s);
            printf("\n Enter Y : " );
            scanf("%f", &j);
        }  
        switch(choice)  
        {  
            case 1:  
                answer = s + j;
                printf("\n the addition of %f and %f is : %f", s, j, answer);  
                break;  
            case 2:  
                answer = s - j;
                printf("\n the subtraction of %f and %f is : %f", s, j, answer);
                break;  
            case 3:  
                answer = s * j;
                printf("\n the multiplication of %f and %f is : %f", s, j, answer);
                break;  
            case 4:  
                answer = s / j;
                printf("\n the division of %f and %f is : %f", s, j, answer);
                break;  
            case 5:  
                exit(0);
            default:  
                printf("\n sorry, you have entered the invalid input!!!");   
        }  
        getch();  
    } while(choice != 5);  
}  

Output:

 1.Addition 
 2.Subtraction 
 3.Multiplication 
 4.Division 
 5.Exit

This program demonstrates basic arithmetic operations and continues until the user chooses to exit.

Conclusion: In summary, the exit control loop is a vital aspect of C programming, providing a convenient and efficient to manage loop execution.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments