Entry Controlled Loops in Programming

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

What are Entry Controlled Loops?

Entry controlled loops are loop structures where the condition is checked before entering the loop body. If the condition evaluates to true, the loop body is executed, otherwise, the loop is terminated. Entry Controlled Loops are executed in the following order:

  • Condition Check: The loop condition is checked before executing the loop body.
  • Execution: If the condition is true, the loop body is executed; otherwise, the loop terminates without executing the loop body.
  • Initialization and Increment/Decrement: For for loops, initialization, condition checking, and increment/decrement are all handled in the loop’s syntax. For while loops, the initialization and increment/decrement must be handled separately within the loop’s body or before and after the loop.

Types of Entry Controlled Loops:

There are two types of Entry Controlled Loops in Programming:

  • For Loop: For loop is a control flow statement in programming that allows you to execute a block of code repeatedly based on a specified condition. It is commonly used when you know how many times you want to execute a block of code.
  • While Loop: While loop is a fundamental control flow structure (or loop statement) in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. Unlike the for loop, which is used for iterating a fixed number of times, the while loop is preferred in scenarios where the number of iterations is uncertain or dependent on dynamic conditions.

Entry Controlled Loop in C:

Below is the implementation of Entry Controlled Loop in C:

C
#include <stdio.h>

int main()
{
    int i = 0;

    // Entry controlled loop using while loop
    printf("Entry controlled loop using while loop:\n");
    while (i < 5) {
        printf("%d ", i);
        i++;
    }
    printf("\n");

    // Reset i for the next loop
    i = 0;

    // Entry controlled loop using for loop
    printf("Entry controlled loop using for loop:\n");
    for (i = 0; i < 5; i++) {
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}

Output
Entry controlled loop using while loop:
0 1 2 3 4 
Entry controlled loop using for loop:
0 1 2 3 4 

Entry Controlled Loop in C++:

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

C++
#include <iostream>

using namespace std;

int main()
{
    int i = 0;

    // Entry controlled loop using while loop
    cout << "Entry controlled loop using while loop:"
         << endl;
    while (i < 5) {
        cout << i << " ";
        i++;
    }
    cout << endl;

    // Reset i for the next loop
    i = 0;

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

    return 0;
}

Output
Entry controlled loop using while loop:
0 1 2 3 4 
Entry controlled loop using for loop:
0 1 2 3 4 

Entry Controlled Loop in Java:

Below is the implementation of Entry Controlled Loop in Java:

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

import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        int i = 0;

        // Entry controlled loop using while loop
        System.out.println(
            "Entry controlled loop using while loop:");
        while (i < 5) {
            System.out.print(i + " ");
            i++;
        }
        System.out.println();

        // Reset i for the next loop
        i = 0;

        // Entry controlled loop using for loop
        System.out.println(
            "Entry controlled loop using for loop:");
        for (i = 0; i < 5; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
    }
}

Output
Entry controlled loop using while loop:
0 1 2 3 4 
Entry controlled loop using for loop:
0 1 2 3 4 

Entry Controlled Loop in Python:

Below is the implementation of Entry Controlled Loop in Python:

Python
# Entry controlled loop using while loop
print("Entry controlled loop using while loop:")
i = 0
while i < 5:
    print(i, end=" ")
    i += 1
print()

# Reset i for the next loop
i = 0

# Entry controlled loop using for loop
print("Entry controlled loop using for loop:")
for i in range(5):
    print(i, end=" ")
print()

Output
Entry controlled loop using while loop:
0 1 2 3 4 
Entry controlled loop using for loop:
0 1 2 3 4 

Entry Controlled Loop in JavaScript:

Below is the implementation of Entry Controlled Loop in Javascript:

JavaScript
// Entry controlled loop using while loop
console.log("Entry controlled loop using while loop:");
let i = 0;
while (i < 5) {
    console.log(i);
    i++;
}
console.log();

// Reset i for the next loop
i = 0;

// Entry controlled loop using for loop
console.log("Entry controlled loop using for loop:");
for (let i = 0; i < 5; i++) {
    console.log(i);
}
console.log();

Output
Entry controlled loop using while loop:
0
1
2
3
4

Entry controlled loop using for loop:
0
1
2
3
4

Entry Controlled Loop in C#:

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

C#
using System;

class GFG {
    static void Main(string[] args)
    {
        // Entry controlled loop using while loop
        Console.WriteLine(
            "Entry controlled loop using while loop:");
        int i = 0;
        while (i < 5) {
            Console.Write(i + " ");
            i++;
        }
        Console.WriteLine();

        // Reset i for the next loop
        i = 0;

        // Entry controlled loop using for loop
        Console.WriteLine(
            "Entry controlled loop using for loop:");
        for (i = 0; i < 5; i++) {
            Console.Write(i + " ");
        }
        Console.WriteLine();
    }
}

Output
Entry controlled loop using while loop:
0 1 2 3 4 
Entry controlled loop using for loop:
0 1 2 3 4 

Comparison with Exit-Controlled Loops

  • In contrast, exit-controlled loops (like the do-while loop) evaluate the condition after the loop body executes, ensuring the loop body runs at least once regardless of the condition.
  • Understanding entry-controlled loops is essential for writing effective and efficient code, as they provide a structured way to repeat operations while maintaining control over the conditions for repetition.

Related Articles: