Exit Controlled Loop in Programming
Last Updated :
27 May, 2024
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:
C++
do {
// Loop body: Code to be executed
} while (condition);
Exit Controlled Loop in C:
Below is the implementation of Exit Controlled Loop in C:
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;
}
Exit Controlled Loop in C++:
Below is the implementation of Exit Controlled Loop in C++:
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;
}
Exit Controlled Loop in Java:
Below is the implementation of Exit 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 = 5;
// Exit Controlled Loop
do {
System.out.println(i);
i--;
} while (i < 5 && i > 0);
}
}
Exit Controlled Loop in C#:
Below is the implementation of Exit Controlled Loop in C#:
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);
}
}
Exit Controlled Loop in JavaScript:
Below is the implementation of Exit Controlled Loop in JavaScript:
JavaScript
let i = 5;
// Exit Controlled Loop
do {
console.log(i);
i--;
} while (i < 5 && i > 0);
Exit Controlled Loop in Python :
Below is the implementation of Exit Controlled Loop in Python :
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.
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:
Similar Reads
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
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 pra
4 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
Loops 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 basics of loops, with the different types and best practices. Loops in ProgrammingTable of Content What
12 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