The if
statement is a fundamental control structure in Tipcontrol and similar to languages such as C++. It allows you to execute certain blocks of code conditionally, based on the evaluation of an expression.
Structure of an if
Statement:
Components:
- Condition: An expression evaluated to determine whether it is
true
(non-zero) orfalse
(zero). - Code Block: The block of code that executes if the condition evaluates to
true
. This block is enclosed within curly braces{}
.
Optional else
Clause
You can extend the if
statement with an else
clause to specify code to execute when the condition is false
:
Simple Example
number = 10;
// Check if the number is positive, negative, or zero
if (number > 0) {
lib.log(“The number is positive.”);
} else if (number < 0) {
lib.log(“The number is negative.”);
} else {
lib.log(“The number is zero.”);
};
Explanation:
- Condition:
number > 0
checks ifnumber
is greater than zero. - Nested checks:
else if
provides additional conditional checks when the previousif
condition isfalse
. else
Clause: Executes if none of the preceding conditions aretrue
.