Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

Writing Code that Branches

REALbasic provides two tools to let you create branching code—that is, code that executes one set of statements or another, depending on some condition that is assessed at run time. These tools are the If structure and the Select Case structure.

The If structure

The If structure spans several lines of code and involves some or all of the keywords If, Then, Else, ElseIf, and End If. The keywords bracket tests and blocks of code to execute based on the tests. The If structure uses a Boolean value in its tests to decide which branch of code to execute (Figure 4.33).

Figure 4.33Figure 4.33 The If structure.


To branch code in one of two directions:

  • Use the If-Then-Else version of the If structure:

    If [Boolean condition] Then
      [first block of statements] 
    Else
      [second block of statements] 
    End If

    If the Boolean condition evaluates to true, the first block of statements will be executed; otherwise, the second block will be executed.

Writing Code that Branches

To execute one block of code conditionally:

  • Use the If-Then version of the If structure:

    If [Boolean condition] Then
      [block of statements] 
    End If

    If the Boolean condition evaluates to true, the block of statements will be executed; otherwise, execution will continue with the next statement after End If.

To branch code in any of several directions:

  • Use the If-Then-ElseIf version of the If structure:

    If [Boolean condition 1] Then
      [first block of statements] 
    ElseIf [Boolean condition 2] Then
       [second block of statements] 
    ElseIf [Boolean condition 3] Then
       [third block of statements] 
    Else
      [last block of statements] 
    End If

    If Boolean condition 1 evaluates to true, the first block of statements is executed. Otherwise, each successive Boolean condition is evaluated in the same way, and as soon as one is found that evaluates to true, the block of code following it is executed. Any number of EndIfs can be used in an If structure.

To perform complex branching of code:

  • Place one or more If structures inside another If structure:

    If [Boolean condition 1] Then 
      If [Boolean condition 2] Then
        [first block] 
      Else
        [second block] 
       End If 
    Else
       [third block] 
    End If

    If Boolean condition 1 evaluates to true, the second If structure is evaluated, resulting in the execution of the first or second block of code. If the first condition evaluates to false, the third block is executed.

The Select Case structure

The Select Case structure does what the If structure does but often produces clearer code. Instead of the Boolean test used by the If structure, the Select Case structure uses an integer or string value to choose among several possibilities (Figure 4.34).

Figure 4.34Figure 4.34 The Select Case structure.


To branch code in one of several directions with the Select Case structure:

  • Follow this pattern:

    Select Case [test expression] 
    Case [first expression]
       [first block of statements] 
    Case [second expression]
       [second block of code] 
    Else
       [last block of statements] 
    End Select

    The test expression is evaluated; then each other expression is evaluated in turn until one is found that matches the test expression. The block of statements following the first matching expression is executed, and execution continues with the next statement after End Select. If no expression matches the test expression, any statements following the Else are executed.

  • + Share This
  • 🔖 Save To Your Account