Types of For loop in Programming
Last Updated :
07 May, 2024
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.
In C, there is only one type of for loop, It consists of three main parts initialization, condition, and update.
Syntax:
for (initialization; condition; update) {
// code block to be executed
}
Example:
C
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
In C++, there are two types of for loop:
- Traditional For Loop
- Range-based For Loop
1. Traditional For Loop in C++:
Traditional For loop consists of three main parts initialization, condition, and update.
Syntax:
for (initialization; condition; update) {
// code block to be executed
}
Example:
C++
#include <iostream>
using namespace std;
int main() {
// Standard For loop
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
return 0;
}
2. Range-based For Loop in C++:
Range-based For Loop executes a for loop over a range. This type of loop is used to iterate over the values of a data structure, say array, vector, map, etc.
Syntax:
for (range_declaration : range_expression ) {
// code block to be executed
}
range_declaration: a declaration of a named variable, whose type is the type of the element of the sequence represented by range_expression, or a reference to that type. Often uses the auto specifier for automatic type deduction.
range_expression: any expression that represents a suitable sequence or a braced-init-list.
loop_statement: any statement, typically a compound statement, which is the body of the loop.
Example:
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
// Range-based For loop
for(int ele: arr)
cout << ele << "\n";
return 0;
}
Java provides two types of for loops:
- Traditional For loop in Java
- For-each loop in Java
1. Traditional for loop in Java
The traditional for loop in Java is used to iterate over a block of code a specific number of times. It consists of three parts Initialization, Condition and Update.
Syntax:
for (initialization; condition; update) {
// code block to be executed
}
Example:
Java
public class Main {
public static void main(String[] args) {
// Traditional for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
2. For-each loop in Java
The for-each loop, also known as the enhanced for loop. It provides a simpler way to iterate over arrays or collections in Java.
Syntax:
for (type variableName : arrayName) {
// code block to be executed
}
Example:
Java
public class Main {
public static void main(String[] args) {
// For-each loop (Enhanced for loop)
int[] numbers = {10, 20, 30, 40, 50};
for (int num : numbers) {
System.out.println(num);
}
}
}
In Python, there is primarily one type of for loop, which is used to iterate over sequences such as lists, tuples, strings, or other iterable objects. However, Python's for loop is quite flexible and can be used in various ways:
for loop for iterating over a sequence: This is the most common use case for a for loop in Python, where it iterates over the elements of a sequence.
Example:
Python
# Iterate over a list
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
Outputapple
banana
cherry
for loop with range(): The range() function is often used with a for loop to iterate over a sequence of numbers.
Example:
Python
for i in range(5):
print(i)
C# provides two types of for loops:
- Traditional for loop
- for-each loop
1. Traditional for loop in C#:
Traditional for loop in C# is Similar to C, C++ for loop.
Syntax:
for (initialization; condition; increment)
{
// code to be executed
}
Example:
C#
using System;
class Program
{
static void Main()
{
// Traditional For Loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
}
}
2. For-each Loop in C#:
The foreach loop in C# is used for iterating over elements in arrays and collections. It simplifies the process of iterating over such structures compared to the traditional for loop.
Example:
C#
using System;
class Program
{
static void Main()
{
// Foreach Loop
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (var number in numbers)
{
Console.WriteLine(number);
}
}
}
In JavaScript, there are three types of for loops:
- Traditional for loop
- for...in loop
- for...of loop
1. Traditional for loop in JavaScript:
Traditional for loop is used to iterate over a block of code a fixed number of times. It's particularly useful when you know how many times you want to loop beforehand.
Syntax:
for (initialization; condition; increment/decrement) {
// code block to be executed
}
Example:
JavaScript
for (let i = 0; i < 5; i++) {
console.log(i);
}
2. for...in loop in JavaScript:
The for...in loop is used to iterate over the properties of an object. It enumerates the properties of an object in arbitrary order.
Syntax:
for (variable in object) {
// code block to be executed
}
Example:
JavaScript
const person = { name: 'Sandeep', age: 30, city: 'Noida' };
for (const key in person) {
console.log(key + ': ' + person[key]);
}
Outputname: Sandeep
age: 30
city: Noida
3. for...of loop in JavaScript:
The for...of loop in JavaScript is used to iterate over the values of an iterable object, such as an array, string, map, set.
Syntax:
for (variable of iterable) {
// code block to be executed
}
Example:
JavaScript
const countries = ['India', 'China', 'Japan'];
for (const country of countries) {
console.log(country);
}
There are many types of For loop in different languages but all of serve the same purpose, that is to execute a block of code repeatedly. These are some of the common variations of for loops, each serving different purposes depending on the specific requirements of the program.
Similar Reads
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
Nested Loops in Programming In programming, Nested Loops occur when one loop is placed inside another. These loops are quite useful in day-to-day programming to iterate over complex data structures with more than one dimension, such as a list of lists or a grid. In this article, we will learn about the basics of nested loops a
6 min read
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
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
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
Difference between For Loop and While Loop in Programming Both for loops and while loops are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. Difference between For Loop and While L
3 min read
Iteration Statements in Programming 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 statem
5 min read
Difference Between For Loop and Do while Loop in Programming For loop and Do while loop are control flow structures in programming that allow you to repeatedly execute a block of code. However, they differ in their syntax and use cases. It is important for a beginner to know the key differences between both of them. For Loop in Programming:The for loop is use
4 min read