if Statements

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

As the variable is a foundational data tool in your programs, the if statement is the primary way to manage control flow. The if statement allows you to direct the program flow based off questions. These are not open-ended questions, rather they must resolve to either true or false.

For example, is the user a premium account holder? Is the user over eighteen? Does the user’s post contain over ten comment?

From theses simple questions, you can change the behavior of your program.

Using Boolean Operators

Boolean operators are the key to if statements. These operators compare two values and produce a true or false result. For example:

let falseValue = "Luke" == "Yoda"
let trueValue = "Luke" != "Yoda"

Working with Braces

When you define an if statement, you start with the keyword if followed by the checked condition. You then provide a pair of curly braces. For example:

if age > 21 {
  // code goes here
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Demo: Using if Statements