For Loop in Programming Language

Rumman Ansari   Software Engineer   2024-10-24 08:14:45   46  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

For Loop

The for loop is one of the most commonly used loops in programming. It is typically used when the number of iterations is known beforehand. The syntax generally includes an initialization, a condition, and an increment/decrement operation.


Flowchart

For Loop in Programming Language
Figure: For Loop in Programming Language


Which loop to use? For or While

Which loop to use in Programming Language
Figure: Which loop to use in Programming Language


Language: Java


public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("Iteration: " + i);
        }
    }
}


Language: Python


for i in range(5):
    print("Iteration:", i)


Language: C++


#include <iostream>
using namespace std;

int main() {
    for (int i = 0; i < 5; i++) {
        cout << "Iteration: " << i << endl;
    }
    return 0;
}


Language: C#


using System;

class Program {
    static void Main() {
        for (int i = 0; i < 5; i++) {
            Console.WriteLine("Iteration: " + i);
        }
    }
}


Language: JavaScript


for (let i = 0; i < 5; i++) {
    console.log("Iteration: " + i);
}


Language: PHP


<?php
for ($i = 0; $i < 5; $i++) {
    echo "Iteration: " . $i . "<br>";
}
?>


Language: Swift


for i in 0..<5 {
    print("Iteration: \(i)")
}


Language: Go


package main
import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println("Iteration:", i)
    }
}


Language: Ruby


for i in 0...4
  puts "Iteration: #{i}"
end


Language: Kotlin


fun main() {
    for (i in 0..4) {
        println("Iteration: $i")
    }
}



Language: X++


static void ForLoopExample(Args _args)
{
    int i;
    
    // For loop that runs 5 times
    for (i = 1; i <= 5; i++)
    {
        info(strFmt("Iteration: %1", i));
    }
}


MCQ Available

There are 10 MCQs available for this topic.

10 MCQ