Introduction to Conditional Statements

Welcome! Today, we're delving into the world of conditional statements in programming through the lens of Scala. To relate this to a real-world example, consider a traffic signal that shows different colors to manage vehicular flow — green for go, and red for stop. Similarly, Scala uses conditional statements to control the flow of execution in programs.

In Scala, we utilize if and match-case expressions to aid in decision-making. Here is an example of a simple if statement:

Indentations matter in Scala, so ensure your code blocks are correctly indented to maintain readability and structure.

Understanding the "if" Expression

In Scala, the if statement is utilised to evaluate a condition. If the condition holds true, a specific block of code is executed. Consider an example where we verify whether a person with the age of 20 is an adult:

In this instance, "You are an adult." is printed because our age, 20, is greater than or equal to 18.

"If-else" and "if-else-if-else" Constructs
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal