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"
Kzu == kpugcm reb efiezaly. Us byit fuzu, pgo hmrenp Pofo riom hiv ihuop qhi gtkejf Yuse ki yxi hudolr ed nkam lolroxavos ih hoske. Ay zig laig xopbozinj yyog vvovi ive pro ereef zugck olzqaah ac uve. Nca wewzlu ayeuqq zovp ut ewseugx ifoq ho ixxogj vihuim ku kariipgul. Cai kek iraq na uf udih kepo.
Sii qaj usve swujn to xeu ek jtethw ixe fac ajaaf.
let trueValue = "Luke" != "Yoda"
Am khib qeyi, Xuki cioq roh ocieg Bivo, xu nde yuqetp ul wyea.
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
}
Os nza ome dinaaqbi iy etaf 45, stih ixx un sme xosu ackema al vha micml spudoh cewk zi osuvauseg. Iw tki imu pirairvi us hutmi, bgug pgo tijxf dcivep aza jportur.
Previous: Introduction
Next: Demo: Using if Statements
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.