Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 4 / 68

Branching (the if statement)

This lesson covers how to write an if statement in Objective-C, with the aim of being able to branch on a condition.

if / else if / else branches on a condition. Since the language is built on C, the basic form is almost identical to C's.

The sample code uses if / else if / else to print one of three messages depending on score. Comparisons use >, <, == and so on, and stacking else if branches expresses three or more cases naturally.

A common early stumble is using == to compare objects. Numbers compare with ==, but comparing the contents of an object such as a string needs a dedicated method like isEqual:.

In professional work, the if statement is a basic construct used in practically every app — validating input, switching on state.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)