Kotlin conditions and decisions
If, else, switch or match style branching, guard clauses, and readable decision trees.
Chapter goal: If, else, switch or match style branching, guard clauses, and readable decision trees.
Simple explanation
A condition is a road junction. The program checks a question and chooses which path to follow.
In Kotlin, this chapter is about choosing one path from several possible paths. Start with the idea above. Then connect each symbol to a value or action in the example.
Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.
Why this topic is important
Programs become useful when they react differently to different data. In Kotlin, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.
When to use it
Use conditions for permissions, validation, UI states, game rules, and workflow decisions.
Example code
val score = 82
if (score >= 80) {
println("strong")
} else if (score >= 50) {
println("keep practicing")
} else {
println("restart the lesson")
}
Line-by-line explanation
val score = 82— This stores or updatesscore. The value on the right is worked out first, then saved under that name.if (score >= 80) {— This checks a true-or-false condition. The controlled block runs only when that condition is true.println("strong")— This is the visible output line. It shows the final value after the earlier work is complete.} else if (score >= 50) {— This checks a true-or-false condition. The controlled block runs only when that condition is true.println("keep practicing")— This is the visible output line. It shows the final value after the earlier work is complete.} else {— This line supports one decision based on the current value. Read it together with the block directly around it.println("restart the lesson")— This is the visible output line. It shows the final value after the earlier work is complete.
What the output means
Pass
The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.
Mistake example
val score = 82
if (score >= 80) {
println("strong")
} else if (score > 50) {
println("keep practicing")
} else {
println("restart the lesson")
}
The comparison excludes the exact boundary value. A score of 50 should pass, but > 50 rejects it.
Fixed version
val score = 82
if (score >= 80) {
println("strong")
} else if (score >= 50) {
println("keep practicing")
} else {
println("restart the lesson")
}
The corrected comparison includes the boundary value and sends every expected input to the right branch.
Common mistakes
- Checking rare cases after the normal path becomes too nested.
- Repeating the same condition in multiple branches.
- Forgetting the default or fallback case.
Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.
Real use cases
- Login validation.
- Difficulty levels.
- Feature flags and user roles.
Practice exercise
- Add a rule for a score below 40.
- Add a distinction grade for 80 or above.
- Test the boundary values 39, 40, 79, and 80.
Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.
Mini quiz
- Which branch runs when the condition is false?
- What boundary value should be tested?
- What happens when no branch handles the input?
How to read AI-generated code
Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.
- What data goes in?
- What values are stored?
- What calculation or decision happens?
- What is printed, displayed, saved, or returned?
- What can go wrong?
- Can you rename one value and still explain the code?
Language reading check
Find the program entry point, follow function calls one at a time, and keep track of each value's type. Do not jump into a class or helper until you know who calls it.
Before you move on
- I can explain this topic in my own words.
- I can read the small example without AI.
- I can change the example and predict the new result.
- I can find and fix one simple mistake.
- I can name one real project that uses this idea.
Next topic
Next, learn loops and repetition. Before opening it, explain this chapter out loud in under one minute.
Open the interactive lesson →