Control Flow Statements in Swift
Swift provides several control flow statements, including if-else statements, loops, and switch statements.
swift
// if-else statement
var age = 18
if age >= 18 {
print("You can vote!")
} else {
print("Sorry, you can't vote yet.")
}
// for loop
for i in 1...5 {
print(i)
}
// switch statement
let country = "USA"
switch country {
case "USA":
print("You are in the United States")
case "Canada":
print("You are in Canada")
default:
print("You are in an unknown location")
}
No comments:
Post a Comment