Branches

Read(4713) Label: branch program,

With branch statements, one or multiple conditions will be checked to decide whether or not to switch over to a new instruction, or what will be returned, depending on the checking result. In esProc, the if/else statement is used to realize the branching. It has the following forms:

2.1.1 if x … else … in the same line

When condition x after if is true, the statement after the condition will be executed; otherwise the statement after else will be executed. The else part can be omitted. else and if must be in the same line. There isn’t necessarily an else part. After execution, the value of the cell in which if is located is the result of x.

 

A

B

C

D

1

-14

 

 

 

2

if A1>=0

>A3=A1

else

>A3=-A1

3

 

 

 

 

A2’s result is as follows:

Since A2’s result is false, the else part, that is, the code in D2, will be executed. After execution A3’s result is as follows:

It is the absolute value of the number in A1.

Sometimes, there is no else part. The statement only decides whether the code after if statement should be executed:

 

A

B

C

1

=demo.query("select * from EMPLOYEE")

 

 

2

for A1

 

 

3

 

if age(A2.BIRTHDAY)>=40

>A4=A4+1

4

 

 

 

A2 loops through each employee record; B3 checks if the current employee is older than or equal to 40. If the result is true, the employee will be counted in A4, where you get the number of all employees whose ages are not less than 40 when the loop is over.

Following logical connectors are used in the conditional statements:

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.

 

A

B

C

1

=demo.query("select * from EMPLOYEE")

 

 

2

for A1

 

 

3

 

if age(A2.BIRTHDAY)>=40 && A2.GENDER=="M"

>A4=A4+1

4

 

 

 

A4 gets the number of male employees whose ages are not less than 40:

2.1.2 if( x , a , b ) function

If x is true, expression a will be computed and result be returned; otherwise, expression b will be computed and result be returned.

Sometimes, you can replace the previously mentioned if…else… statement with if function to have more concise code.

 

A

1

-14

2

=if(A1>=0,A1,-A1)

A2’s result is the absolute value of A1:

2.1.3 if x … else … in the code block

If x is true, if code block will be executed; otherwise, the else code block will be executed. else and if must be in the same column. The else part can be absent. esProc employs code block, instead of symbols such as {} or statements like end if, to determine a statement’s work scope.

 

A

B

C

1

=demo.query("select * from EMPLOYEE")

 

 

2

Male

[]

0

3

Female

[]

0

4

for A1

if A4.GENDER=="M"

>B2=B2|A4.(NAME+" " +SURNAME)

5

 

 

>C2+=1

6

 

else

>B3=B3|A4.(NAME+" " +SURNAME)

7

 

 

>C3+=1

As shown in the example, depending on the judging result of B4, the if code block composed of C4 and C5 and the else code block composed of C6 and C7 are executed alternatively to find out respectively the names and numbers of male and female employees. Results can be viewed in B2, B3, C2 and C3 after the computations finish:

 

 

2.1.4 Multiple blocks of if x … else if y …

This is esProc multi-branch statement, which can be written repeatedly forever. The if … else if… statement must be written along one column. You must always remember that there is no statement in esProc which can be thought of as the counterpart of end if statement. esProc employs the scope of code block to determine where an if statement ends.

 

A

B

C

1

4

*

7

2

if B1=="+"

 

 

3

 

>B12=A1+C1

 

4

else if B1=="-"

 

 

5

 

>B12=A1-C1

 

6

else if B1=="*"

 

 

7

 

>B12=A1*C1

 

8

else if B1=="/"

 

 

9

 

>B12=A1/C1

 

10

else

 

 

11

 

>B12="Error"

 

12

Result:

 

 

A1, B1 and C1 combine to form an expression, which is computed in B12. The result is as follows:

2.1.5 Nested branch statements

One or more branch statements can be nested into an if or else code block to make further judgment.

 

A

B

C

1

2016-2

 

 

2

=A1.split@p("-")

>year=A2(1)

>month=A2(2)

3

if [1,3,5,7,8,10, 12].pos

(month)>0

>A8=31

 

4

else if [4,6,9,11].pos

(month )>0

>A8=30

 

5

else if month==2

if year%400==0

>A8=29

6

 

else if year%100!=0 && year%4==0

>A8=29

7

 

Else

>A8=28

8

 

 

 

A string of year and month delimited by “-” is typed in A1. The following code computes the number of days in this month. The computation first finds which month it is. If it is February, then find out whether it is a leap year or not. A8’s result is as follows: