Bird
0
0

You want to check if a number n is between 10 and 20 inclusive in Swift. Which of the following expressions correctly uses comparison operators to do this?

hard📝 Application Q15 of 15
Swift - Operators and Expressions
You want to check if a number n is between 10 and 20 inclusive in Swift. Which of the following expressions correctly uses comparison operators to do this?
An > 10 || n < 20
Bn >= 10 && n <= 20
Cn == 10 && n == 20
Dn != 10 && n != 20
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range condition

    To check if n is between 10 and 20 inclusive, it must be greater than or equal to 10 and less than or equal to 20.
  2. Step 2: Analyze each option

    n >= 10 && n <= 20 uses n >= 10 && n <= 20, which correctly checks the inclusive range. n > 10 || n < 20 uses OR which is incorrect for range. n == 10 && n == 20 checks if n equals both 10 and 20 simultaneously, which is impossible. n != 10 && n != 20 checks if n is not 10 and not 20, which does not ensure the range.
  3. Final Answer:

    n >= 10 && n <= 20 -> Option B
  4. Quick Check:

    Inclusive range uses >= and <= with AND [OK]
Quick Trick: Use && with >= and <= for inclusive range checks [OK]
Common Mistakes:
  • Using || instead of && for range
  • Checking equality to both ends at once
  • Using != which excludes values instead of range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes