For in Crystal
To run the program, save it as for.cr
and use the crystal
command:
Crystal doesn’t have a for
keyword like in some other languages. Instead, it provides various looping constructs that achieve similar functionality:
- The
while
loop is used for condition-based loops. - Ranges with
each
method are used for iterating over a sequence of numbers. - The
times
method on integers is used for simple repetition. loop do
creates an infinite loop that can be broken out of.- The
next
keyword is used to skip to the next iteration (similar to continue
in other languages).
These constructs provide flexible and expressive ways to create loops in Crystal, covering the same use cases as Go’s for
loops.