Introduction to PowerBuilder

HomePrevious Lesson: Scripting Structures
Next Lesson: DO WHILE...LOOP

IF

The IF statement is the fundamental control statement that allows PowerScript to make decisions, or, more precisely, to execute statements conditionally. The statement has five forms. The first is:
IF Expression THEN
   Statement
END IF

In this form, the expression is evaluated. If the resulting value is true or can be converted to true, statement is executed. If the expression is false or converts to false, statement is not executed. For example:
Int li_position
�
�
IF li_position > 100 THEN
   li_position = 1
END IF

As mentioned above, we can always replace a single statement with a statement block. So the IF statement might also look like this:
IF ls_address = "" THEN
   SetNull(ls_address)
   MessageBox("Error", "Pl. specify address!")
END IF

The indentation used in these examples is not mandatory. Extra spaces and tabs are ignored in PowerScript, and if we separate each statement by semicolon, these examples could be written all in one line as shown below, if we wanted to.
IF ls_address = "" THEN; SetNull(ls_address); MessageBox( "Error", "Pl. specify address!"); END IF
// Above code is shown in 2 lines b'use of textbook width.
// In reality it is one line only. 

Using line breaks and indentation, however, makes the code easier to read and understand. The second form of the if statement introduces an ELSE clause that is executed when the expression is false. Its syntax is:
IF Expression THEN
   Statement1(s)
ELSE
   Statement2(s)
END IF

In this form of the statement, the expression is evaluated and if it is true, Statetement1 is executed; otherwise, statement2 is executed. For example:
// Object: cb_right
// Event: Clicked
IF cb_left.visible THEN
   cb_left.Show()
   cb_right.text = "&Show"
ELSE
   cb_left.Hide()
   cb_right.text = "&Hide"
END IF

You can make changes to the statement, substituting specific variables, test conditions and so on. In the previous section, we wrote code to hide cb_left CommandButton, and there was no code to display it back. Change the script for cb_right's clicked event as shown above and run the application and click on the cb_right button multiple times and see what happens.

We've seen that the IF/ELSE statement is useful for testing a condition and executing one of two pieces of code, depending on the outcome. But what about when we need to execute one of many pieces of code? One way to do this is with and ELSEIF statement. ELSEIF is not really a PowerScript statement itself, it is part of the IF statement.
IF Expression THEN
   Statement1(s)
ELSEIF Expression THEN
   Statement2(s)
ELSEIF Expression THEN
   Statement3(s)
ELSE
   Statement4(s)
END IF

There is nothing special about this code. It is just a series of IF statements. Using the ELSEIF is preferable to, and more legible than, writing these statements out in their syntactically equivalent fully nested form:
IF Expression THEN
   Statement1(s)
ELSE
   IF Expression THEN
      Statement2(s)
   ELSE
     IF Expression THEN
        Statement3(s)
     ELSE
        Statement4(s)
     END IF
   END IF
END IF

Another form of IF statement doesn't need to be terminated with END IF as long as the statement block has one statement. For example:
IF cb_left.Visible Then cb_left.Visible = Flase ELSE &
    cb_left.Visible = True
HomePrevious Lesson: Scripting Structures
Next Lesson: DO WHILE...LOOP