Introduction
If and if-else statements help your program make choices. They let your code do different things based on conditions.
If and if-else statements help your program make choices. They let your code do different things based on conditions.
if condition { // code to run if condition is true } else { // code to run if condition is false }
let number = 10 if number > 0 { print("Number is positive") }
let number = -5 if number > 0 { print("Positive") } else { print("Not positive") }
let age = 18 if age >= 18 { print("You can vote") } else { print("You cannot vote yet") }
This program checks the temperature and prints if it is hot or not.
let temperature = 30 if temperature > 25 { print("It's hot outside") } else { print("It's not hot outside") }
Always use curly braces { } even for single lines to avoid mistakes.
Conditions must be Boolean expressions, like comparisons (==, >, <).
If statements let your program choose actions based on conditions.
If-else statements provide two paths: one if true, another if false.
Use them to make your code respond to different situations.
if statement do in Swift?if statementif statement checks a condition and runs code only when that condition is true.func.if condition { } and else { } blocks without extra keywords like 'then' or colons.let number = 10
if number > 5 {
print("Big")
} else {
print("Small")
}number is 10. The condition number > 5 is true because 10 is greater than 5.if block runs, printing "Big".let age = 18
if age >= 18
print("Adult")
else
print("Minor")if age >= 18 and else, causing a syntax error.num. Which code correctly uses if-else statements to do this?num is greater than 0, less than 0, or exactly 0.Pass the test and this pattern moves unknown