if

X3 programming language, the "if" keyword is used for conditional execution. It allows you to execute a block of code only if a specified condition evaluates to true. Here's how you would use the "if" keyword in your X3 language:

Basic Usage:

var x = 10

# Using 'if' to check a condition
if x > 5 then
    show("x is greater than 5.")
end

Explanation:

  • In this example, the "if" statement checks if the variable x is greater than 5.

  • If the condition x > 5 evaluates to true, the code inside the "if" block (in this case, printing "x is greater than 5") will be executed.

Else Clause:

var x = 3

# Using 'if' with 'else' to handle alternate case
if x > 5 then
    show("x is greater than 5.")
else
    show("x is not greater than 5.")
end

Explanation:

  • Here, if the condition x > 5 is false, the code inside the "else" block will be executed.

Elif Clause (Else If):

var x = 5

# Using 'if' with 'elif' to check multiple conditions
if x > 5 then
    show("x is greater than 5.")
elif x == 5 then
    show("x is equal to 5.")
else
    show("x is less than 5.")
end

Explanation:

  • In this example, if the first condition x > 5 is false, the "elif" clause (x == 5) will be checked.

  • If x is neither greater than 5 nor equal to 5, the code inside the "else" block will be executed.

Nested 'if' Statements:

var x = 10
var y = 5

# Using nested 'if' statements
if x > 5 then
    if y > 2 then
        show("Both x and y are greater than their respective thresholds.")
    end
end

Explanation:

  • In this example, the "if" statement inside another "if" statement will only be executed if both conditions (x > 5 and y > 2) are true.

By incorporating the "if" keyword in your X3 language, you provide users with the ability to control the flow of their programs based on specified conditions, enabling more dynamic and responsive behavior.

Last updated