Iteration Statements in Programming
Last Updated :
03 Jun, 2024
Iteration statements, commonly known as loops, are statements in programming used to execute part of code repeatedly based on condition or set of conditions. These constructs are important for performing repetitive tasks efficiently. In this article, we will discuss various types of iteration statements and their use in different programming languages.
Types of Iteration Statements in programming:
There are mainly three types of iteration statements:
1. For Loop:
For loops iterate over a range of values or elements in a collection. These are mainly used where the number of iterations is known beforehand like iterating through elements in an array or predefined range.
Syntax
for i in range(5):
print(i)
2. While Loops
While loops execute as long as specifies condition evaluates to true. These are suitable for situations where the number of iterations is not known and depends on dynamic conditions.
Syntax
count = 0
while count < 5:
print(count)
count += 1
3. Do-While Loops
Do while loops are similar to while loop but guarantee at least one execution of the loop body before checking the condition. These are useful when loop must execute at least once regardless of the condition.
Syntax
int count = 0;
do {
cout << count << endl;
count++;
} while (count < 5);
Iteration Statements across Different Languages:
Here, we will see the implementation of different iteration statements in different programming languages.
1. Iteration Statements in C:
C supports all the three loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in C:
C
#include <stdio.h>
int main()
{
// For Loop
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
printf("\n");
// While Loop
int count = 0;
while (count < 5) {
printf("%d ", count);
count++;
}
printf("\n");
// Do-While Loop
count = 0;
do {
printf("%d ", count);
count++;
} while (count < 5);
printf("\n");
return 0;
}
Output0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
2. Iteration Statements in C++
C++ provides support for all the common loops like: for, while, and do-while loops. Below is the implementation of all types of iteration statements in C++:
C++
#include <iostream>
using namespace std;
int main()
{
// For Loop
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
cout << endl;
// While Loop
int count = 0;
while (count < 5) {
cout << count << " ";
count++;
}
cout << endl;
// Do-While Loop
count = 0;
do {
cout << count << " ";
count++;
} while (count < 5);
cout << endl;
return 0;
}
Output0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
3. Iteration Statements in Python:
Python supports for and while loops. It does not have a native do-while loop. But same thing can be achieved using while loop with a break condition. Below is the implementation of all types of iteration statements in Python:
Python
# For Loop
for i in range(5):
print(i, end=" ")
print()
# While Loop
count = 0
while count < 5:
print(count, end=" ")
count += 1
print()
count = 0
while True:
print(count, end = " ")
count += 1
if count >= 5:
break
Output0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
4. Iteration Statements in Java
Java provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in Java:
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
// For Loop
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
System.out.println();
// While Loop
int count = 0;
while (count < 5) {
System.out.print(count + " ");
count++;
}
System.out.println();
// Do-While Loop
count = 0;
do {
System.out.print(count + " ");
count++;
} while (count < 5);
System.out.println();
}
}
Output0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
5. Iteration Statements in C#:
C# provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in C#:
C#
using System;
public class GFG {
static public void Main()
{
// For Loop
for (int i = 0; i < 5; i++) {
Console.Write(i + " ");
}
Console.WriteLine();
// While Loop
int count = 0;
while (count < 5) {
Console.Write(count + " ");
count++;
}
Console.WriteLine();
// Do-While Loop
count = 0;
do {
Console.Write(count + " ");
count++;
} while (count < 5);
Console.WriteLine();
}
}
Output0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
5. Iteration Statements in JavaScript:
JavaScript provides support for all the commonly used loops: for, while and do-while loop. Below is the implementation of all types of iteration statements in JavaScript:
JavaScript
// For Loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// While Loop
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
// Do-While Loop
count = 0;
do {
console.log(count);
count++;
} while (count < 5);
Output0
1
2
3
4
0
1
2
3
4
0
1
2
3
4
Iteration statements, commonly known as loops, are fundamental constructs in programming that enable repetitive execution of code blocks based on specified conditions. Understanding and utilizing these loops effectively is crucial for implementing repetitive tasks efficiently, reducing code redundancy, and enhancing the overall clarity and maintainability of the code.
Similar Reads
What is Iteration in Scratch Programming? Scratch is a high-level visual programming language tool that interacts with users through diagrams and blocks that have the basics of a program inbuilt in it. Scratch is used to make interactive programs especially for kids using the block kind of interfaces so that they can easily learn languages
7 min read
While loop in Programming While loop is a fundamental control flow structure in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. While loop works by repeatedly executing a block of code as long as a specified condition remains true. It evaluates the condition be
11 min read
Types of For loop in Programming For Loop is a control flow statement that allows you to repeatedly execute a block of code based on a condition. It typically consists of an initialization, a condition, and an iteration statement. Types of For loop in C:In C, there is only one type of for loop, It consists of three main parts initi
6 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
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