Entry vs Exit Controlled Loop in Programming

Loops or Iteration Statements in Programming are helpful when we need a specific task in repetition. They’re essential as they reduce hours of work to seconds. In this article, we will explore the difference between entry and exit controlled loop in programming, with the different types and best practices.

Entry Controlled loop in Programming

In an entry controlled loop, the condition to enter the loop is evaluated before the loop body is executed. This means that the loop is only entered if the condition evaluates to true initially. If the condition is false from the outset, the loop is bypassed entirely. The condition is evaluated at the beginning of each iteration, and if it becomes false at any point, the loop terminates.

Below are the examples of Entry-controlled loops:

C++
#include <iostream>
using namespace std;

int main()
{

    // Entry-controlled for loop
    int i;
    for (i = 0; i < 5; i++) {
        cout << i << " ";
    }
    cout << endl;

    // Entry-controlled while loop
    i = 0;
    while (i < 5) {
        cout << i << " ";
        i++;
    }

    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        // Entry-controlled for loop
        int i;
        for (i = 0; i < 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();

        // Entry-controlled while loop
        i = 0;
        while (i < 5) {
            System.out.print(i + " ");
            i++;
        }
    }
}
Python
# Entry-controlled for loop
for i in range(5):
    print(i, end=" ")
print()

# Entry-controlled while loop
i = 0
while(i < 5):
print(i, end=" ")
i += 1
C#
// c# code for the above approach
using System;

class Program {
    static void Main()
    {
        // Entry-controlled for loop
        for (int i = 0; i < 5; i++) {
            Console.Write(i + " ");
        }
        Console.WriteLine();

        // Entry-controlled while loop
        int j = 0;
        while (j < 5) {
            Console.Write(j + " ");
            j++;
        }
    }
}
JavaScript
let outputFor = '';
let outputWhile = '';

// Entry-controlled for loop
for (let i = 0; i < 5; i++) {
    outputFor += i + ' ';
}

// Entry-controlled while loop
let j = 0;
while (j < 5) {
    outputWhile += j + ' ';
    j++;
}

console.log(outputFor);
console.log(outputWhile);

Output
0 1 2 3 4 
0 1 2 3 4 

Exit Controlled loop in Programming

An exit controlled loop evaluates its condition after executing the loop body. This means that the loop body is executed at least once, regardless of the condition. After each iteration, the condition is evaluated, and if it becomes false, the loop terminates.

Below are the examples of Exit-controlled loops

C++
#include <iostream>
using namespace std;

int main()
{
    // Exit controlled do-while loop
    int i = 0;
    do {
        cout << i << " ";
        i++;
    } while (i < 5);
    return 0;
}
Java
/*package whatever //do not write package name here */

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        // Exit controlled do-while loop
        int i = 0;
        do {
            System.out.print(i + " ");
            i++;
        } while (i < 5);
    }
}
Python
# Exit controlled do-while loop
i = 0
while True:  # In Python, there's no direct equivalent of a do-while loop, so we use a while loop with a conditional break
    print(i, end=" ")  # Print the current value of i followed by a space
    i += 1  # Increment i
    if i >= 5:  # Check if i is greater than or equal to 5
        break  # If so, exit the loop
C#
using System;

class Program
{
    static void Main()
    {
        // Initialize the counter variable
        int i = 0;

        // Exit-controlled do-while loop
        do
        {
            // Print the current value of i
            Console.Write($"{i} ");

            // Increment the counter
            i++;
        } while (i < 5);

        // Return 0 to indicate successful completion
        Environment.Exit(0);
    }
}
JavaScript
let i = 0;
let output = '';

do {
    output += i + ' ';
    i++;
} while (i < 5);

console.log(output);

Output
0 1 2 3 4 

Entry vs Exit Controlled Loop in Programming:

Below are the differences between Entry Controlled Loop and Exit Controlled Loop in Programming:

Entry Controlled Loop

Entry Controlled Loop

Condition is evaluated before body of loop is executed.

Condition is evaluated after body of loop is executed.

Loop body may not execute if condition is false initially.

Loop body executes at least once before condition evaluation.

while (condition) { // Loop body }

do { // Loop body } while (condition);

Used when the loop may not need to execute at all if the condition is initially false.

Used when the loop body must execute at least once before evaluating the condition.