Skip to main content

if

The if statement is used for conditional execution of code blocks. It allows your program to make decisions and execute different code paths based on whether conditions are true or false.

Basic If Statement

Simple conditional execution:

If-Else Statement

Execute different code blocks based on condition:

If-Else If-Else Chain

Multiple conditions with different outcomes:

Nested If Statements

If statements within other if statements:

Complex Conditions

Using logical operators in conditions:

If with Function Calls

Using function results in conditions:

If with Comparisons

Different comparison operators:

If with Null Checks

Checking for null or undefined values:

If with Array/List Conditions

Checking array properties:

If with Object Property Checks

Checking object properties:

If with Assignment

Assigning values based on conditions:

If with Early Returns

Using if statements for early function returns:

If with Error Handling

Conditional error handling:

If with Range Checks

Checking if values are within ranges:

If with Type Checking

Conditional logic based on types:

If with Pattern Matching

Using if statements for pattern matching:

If with Guard Clauses

Using if statements as guard clauses:

Best Practices

  1. Use clear conditions: Make conditions easy to understand
  2. Avoid deep nesting: Use early returns or guard clauses
  3. Use meaningful variable names: Make conditions self-documenting
  4. Handle edge cases: Consider null/undefined values
  5. Use appropriate comparison operators: Choose the right operator for the context
The if statement is fundamental to controlling program flow and making decisions in Soul applications. Use it wisely to create clear, maintainable conditional logic.