0
0
Swiftprogramming~10 mins

If and if-else statements in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the number is positive.

Swift
let number = 5
if number [1] 0 {
    print("Positive number")
}
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will check for negative numbers.
2fill in blank
medium

Complete the code to print "Even" if the number is divisible by 2.

Swift
let number = 8
if number % 2 [1] 0 {
    print("Even")
}
Drag options to blanks, or click blank then click option'
A!=
B<
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will check for odd numbers instead.
3fill in blank
hard

Fix the error in the if-else statement to print "Adult" if age is 18 or more.

Swift
let age = 20
if age [1] 18 {
    print("Adult")
} else {
    print("Minor")
}
Drag options to blanks, or click blank then click option'
A>=
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will check for ages less than 18, which is incorrect here.
4fill in blank
hard

Fill both blanks to print "Child" if age is less than 13, otherwise "Teen or Adult".

Swift
let age = 10
if age [1] 13 {
    print("Child")
} [2] {
    print("Teen or Adult")
}
Drag options to blanks, or click blank then click option'
A<
B>
Celse
Delif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'elif' instead of 'else' in the second blank causes syntax error.
5fill in blank
hard

Fill all three blanks to print "Positive", "Negative", or "Zero" based on the value of number.

Swift
let number = 0
if number [1] 0 {
    print("Positive")
} else if number [2] 0 {
    print("Negative")
} [3] {
    print("Zero")
}
Drag options to blanks, or click blank then click option'
A>
B<
Celse
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' in the first or second blank causes wrong logic.