Entry vs Exit Controlled Loop in Programming
Last Updated :
24 May, 2024
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.
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);
Output0 1 2 3 4
0 1 2 3 4
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);
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.
|
Similar Reads
Exit Controlled Loop in Programming 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
3 min read
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
6 min read
For loop in Programming For loop is one of the most widely used loops in Programming and is used to execute a set of statements repetitively. We can use for loop to iterate over a sequence of elements, perform a set of tasks a fixed number of times. In this article, we will learn about the basics of For loop, its syntax al
11 min read
Control flow statements in Programming Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
15+ min read
Do-While loop in Programming Do-while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop's body, ensuring that the loop's body is executed at least once. In this article, we will learn abou
10 min read