Instruction

First, you need to understand the concepts of conditions and decision-making in programming. Imagine you’re a teacher and you have to review your students’ assignments and assign them relevant grades. Now, you want to write a simple program in Kotlin that translates the grades to their descriptions. For example, an A means Excellent, a B means Very Good, a C - Good, and so on. You can use the if statement to check the grade and print the description accordingly. But, what is a condition? Take a look at the diagram below.

Excellent! Not so good Very good! Yes No Yes No Grade equal to A? Grade equal to B?

A condition is a decision-based statement that evaluates to either true or false — these are the only two possible values. You can decide to execute some code only when a condition is met, or you can choose to execute some code in case a condition is not met. And, you can chain multiple conditions. You’ll learn all of this in this lesson.

In the diagram above, there are two conditions with three branches. The first condition results in Excellent if the grade is A, otherwise a second condition results in Very good if the grade is B. If both the conditions do not match, it results in Not so good.

The if statement is the most basic form of decision-making in programming. As the name suggests, it checks a condition and executes a block of code only if the condition is met. Or in other words, if the expression evaluates to true.

The syntax of the if statement consists of the condition expression inside the parentheses and the body inside the optional curly braces. You can put there any code you want. Program will execute all that code if the condition is met. Without the curly braces, only the next line of code is a part of the body. For instance the if statement may look like this:

if (grade == 'A') {
    println("Your grade is excellent")
    println("You are the best")
}

In case of more than one condition, you can use the else if statement. It allows you to check another condition if the previous one is not met. For example:

if (grade == 'A') {
    println("Your grade is excellent")
} else if (grade == 'B') {
    println("You are very good")
}

Note the if and else-if branches are mutually exclusive. When the program encounters a matching condition it won’t check any further ones.

Imagine there are percentages instead of the grades. And you have two conditions:

  • score > 90
  • score > 80 For the score of 95 both conditions are met. But, the program will execute only the first one.

There is also an else statement. It is always at the end of the chain. If none of the previous conditions are met, the program executes the code inside the else block. It is optional and you can also use it without the else if statement.

The next topic of this lesson is the variable’s scope. The scope of a variable defines the part of the code where it can be accessed. Take a look at the following image:

The yellow color occupies most of the space. But, on some parts of the image you can see the red and blue areas. There’s no yellow color inside them. They cover it completely.

Scopes work in a similar way. A variable is accessible only inside the scope where it’s declared. Another variable with the same name can cover one from the outer scope. This is called shadowing.

OK, that’s enough theory. It’s time to dive into the code!

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo