Control flow
Control flow is where the rubber really meets the road in programming. Without it, a program is simply a list of statements that are sequentially executed. With control flow, you can execute certain code blocks conditionally and/or repeatedly: these basic building blocks can be combined to create surprisingly sophisticated programs!Here we'll cover conditional statements (including "if", "elif", and "else"), loop statements (including "for" and "while" and the accompanying "break", "continue", and "pass").
Conditional statements, often referred to as if-then statements, allow the programmer to execute certain pieces of code depending on some Boolean condition. A basic example of a conditional statement is this:
go
js
dart
python
julia
// ...
if x := -15; x == 0 {
    fmt.Printf("%d is zero", x)
} else if x > 0 {
    fmt.Printf("%d is positive", x)
} else if x < 0 {
    fmt.Printf("%d is negative", x)
} else {
    fmt.Printf("%d is unlike anything I've ever seen...", x)
}
// ...
let x = -15

if (x == 0) {
    console.log(`${x} is zero`)
} else if (x > 0) {
    console.log(`${x} is positive`)
} else if (x < 0) {
    print(`${x} is negative`)
} else {
    console.log(`${x} is unlike anything I've ever seen...`)
}
// ...
int x = -15;

if (x == 0) {
print("$x is zero"); 
}
else if (x > 0) {
print("$x is positive"); 
} 
else if (x < 0) {
print("$x is negative"); 
}
else {
print("$x is unlike anything I've ever seen...");
}
// ...
x = -15

if x == 0:
    print(x, "is zero")
elif x > 0:
    print(x, "is positive")
elif x < 0:
    print(x, "is negative")
else:
    print(x, "is unlike anything I've ever seen...")
x = -15

if x == 0
    println("$x is zero")
elseif x > 0
    println("$x is positive")
elseif x < 0
    println("$x is negative")
else
    println("$x is unlike anything I've ever seen...")
end
Loops are a way to repeatedly execute some code statement. So, for example, if we'd like to print each of the items in a list, we can use a for loop:
go
js
dart
python
julia
// ...
for _, n := range []int{2, 3, 5, 7} {
    fmt.Print(n, " ")
}

// for range
for i := 0; i <= 10; i++ {
    fmt.Println(i)
}

// while loop
i := 0
for i < 10 {
    fmt.Println(i)
    i ++
}

// using continue
for i := 0; i < 10; i++ {
    if i%2 == 0 {
        continue
    }
    fmt.Println(i)
}

// Here is an example of a break statement used for a less trivial task. 
// This loop will fill a list with all Fibonacci numbers up to a certain value
var a, b = 0, 1
amax := 100
var L []int

for true {
    a, b = b, a+b
    if a > amax {
        break
    }
    L = append(L, a)
}

fmt.Println(L)
// ...
[2, 3, 5, 7].forEach((n: number) => {
    console.log(n)
})

// for range
for (const i of [...Array(10).keys()]) { 
    console.log(i) 
}

// while loop
let i = 0

while (i < 10) { 
console.log(i)
i ++
}

// using continue
for (let i = 0; i < 10; i++ ) {
    if (i%2 == 0) {
        continue
    }
    console.log(i)
}

// Here is an example of a break statement used for a less trivial task. 
// This loop will fill a list with all Fibonacci numbers up to a certain value
let a = 0, b = 1
let amax = 100
let L: number[] = []
let temp 

while (true) {
    temp = a
    a = b
    b = temp + b
    if (a > amax) {
        break
    }
    L.push(a)
}

console.log(L)
List<int> numbers = [2, 3, 5, 7];

for (var n in numbers) {
    print("Number: $n");
}

// warn: Function literals should not be passed to 'forEach'
numbers.forEach((n) => print(n));

// for range
for (var i in Iterable<int>.generate(10).toList()) { 
    print(i); 
}

// while loop
int i = 0;

while (i < 10) { 
  print(i);
  i ++;
}

// using continue
for (int i = 0; i < 10; i++ ) {
    if (i%2 == 0) {
        continue;
    }
    print(i);
}

// Here is an example of a break statement used for a less trivial task. 
// This loop will fill a list with all Fibonacci numbers up to a certain value
var a = 0, b = 1;
int amax = 100;
var L = <int>[];
late int temp;

while (true) {
    temp = a;
    a = b; 
    b = temp + b;
    if (a > amax) {
        break;
    }
  
    L.add(a);
}
print(L);
for N in [2, 3, 5, 7]:
    print(N, end=' ')

  # for range
for i in range(10):
    print(i)

# while loop
i = 0

while i < 10:
  print(i)
  i += 1

# using continue
for  i in range(20):
    if (i%2 == 0):
        continue
    print(i)

# Here is an example of a break statement used for a less trivial task. 
# This loop will fill a list with all Fibonacci numbers up to a certain value
a, b = 0, 1;
amax = 100
L = []

while True:
    a, b = b, a + b
    if a > amax:
        break
  
    L.append(a)
}
print(L)
for n in [2, 3, 5, 7]
    println(n)
end

# for range
for i = 0:10
    println(i)
end

foreach(i -> println(i), 0:10)


# while loop
i = 0

while i < 8
    println(i)
    global i += 1
end

# using continue
for  i = 0:20
    if i % 2 == 0
        continue
    end
    println(i)
end

#= 
Here is an example of a break statement used for a less trivial task. 
This loop will fill a list with all Fibonacci numbers up to a certain value
=#

a, b = 0, 1
amax = 100
L = Int64[] 
# or
# L = Vector{Int}([])
# L = Array{Int}([])
# also look at append!

while true
    global a, b = b, a + b
    
    if a > amax
        break
    end
            
    push!(L, a)
end

println(L)