Loops compared across 16 programming languages
Repeat tasks: counting loops and loops over collections compared side by side.
PyPython
for i in range(3):
print("Round", i)
for frucht in ["Apple", "Pear"]:
print(frucht)range(3) yields 0, 1, 2; the second loop runs directly over the list.
JSJavaScript
for (let i = 0; i < 3; i++) {
console.log("Round", i);
}
for (const frucht of ["Apple", "Pear"]) {
console.log(frucht);
}The classic for loop counts, for...of iterates directly over the values of a collection.
TSTypeScript
for (let i = 0; i < 3; i++) {
console.log("Round", i);
}
const fruechte: string[] = ["Apple", "Pear"];
for (const frucht of fruechte) {
console.log(frucht.toUpperCase());
}frucht automatically has the type string, so the editor offers string methods.
JvJava
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("Round " + i);
}
for (String frucht : new String[]{"Apple", "Pear"}) {
System.out.println(frucht);
}
}
}The first loop is the classic counting loop (0, 1, 2), the second is the for-each loop over an array.
CC
#include <stdio.h>
int main(void) {
for (int i = 0; i < 3; i++) {
printf("Round %d\n", i);
}
const char *fruechte[] = {"Apple", "Pear"};
for (size_t i = 0; i < 2; i++) {
printf("%s\n", fruechte[i]);
}
return 0;
}The first loop counts 0, 1, 2; the second iterates by index over an array of strings (C has no for-each).
C++C++
#include <iostream>
#include <string>
#include <vector>
int main() {
for (int i = 0; i < 3; ++i)
std::cout << "Round " << i << '\n';
std::vector<std::string> fruechte{"Apple", "Pear"};
for (const auto& f : fruechte)
std::cout << f << '\n';
}The classic for loop counts, the range-for loop iterates directly over the container. const auto& avoids copies.
C#C#
for (int i = 0; i < 3; i++)
{
Console.WriteLine($"Runde {i}");
}
foreach (string frucht in new[] { "Apple", "Pear" })
{
Console.WriteLine(frucht);
}for counts with an index, foreach iterates directly over each element of a collection.
GoGo
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Println("Runde", i)
}
for _, frucht := range []string{"Apple", "Pear"} {
fmt.Println(frucht)
}
}Go has only for: classic with a counter or with range over collections. The underscore discards the index you do not need.
RsRust
fn main() {
for i in 0..3 {
println!("Round {i}");
}
for frucht in ["Apple", "Pear"] {
println!("{frucht}");
}
}0..3 yields 0, 1, 2 (0..=3 would include the 3). The second loop runs directly over an array.
PHPPHP
<?php
for ($i = 0; $i < 3; $i++) {
echo "Round $i\n";
}
foreach (["Apple", "Pear"] as $frucht) {
echo $frucht, "\n";
}for counts with an index, foreach runs directly over the values of an array (optionally with $schluessel => $wert).
RbRuby
3.times { |i| puts "Round #{i}" }
["Apple", "Pear"].each do |frucht|
puts frucht
endRuby uses iterators with blocks instead of counting loops: times repeats, each runs over the elements. { } for single-line blocks, do ... end for multi-line ones.
SwSwift
for i in 0..<3 {
print("Round", i)
}
for frucht in ["Apple", "Pear"] {
print(frucht)
}0..<3 yields 0, 1, 2 (0...3 would include the 3). The second loop runs directly over the array.
KtKotlin
fun main() {
for (i in 0 until 3) {
println("Round $i")
}
for (frucht in listOf("Apple", "Pear")) {
println(frucht)
}
}0 until 3 is a range from 0 to 2; the second loop runs directly over the list.
SQLSQL
WITH RECURSIVE zaehler(n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM zaehler WHERE n < 5
)
SELECT n, n * n AS quadrat FROM zaehler;
-- PostgreSQL shorter: SELECT n, n*n FROM generate_series(1, 5) AS n;SQL is set-oriented; there are no real loops. A recursive CTE repeats the second SELECT until the WHERE condition yields nothing more. PostgreSQL has a shortcut with generate_series.
shBash
for i in 1 2 3; do
echo "Round $i"
done
for frucht in Apple Pear; do
echo "$frucht"
done
n=0
while (( n < 3 )); do
echo "n=$n"
n=$((n + 1))
donefor runs over a list of words (also {1..5} or *.txt), while as long as the condition holds. Blocks are formed with do … done.
</>HTML & CSS
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.raster { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; counter-reset: nr; }
.raster div { counter-increment: nr; background: #cde; border-radius: 8px; padding: 18px 0; text-align: center; }
.raster div::before { content: "Nr. " counter(nr); font-weight: 700; }
</style>
<div class="raster">
<div></div><div></div><div></div><div></div>
<div></div><div></div><div></div><div></div>
</div>HTML/CSS has no loops: elements are written out individually in the HTML (or generated with JavaScript). But CSS repeats columns with repeat(4, 1fr) and counts elements automatically with counter-increment/counter().