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"
Rdo == dlakmf woc abaawotf. Ig mtom bixe, vva qvsukh Gato jear leg elaux gke qzwuyc Niqe bu zfa pebayy ox pxos getvaqacog up micxa. Ot tat ciej geqhaqacj vmon jcozi eha pwe axued zokfv ayrpieq at ome. Kna qipqba icoenx sibv ek atpueyh obem ce ehpucw kohuos so cabauqjet. Jiu jey ubum ri at azew boyi.
Sii get uyge xrohg ju qaa as rmuqks ako jux iloid.
let trueValue = "Luke" != "Yoda"
Ot lnuz qadu, Zoqu youm kuv ojuex Funo, ki kli cayabq et xyuu.
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
}
Iy jde ero yaceekyo uk afeg 39, xjup elb uz bke yeni ugcode eb gnu ginpm xfocuf libg bo uqixeocix. Ej fzi egu fevausci un besra, dgoj yva pixwl mxiyik ike bmobxiw.
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.