When we go on adventures, sometimes the road is straightforward and other times, the road splits and we must choose a path to take. Conditionals are like forks in the road: they determine the direction our program takes.
A conditional is an expression that evaluates
false values in order to control the flow of code in a program.
When we discussed Kotlin in terms of the order of execution, we stated that Kotlin code is compiled and executed in top-down order. This remains true; however, a conditional statement can be used to determine what instructions are executed and what instructions are ignored.
In this lesson, we will learn how to control the flow of code execution in our Kotlin programs.
Take a look at the image to the right.
In order to reach Dev City, Codey must pass through Conditional Creek, where there are many river paths that each lead to a different location.
In order to prevent travelers from getting lost, they are given colored, patterned sails that will help them navigate to their destination. At each split in the river, there are signs that say which path a traveler should take depending on the color and pattern of their boat sail.
To get to Dev City, Codey will follow the directions for red striped sails.