×

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C.

If the evaluation for termination condition results in true, then the control would be exited from the main body of the loop. Otherwise, the control enters inside the loop one more time.

This sort of loop usually deals with the exit control statement of the loop.

Exit control loop in C

DO WHILE LOOP:

  1. The do-while loop is the most relevant example of an exit control loop.
  2. The do-while loop is an adaptation of the while loop.
  3. When there is a need to execute the program and do some tasks at least once, the do-while loop is the best loop for that.
  4. When the termination condition/ test expression evaluation results in false, the do-while loop cannot be executed after the first attempt.
  5. The reason behind this is that the do-while loop is the most significant exit control loop; it evaluates the termination condition at the end, that's why the do-while loop will initially execute unconditionally for the first time.
  6. If the evaluation of the termination condition results true for the exit loop, the do-while loop will likely be terminated.
  7. If this does not occur, it will be implemented one more time.

Syntax of the do-while loop is given below:

do
{
    // main body of the do-while loop 
} 
while (termination condition/ test expression);

while (termination condition/ test expression);

The most significant dissimilarities between the do-while loop and the while loop are given below:

Do-while loop:

  1. The do-while loop is an exit control loop.
  2. In the do-while loop, the termination condition/ test expression is evaluated at the time of exit.
  3. In the do-while loop, a mandatory thing to be noted is that at the end of the termination condition, we have to use the semicolon, which specifies the end of the do-while loop.

While loop:

  1. The while loop is an entry control loop.
  2. In the while loop, the termination condition/ test expression is evaluated at the time of entrance.
  3. There is no mandatory thing in the while loop to be implemented at the end of the termination condition.

Here is a C program that is very helpful in understanding the use of the do-while loop in a far better way. This program demonstrates how to perform fundamental arithmetic operations such as addition, subtraction, multiplication, and division.

// HERE WE GO……
#include <stdio.h> // standard input and output program line
#include <conio.h> // libraries
void main()
{
    float x, y, answer; // declaration of variables with float datatype
    int choice; // declaration of variables with int datatype
    do
    {
      printf("\n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n 5.Exit");
      // providing more and more choices to the user
      printf("\n Enter Your Choice: "); // taking input from the user
      scanf("%d", &choice); // assigning value entered by user to variable
    if(choice!= 5)
    {
       printf("\n Enter X : " ); // taking input from the user
       scanf("%f", &x); // assigning value entered by user to variable
       printf("\n Enter Y : " ); // taking input from the user
       scanf("%f", &y); // assigning value entered by user to variable
    }
    switch(choice)
    {
        case 1: // addition case
               answer = x + y; // basic addition operation 
               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 subtraction operation
                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 multiplication operation
                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 division operation
                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 deal with invalid inputs by user
               printf("\n sorry, you have entered the invalid input!!!"); 
    }
    getch();
 } while(choice!= 5);
}



Related Topics

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.