An if statement is a conditional statement that changes the control flow according to a conditional outcome, true or false. As per Fig.55 and Fig.56, the illustration shows two IF statements to evaluate Symbol price against the moving average MA. Depending on the answer from the conditional statement, the program will execute action accordingly.

A flowchart of IF statements
Fig.55 A flowchart of IF statements
Same conditional statements as Fig.55 in code
Fig.56 Same conditional statements as Fig.55 in code

Anatomy of an IF statement

Anatomy of an IF statement
Fig.57 Anatomy of an IF statement
  1. An IF statement is composed of an if followed by a conditional statement that compares values or variables with logical operators. The conditional statement must be between parentheses.
  2. Curly brackets are required if more than one operation is performed.
Note:

  • An if conditional statement such as a comparative instruction should always be written between parentheses.
  • Curly brackets are not mandatory to be used if there is only one instruction given in case the condition is true.
  • Conversely, curly brackets must be used to delimit code executed if there is more than one instruction or operation (Fig.58).

In the Fig.58, instruction_one() and instruction_two() will be executed if Condition is met while instruction_three() will be executed by the programme regardless of the Condition.

Full If-Statement format with curly brackets to delimit more than one instruction
Fig.58 Full If-Statement format with curly brackets to delimit more than one instruction

Fig.59 demonstrates that without the use of the curly brackets, the programme will only execute the first instruction in the code sequence if the if-condition is true. In this example, instruction_one() will be called if Condition is met. The function instruction_two() and instruction_three() will be called irrespective of the Condition.

If-Statement format without curly brackets
Fig.59 If-Statement format without curly brackets

Fig.60 demonstrates a code sample that returns the same result as Fig.59, but with an if-statement written on one line of code. However, for best practice, it is recommended to provide space between block code to improve readability.

Same if Statement that Fig.59 with if-statement and instruction on the same line of code
Fig.60 Same if Statement that Fig.59 with if-statement and instruction on the same line of code

Fig.61 is an example of nested if-statements. A nested structure is basically a multi-level conditional test. Note the curly brackets that delimit the first if-statement to test the first Condition, which if true, instruction_one() and instruction_two() will be executed. For instruction_three() to be called, an additional condition must be met, Condition_two and Condition_three must be true.

Nested if-Statement
Fig.61 Nested if-Statement
Coding tips:

To simplify the code, an if-Statement can be nested into several nested structures which allow building complex algorithms such as demonstrated in Fig.61.

 

 

Leave a Reply

3  +  2  =