Types of Loops in Programming
In programming, loops are categorized into two main types based on the control mechanism: entry-controlled loops and exit-controlled loops.
1. Entry-Controlled loops:
In Entry controlled loops the test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops.
Below are the examples of Entry-controlled loops:
#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;
}
/*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++;
}
}
}
# 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# 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++;
}
}
}
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
2. Exit-Controlled loops:
In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. Do-while Loop is an example of Exit Controlled loop.
Below are the examples of Exit-controlled loops:
#include <iostream>
using namespace std;
int main()
{
// Exit controlled do-while loop
int i = 0;
do {
cout << i << " ";
i++;
} while (i < 5);
return 0;
}
/*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);
}
}
# 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
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);
}
}
let i = 0;
let output = '';
do {
output += i + ' ';
i++;
} while (i < 5);
console.log(output);
Output
0 1 2 3 4
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.
Table of Content
- What are Loops in Programming?
- Types of Loops in Programming
- For loop
- While Loop
- Do-While Loop
- Nested Loops
- For Loop vs While Loop vs Do-While Loop in Programming
- Common mistakes to avoid in Programming