Loops (for/foreach/while)
Demonstrates counting for loops, traversal loops (foreach/range/for...of), and while loops.
C C23
C23
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
printf("%d\n", total);
int nums[] = {10, 20, 30};
int sum = 0;
for (size_t i = 0; i < sizeof nums / sizeof nums[0]; i++) { // no foreach; iterate by index
sum += nums[i];
}
printf("%d\n", sum);
int i = 0;
int w = 0;
while (i < 3) {
w += i;
i++;
}
printf("%d\n", w);
return 0;
}
Output
15 60 3
C++ C++26
C++26
#include <iostream>
#include <vector>
int main() {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
std::cout << total << "\n";
int sum = 0;
for (int n : {10, 20, 30}) { // range-for loop
sum += n;
}
std::cout << sum << "\n";
int j = 0;
int w = 0;
while (j < 3) {
w += j;
j++;
}
std::cout << w << "\n";
return 0;
}
Output
15 60 3
C# v11.0.100
v11.0.100
var total = 0;
for (var i = 1; i <= 5; i++) {
total += i;
}
Console.WriteLine(total);
var sum = 0;
foreach (var n in new[] { 10, 20, 30 }) { // foreach over an array
sum += n;
}
Console.WriteLine(sum);
var j = 0; // top-level statements share one scope; avoid reusing variable names from above
var w = 0;
while (j < 3) {
w += j;
j++;
}
Console.WriteLine(w);
Output
15 60 3
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
total := 0
for i := 1; i <= 5; i++ {
total += i
}
fmt.Println(total)
sum := 0
for _, n := range []int{10, 20, 30} {
sum += n
}
fmt.Println(sum)
i, w := 0, 0
for i < 3 {
w += i
i++
}
fmt.Println(w)
}
Output
15 60 3
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println(total);
int sum = 0;
for (int n : new int[]{10, 20, 30}) {
sum += n;
}
System.out.println(sum);
int i = 0, w = 0;
while (i < 3) {
w += i;
i++;
}
System.out.println(w);
}
}
Output
15 60 3
JavaScript v26.13.1
v26.13.1
let total = 0;
for (let i = 1; i <= 5; i++) {
total += i;
}
console.log(total);
let sum = 0;
for (const n of [10, 20, 30]) {
sum += n;
}
console.log(sum);
let i = 0;
let w = 0;
while (i < 3) {
w += i;
i++;
}
console.log(w);
Output
15 60 3
PHP v8.6.0
v8.6.0
$total = 0;
for ($i = 1; $i <= 5; $i++) {
$total += $i;
}
echo $total, "\n";
$nums = [10, 20, 30];
$sum = 0;
foreach ($nums as $n) {
$sum += $n;
}
echo $sum, "\n";
$i = 0;
$w = 0;
while ($i < 3) {
$w += $i;
$i++;
}
echo $w, "\n";
Output
15 60 3
Python v3.15.2
v3.15.2
total = 0
for i in range(1, 6):
total += i
print(total)
sum_ = 0
for n in [10, 20, 30]:
sum_ += n
print(sum_)
i = 0
w = 0
while i < 3:
w += i
i += 1
print(w)
Output
15 60 3
Rust v1.99.1
v1.99.1
fn main() {
let mut total = 0;
for i in 1..=5 {
total += i;
}
println!("{}", total);
let mut sum = 0;
for n in [10, 20, 30] {
sum += n;
}
println!("{}", sum);
let mut i = 0;
let mut w = 0;
while i < 3 {
w += i;
i += 1;
}
println!("{}", w);
}
Output
15 60 3
TypeScript v26.13.1
v26.13.1
let total = 0;
for (let i = 1; i <= 5; i++) {
total += i;
}
console.log(total);
let sum = 0;
for (const n of [10, 20, 30]) { // for...of over an array
sum += n;
}
console.log(sum);
let i = 0;
let w = 0;
while (i < 3) {
w += i;
i++;
}
console.log(w);
Output
15 60 3
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
Go: traverses slices with range; no while — for cond {} plays that role.
Java: the enhanced for (T x : arr); while loops via while (cond).
JavaScript: for...of for values and for...in for keys; while via while (cond).
PHP: counting and traversal loops — counts with for, traverses arrays with foreach ($arr as $v); while via while (cond).
Python: for traverses iterables, with range for counting; while via while (cond).
Rust: for n in iterable (1..=5 includes the right endpoint, 1..5 excludes it); while via while (cond).