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
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
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
Data Types in Programming
In Programming, data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and that the value of each property is as expected. Data Types in Programming Table of Co
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
What are Operators in Programming?
Operators in programming are essential symbols that perform operations on variables and values, enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types. Operators in Programming Table of
15+ 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
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
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