Less than or greater than in the Swift switch statement


In Swift, there are many comparison operators to perform calculations and check different conditions. Less than or greater than operators are used to checking conditional statements. In this article, let's see how to use them with switch and if statements.

Less Than or Greater Than the Switch Statement

In Swift, you can use the case ..< and case ... syntax to define ranges in a switch statement.

The case ..< syntax is used to define a range that includes all values greater than or equal to the first value and less than the second value

  • Step 1 − If the value is less than 3, the first case will be executed.

  • Step 2 − If the value is between 3 and 5 (inclusive), the second case will be executed.

  • Step 3 − If the value is greater than or equal to 6, the default case will be executed.

Example

import Foundation
let value = 5
switch value {
case ..<3:
   print("Less than 3")
case 3..<6:
   print("Between 3 and 5")
default:
   print("Greater than or equal to 6")
}

Output

Between 3 and 5

The case ... syntax is used to define a range that includes all values between the first and second values, inclusive:

  • Step 1 − If the value is less than or equal to 5, the first case will be executed.

  • Step 2 − If the value is between 6 and 8 (inclusive), the second case will be executed.

  • Step 3 − If the value is greater than 8, the default case will be executed.

Example

import Foundation
let value = 7
switch value {
case ...5:
   print("Less than or equal to 5")
case 6...8:
   print("Between 6 and 8")
default:
   print("Greater than 8")
}

Output

Between 6 and 8

Checking if a Number is Positive, Negative, or Zero

  • Step 1 − If the number is less than 0, the first case will be executed and print "Negative".

  • Step 2 − If the number is exactly 0, the second case will be executed and print "Zero".

  • Step 3 − If the number is greater than or equal to 1, the third case will be executed and print "Positive".

  • Step 4 − If the number is not a valid number (e.g. it's a string), the default case will be executed and print "Not a valid number".

Example

import Foundation
let number = -3
switch number {
case ..<0:
   print("Negative")
case 0:
   print("Zero")
case 1...:
   print("Positive")
default:
   print("Not a valid number")
}

Output

Negative

Checking if a Number is in a Specific Range

  • Step 1 − If the number is between 0 and 4 (inclusive), the first case will be executed and print "Number is between 0 and 4".

  • Step 2 − If the number is between 5 and 9 (inclusive), the second case will be executed and print "Number is between 5 and 9".

  • Step 3 − If the number is between 10 and 14 (inclusive), the third case will be executed and print "Number is between 10 and 14".

  • Step 4 − If the number is outside of the valid range (e.g. it's negative or greater than 14), the default case will be executed and print "Number is outside of the valid range".

Example

import Foundation
let number = 10
switch number {
case 0..<5:
   print("Number is between 0 and 4")
case 5..<10:
   print("Number is between 5 and 9")
case 10..<15:
   print("Number is between 10 and 14")
default:
   print("Number is outside of the valid range")
}

Output

Number is between 10 and 14

Less Than or Greater Than the if Statement

Checking if a number is positive, negative, or zero −

  • Step 1 − The comparison operators < and == are used to compare numbers to 0.

  • Step 2 − If the number is less than 0, the first branch of the if statement is executed, and "Negative" is printed.

  • Step 3 − If the number is equal to 0, the second branch of the if statement is executed, and "Zero" is printed.

  • Step 4 − If the number is greater than 0, the third branch of the if statement is executed, and "Positive" is printed.

Example

import Foundation
let number = -3
if number < 0 {
   print("Negative")
} else if number == 0 {
   print("Zero")
} else {
   print("Positive")
}

Output

Negative

Checking if a Number is in a Specific Range

  • Step 1 − The comparison operators >= and < are used to compare numbers to the endpoints of ranges. The && operator is used to combine multiple conditions into a single expression.

  • Step 2 − The first branch of the if statement is executed if the number is between 0 and 4 (inclusive), and the second branch is executed if the number is between 5 and 9 (inclusive).

  • Step 3 − The third branch is executed if the number is between 10 and 14 (inclusive), and the last branch is executed if the number is outside the valid range.

Example

import Foundation
let number = 10
if number >= 0 && number < 5 {
   print("Number is between 0 and 4")
} else if number >= 5 && number < 10 {
   print("Number is between 5 and 9")
} else if number >= 10 && number < 15 {
   print("Number is between 10 and 14")
} else {
   print("Number is outside of the valid range")
}

Output

Number is between 10 and 14

Conclusion

In conclusion, Swift typically uses comparison operators such as, =, >, >=, ==, and!= to compare data and make judgments based on those comparisons They can be used in switch statements, but they may also be used in other control flow statements to make choices depending on comparison results, such as if, else if, and else statements. These operators allow us to build more intricate and adaptable logic into our programs.

Updated on: 04-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements