Crystal by example: Loops
Loops, iterations, or repetition structures are a programming concept that allows a block of code to be repeated based on boolean conditions (true or false). They are part of the control flow in many programs.
In Crystal, there are several ways to create loops, such as using while, until, loop, and each. Crystal does not have a for structure like other languages, but there are alternatives.
The each loop is mainly used with arrays.
while is commonly used to run code until a certain condition becomes false.
loop and while are similar, but while requires conditions to be true in order to run, whereas loop runs automatically.
next is used to skip to the next iteration, and break stops the iteration.
times is used for repeating a code n times.
arr = [1, 2, 3, 4]
arr.each do |i| puts iend
h = 3
while h < 3 h += 1 puts hend
n = 0
until n >= 3 n += 1 puts nend
while true # runs for eternityend
loop do # similar to while true but without conditionsend
ab = 0
while ab < 5 ab += 1 if ab == 2 puts "2!" next end
if ab == 3 puts ab break endend
2.times do puts "Repeating 2 times"end$ crystal run loops.cr
12341232!3Repeating 2 timesRepeating 2 times Next example: Arrays