January 2019
Intermediate to advanced
246 pages
5h 23m
English
Now that you have a core set of variables, it’s time to drive decisions with them. As with any programming language, Crystal has a number of control flow constructs that let you specify the flow of program execution. We’ll go over some of them here, and we’ll add some more info in Chapter 3, Typing Variables and Controlling the Flow.
Crystal offers the classic if expression with variations that let you create complex structures. A simple if expression tests a condition and does something if that condition is met:
| | hardness = 7 # quartz |
| | |
| | if hardness < 8 |
| | puts "softer than topaz" |
| | end |
| | # => softer than topaz |
In this case, 7 is less than 8, so Crystal will report “softer ...
Read now
Unlock full access