📖 Scenario: You are creating a simple app that decides if a person can enter a club based on their age and membership status.
🎯 Goal: Build a Swift program that uses logical operators to check if a person is allowed to enter the club.
📋 What You'll Learn
Create a variable for the person's age
Create a variable for membership status (true or false)
Use logical operators to check if the person is at least 18 years old and is a member
Print the result as a message
💡 Why This Matters
🌍 Real World
Logical operators help apps make decisions based on multiple conditions, like checking age and membership before allowing access.
💼 Career
Understanding logical operators is essential for programming roles that involve decision-making, such as app development and software testing.
Progress0 / 4 steps
1
Create variables for age and membership
Create a variable called age and set it to 20. Create a variable called isMember and set it to true.
Swift
Hint
Use var to create variables in Swift.
2
Create a variable for minimum age
Create a constant called minimumAge and set it to 18.
Swift
Hint
Use let to create a constant in Swift.
3
Check if person can enter using logical operators
Create a variable called canEnter that is true only if age is greater than or equal to minimumAgeandisMember is true. Use the logical AND operator &&.
Swift
Hint
Use && to combine two conditions that both must be true.
4
Print the result message
Write a print statement that shows "Allowed to enter" if canEnter is true, otherwise "Not allowed to enter". Use a simple if statement.
Swift
Hint
Use if canEnter { print(...) } else { print(...) } to show the message.
Practice
(1/5)
1. Which logical operator in Swift returns true only if both conditions are true?
easy
A. == (Equality)
B. || (OR)
C. && (AND)
D. ! (NOT)
Solution
Step 1: Understand the AND operator
The AND operator (&&) returns true only when both conditions it connects are true.
Step 2: Compare with other operators
The OR operator (||) returns true if at least one condition is true, NOT (!) flips true to false and vice versa, and == checks equality, not logical combination.
Final Answer:
&& (AND) -> Option C
Quick Check:
AND = && [OK]
Hint: AND needs both true; look for && operator [OK]
Common Mistakes:
Confusing AND (&&) with OR (||)
Thinking NOT (!) combines conditions
Mixing equality (==) with logical operators
2. Which of the following is the correct syntax to negate a Boolean value isActive in Swift?
easy
A. !isActive
B. not isActive
C. isActive!
D. isActive.not()
Solution
Step 1: Recall the NOT operator syntax
In Swift, the NOT operator is written as an exclamation mark (!) placed before the Boolean variable to flip its value.
Step 2: Check each option
!isActive is the correct syntax. The other options are not valid Swift syntax for negation.
Final Answer:
!isActive -> Option A
Quick Check:
NOT = ! before variable [OK]
Hint: Use ! before variable to negate Boolean [OK]
Common Mistakes:
Writing 'not' instead of '!'
Putting ! after variable name
Using method calls like .not()
3. What will be the output of this Swift code?
let a = true
let b = false
print(a || b && !b)
medium
A. Syntax Error
B. true
C. false
D. nil
Solution
Step 1: Evaluate the NOT operator
!b means !false which is true.
Step 2: Evaluate AND operator
b && !b is false && true which is false.
Step 3: Evaluate OR operator
a || (result of AND) is true || false which is true.
Final Answer:
true -> Option B
Quick Check:
OR with true = true [OK]
Hint: Remember NOT first, then AND, then OR [OK]
Common Mistakes:
Ignoring operator precedence
Evaluating OR before AND
Misunderstanding ! operator
4. Identify the error in this Swift code snippet:
let x = true
let y = false
if x || && y {
print("Yes")
}
medium
A. Missing parentheses around conditions
B. print statement syntax error
C. Variables x and y are not Boolean
D. Incorrect use of logical operators together
Solution
Step 1: Analyze the if condition syntax
The condition uses 'x || && y' which combines OR (||) and AND (&&) operators without a proper operand between them.
Step 2: Identify the logical operator misuse
Logical operators must connect two Boolean expressions. Here, '|| &&' is invalid syntax.
Final Answer:
Incorrect use of logical operators together -> Option D
Quick Check:
Operators must connect expressions properly [OK]
Hint: Operators need operands on both sides [OK]
Common Mistakes:
Writing two operators without a condition between
Assuming parentheses fix operator misuse
Confusing variable types with operator errors
5. You want to check if a number n is between 10 and 20 inclusive in Swift. Which condition correctly uses logical operators?
hard
A. n >= 10 && n <= 20
B. n > 10 && n < 20
C. n >= 10 || n <= 20
D. !(n < 10 && n > 20)
Solution
Step 1: Understand the range condition
To check if n is between 10 and 20 inclusive, n must be greater than or equal to 10 AND less than or equal to 20.
Step 2: Evaluate each option
n >= 10 || n <= 20 uses OR which is incorrect because it allows values outside the range. n > 10 && n < 20 excludes 10 and 20 by using > and <. !(n < 10 && n > 20) negates an impossible condition and is incorrect.
Final Answer:
n >= 10 && n <= 20 -> Option A
Quick Check:
Use AND for range checks [OK]
Hint: Use && to combine lower and upper bounds [OK]
Common Mistakes:
Using OR instead of AND for range
Excluding boundary values by using > or <
Using negation incorrectly for range
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.