Open In App

Exit Controlled Loop in Programming

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Exit controlled loops in programming languages allow repetitive execution of a block of code based on a condition that is checked after entering the loop. In this article, we will learn about exit controlled loops, their types, syntax, and usage across various popular programming languages.

What are Exit Controlled Loops?

Exit Controlled loops are loop structures where the loop's condition is checked after the loop body has executed. This means that the loop will always execute at least once, regardless of whether the condition is true or false initially. The most common examples of exit-controlled loops are the do-while loop in languages like C, C++, and Java.

Here's a general structure of a do-while loop:

do {
    // Loop body: Code to be executed
} while (condition);

Exit Controlled Loop in C:

Below is the implementation of Exit Controlled Loop in C:

#include <stdio.h>

int main()
{
    int i = 5;
    // Exit Controlled Loop
    do {
        printf("%d\n", i);
        i--;
    } while (i < 5 && i > 0);
    return 0;
}

Output
5
4
3
2
1

Exit Controlled Loop in C++:

Below is the implementation of Exit Controlled Loop in C++:

#include <iostream>
using namespace std;

int main()
{

    int i = 5;
    // Exit Controlled Loop
    do {
        cout << i << endl;
        i--;
    } while (i < 5 && i > 0);
    return 0;
}

Output
5
4
3
2
1

Exit Controlled Loop in Java:

Below is the implementation of Exit Controlled Loop in Java:

/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int i = 5;
        // Exit Controlled Loop
        do {
            System.out.println(i);
            i--;
        } while (i < 5 && i > 0);
    }
}

Output
5
4
3
2
1

Exit Controlled Loop in C#:

Below is the implementation of Exit Controlled Loop in C#:

using System;

public class GFG {

    static public void Main()
    {

        int i = 5;
        // Exit Controlled Loop
        do {
            Console.WriteLine(i);
            i--;
        } while (i < 5 && i > 0);
    }
}

Output
5
4
3
2
1

Exit Controlled Loop in JavaScript:

Below is the implementation of Exit Controlled Loop in JavaScript:

let i = 5;
// Exit Controlled Loop
do {
    console.log(i);
    i--;
} while (i < 5 && i > 0);

Output
5
4
3
2
1


Exit Controlled Loop in Python :

Below is the implementation of Exit Controlled Loop in Python :

def main():
    i = 5
    # Exit Controlled Loop
    while True:
        print(i)
        i -= 1
        # Condition to exit the loop
        if not (i < 5 and i > 0):
            break

# Call the main function to execute the code
if __name__ == "__main__":
    main()

#Note: Python does not have a built-in do-while loop syntax 
# like C++. However, you can simulate a do-while loop using a
# while True loop combined with a conditional break statement to exit the loop.

Output
5
4
3
2
1


Comparison with Entry-Controlled Loops:

Entry-Controlled Loop (e.g., while, for loops): The condition is checked before the loop body executes. If the condition is false initially, the loop body may not execute at all.

Exit-Controlled Loop (e.g., do-while loop): The loop body executes at least once before the condition is checked.

Related Articles:


Next Article
Article Tags :

Similar Reads