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
Jump Statements in Programming
Jump statements in programming allow altering the normal flow of control within a program. These statements provide a way to transfer the execution to a different part of the code, facilitating conditional exits, loop control, function return, or unconditional jumps. Table of Content What is a Jump
6 min read
Goto Statement in Programming
Goto statement is a control flow statement present in certain programming languages like C and C++. It enables programmers to transfer program control to a labeled section of code within the same function or block. Despite its availability, its usage is often discouraged in modern programming practi
3 min read
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
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
Break vs Continue Statement in Programming
Break and Continue statements are the keywords that are used always within a loop. The purpose of a break and continue statement is to stop a running loop or to continue a particular iteration. In this article we will see what are the break and continue statements, what is their use and what are the
4 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
Dynamic Programming (DP) Introduction
Dynamic Programming is a commonly used algorithmic technique used to optimize recursive solutions when same subproblems are called again.The core idea behind DP is to store solutions to subproblems so that each is solved only once. To solve DP problems, we first write a recursive solution in a way t
15+ min read
Jump Statements in Python
In any programming language, a command written by the programmer for the computer to act is known as a statement. In simple words, a statement can be thought of as an instruction that programmers give to the computer and upon receiving them, the computer acts accordingly. There are various types of
4 min read
What is Product Iteration?
In todayâs fast-moving market, focusing on customers is essential. Creating a product isnât a one-time task, it is about continuously improving it. Product iteration means regularly updating the product based on customer feedback and data. This helps businesses make products that better meet custome
9 min read