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

next up previous contents
Next: Multiplex branch Up: Drawing conditional branch Previous: Drawing conditional branch

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

Simple branch

To draw a simple branch, you use if tex2html_wrap_inline2486 else . But notice that using if tex2html_wrap_inline2488 else  statement does not guarantee that the output will be a simple branch; it may be possible that the output becomes a multiplex branch. For more information, refer to section3.6.2.

The simplest conditional branch is one that has only if. This example is a program to return an absolute value.

    if(x < 0)
        x = -x

tex2html_wrap2502

By using else you can write process to be executed when condition is false. The next example calculate a square root, and it will print an imaginary unit i if the number is negative:

    if(x >= 0)
        print sqrt(x)
    else{
        print sqrt(-x)
        print 'i'
    }

tex2html_wrap2504

Finally, I am going to explain valid and invalid syntax of if tex2html_wrap_inline2500 else statement.

If true part or false part contains only one command, you do not have to enclose it with { }   as in the first example.

However, these are both illegal:

    if(x < 0)  x = -x
    if(x < 0){  x = -x  }

You can omit both true part and false part. Hence, the following example is OK:

    if(x >= 0){
    }else{
        x = -x
    }
    
    if(x < 0){
    }else{
    }

You can insert a newline code before { and after }, and you can put as many spaces as you like. So, the next example is valid.

    if  (  x >= 0  )
    {
        print sqrt(x)
    }
    else
    {
        print sqrt(-x)
        print 'i'
    }

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

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