Welcome! Today, we'll dive into the world of conditional statements in programming. Let's start with a real-life example. Consider a traffic light: it displays different colors to control traffic flow — if it's green, the cars move. However, if it's red, they stop. Programming languages, like Kotlin, use conditional statements to control the execution flow similarly.
In Kotlin, we use the if and when expressions to make decisions. Consider a simple if statement:
In Kotlin, the if statement checks a condition and runs a specific code block if the condition is true — quite like making the decision to take an umbrella based on whether it's raining or not.
Take a look at the example where we verify whether a person with an age of 20 is an adult:
The message "You are an adult." gets printed because our age, which is 20, is greater than 18.
