Conditions and comparison operators

In many programming and scripting languages, conditions and comparison operators are fundamental concepts used to make decisions and control the flow of execution. Here’s an overview of common conditions and comparison operators found in various languages, including Bash, Python, and SQL.

1. Bash (Shell Scripting)

Conditions:

  • Conditions in Bash are typically used with if, while, and until statements.

  • The test command or [ and ] are used to evaluate conditions.

Comparison Operators:

  • String Comparison:

    • =: Check if two strings are equal.

    • !=: Check if two strings are not equal.

    • <: Check if one string is less than another (in alphabetical order).

    • >: Check if one string is greater than another (in alphabetical order).

  • Numeric Comparison:

    • -eq: Equal to.

    • -ne: Not equal to.

    • -lt: Less than.

    • -le: Less than or equal to.

    • -gt: Greater than.

    • -ge: Greater than or equal to.

Example:

2. Python

Conditions:

  • Conditions in Python are used with if, elif, and else statements.

  • Python uses boolean expressions and relies on indentation for blocks of code.

Comparison Operators:

  • ==: Equal to.

  • !=: Not equal to.

  • <: Less than.

  • <=: Less than or equal to.

  • >: Greater than.

  • >=: Greater than or equal to.

Example:

3. SQL

Conditions:

  • SQL uses conditions in WHERE, HAVING, and CASE statements to filter records and perform conditional logic.

Comparison Operators:

  • =: Equal to.

  • <> or !=: Not equal to.

  • <: Less than.

  • <=: Less than or equal to.

  • >: Greater than.

  • >=: Greater than or equal to.

  • BETWEEN: Range check.

  • LIKE: Pattern matching.

  • IN: Check if a value is in a list of values.

  • IS NULL: Check for NULL values.

Example:

Summary

  • Bash: Uses test command or [ ] with -eq, -ne, -lt, -le, -gt, -ge for numeric comparisons and =, !=, <, > for string comparisons.

  • Python: Uses ==, !=, <, <=, >, >= for both numeric and string comparisons.

  • SQL: Uses =, <>, !=, <, <=, >, >=, BETWEEN, LIKE, IN, and IS NULL for comparisons and conditions.

Understanding these operators and how to use them in different contexts is crucial for writing effective conditional statements and performing comparisons in your scripts and queries.

Last updated