---------------------------------------------------------------------

next up previous contents
Next: Exception handler Up: Drawing conditional branch Previous: Simple branch

---------------------------------------------------------------------

Multiplex branch

 

To draw a multiplex branch, you normally use switch  The next example returns a sign:

    switch{
        case x>0:
            return 1
        case x=0:
            return 0
        case x<0:
            return -1
    }

tex2html_wrap2530

Like in this example, a multiplex branch has more than three branch destinations. Each condition is written with case .

If you are familiar with C language, you may prefer this style:

    switch(n%3){
        case 0:
            print Multiple of 3
        case 1:
            print Remainder is 1
        case 2:
            print Remainder is 2
    }

This is equivalent to the following:

    switch{
        case n%3=0:
            print Multiple of 3
        case n%3=1:
            print Remainder is 1
        case n%e=2:
            print Remainder is 2
    }

You can write several conditional expressions in order. You can also write processes to be executed when all conditions are false with default . At that time, no character will be prited in PAD figure.

The following program checks a number sequence and count up how many 1, single place prime numbers, and others are in the given sequence.

    {
        one = 0
        prime = 0
        others = 0
    }
    
    for(i = 1, N){
        switch(A[i]){
            case 1:
                one = one + 1
            case 2:
            case 3:
            case 5:
            case 7:
                prime = prime + 1
            default:
                others = others + 1
        }
    }

tex2html_wrap2532

You can omit the contents of case and default, thereby stating that nothing is to be done when the corresponding condition is true. In this case put only an empty line after the condition. Here is an example:

    switch{
        case x%3=0:
    
        case x%3=1:
            move(+1)
        case x%3=2:
            move(-1)
    }

This example will produce the next PAD figure:

tex2html_wrap2534

You can make a new line just before { in switch command. You can put as many spaces as you like. These rules are quite the same as those of loop and a simple branch. You have to write a branch label such as case and default in one line. So, the next example is an error.

    switch{
        case n > 0  &&
             m > 0:
                    print n*m > 0
        case n < 0: print n < 0
    }

Now, I am going to show you the way to write a multiplex branch with if tex2html_wrap_inline2516 else if tex2html_wrap_inline2518 else .

Check the next example:

    if(x>0)
        return 1
    else if(x=0)
        return 0
    else
        error
    }

This is equivalent to the next example with switch statement:

    switch{
        case x>0:
            return 1
        case x=0:
            return 0
        default:
            error
    }

tex2html_wrap2536

If you add a newline in middle of else if, it is no longer regarded as if tex2html_wrap_inline2522 else if tex2html_wrap_inline2524 else.

    if(x>0)
        return 1
    else
    if(x=0)
        return 0
    else
        error

In this case the result is like this. Notice that it has become a nesting of if tex2html_wrap_inline2526 else commands.

tex2html_wrap2538

As shown in this example, you need to be careful when using if command. When your PAD figure looks different from what you have expected, recall the contents in this section and debug your source. On contrary, you can change shapes of branches by using these command properly.

---------------------------------------------------------------------

next up previous contents
Next: Exception handler Up: Drawing conditional branch Previous: Simple branch

---------------------------------------------------------------------

Go back to pad2ps - automatic PAD drawer.
Go back to Seiichi Yoshida's Home Page.
Copyright(C) Seiichi Yoshida (comet@aerith.net). All rights reserved.
Sun Nov 10 01:36:04 JST 1996