×

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

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.