For Loop in Programming Language
☰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
Which loop to use? For or While
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 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
"; } ?>
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)); } }