Conditional Structures

Introduction to Conditional Structures

In pseudocode, there are two types of conditional structures that are utilised, being IF statements and CASE statements.

IF Statements

IF statements utilise a specific set of keywords that need to be included, being IF, THEN, ENDIF.

IF <condition> THEN
    <code to be executed>
ENDIF

E.g:

IF num1 = 3 THEN
    OUTPUT "num1 is 3"
ENDIF

IF statements can also include an ELSE clause to be able to branch into other code depending on if the IF statement has passed or not.

IF <condition> THEN
    <code to be executed>
ELSE
    <code to otherwise be executed>
ENDIF

E.g:

IF num1 = 3 THEN
    OUTPUT "num1 is 3"
ELSE
    OUTPUT "num1 isn't 3"
ENDIF

Nested IF statements can also be used, however, care must be taken with inserting ENDIFs into the correct locations. An example of this would be:

CASE Statements

CASE statements are where multiple branches of code can be executed, depending on the value of the variable given to the statement. They follow the keywords CASE OF, ENDCASE.

E.g:

An OTHERWISE clause can be added, which acts similarly to an ELSE clause. It must always be the last case.

E.g:

Last updated

Was this helpful?