July 24, 2014

The Branch Statement in esProc

In branch statement, we need to check one or multiple conditions, execute different code according to the results or return different results. In esProc, the branch statement if/else has the following form:

1. if x … else … at the same line
When condition x after if is true, execute the statement after it. Otherwise execute the statement after else. The else part can be omitted, else and if must be in the same row. After execution, the value of the cell in which if is located is the computed result of x.
 

Sometimes, there is no else part, we only need to decide whether the code after if statement should  be executed:
The below logical connector can be used in the judgment state ments:


  • a&&b

For the "a and b", the result is true only on condition that both a and b are true.

  • a||b

For the "a or b", the result is true only on condition that either a or b is true.

  • !a

For the "not a", the result is true only on condition that a is false.


2. Function if(x,a,b)
When x is true, compute expression a and return the result; otherwise, compute expression b and return the result. Sometimes, the previously mentioned if…else… code can be replaced by function if in order to make the statement more concise.
 

3. if x … else … in the code block
If x is true, then execute the code block of if; otherwise, execute the code block of else. You can certainly ignore the else part. else and if must be in the same column. Different from some other program languages, esProc employs code block, in stead of symbols such as {} or statements like end if, to determine the statement’s valid scope.


As shown in the example, code block of if, or code block of else, is executed alternatively based on the judging results of B4 to compute the name list and total number of male employees, as well as those of female employees. Results can be seen in B2, B3, C2 and C3 after the computations are over:
 
4. Multiple blocks of if x … else if y …
This is code for multi-branch statement in esProc, which can be written repeatedly forever. If x is true, then execute the code block of if; otherwise, if y is true, execute the code block of else if…., else and if must be in the same cell. Again the point is emphasized that there is no such corresponding statement of end if, and esProc employs the scope of code block to determine when if statement will be over.


5. The Nested Branch Statement
Branch statement can be used to make further judgment in the code block of if or else.
 
In the above example, a character string of year and month, with a separator "-", is input in A1, and the code after it computes the total days of the month. The computation should first judge which month it is. For February, further judgment is needed to determine whether it is a leap year. The computed result in A8 is as follows:
 

No comments:

Post a Comment