PB之——流程控制

http://www.cnblogs.com/powerbuilder/archive/2011/11/25/2263318.html

PB 基本的流程控制语句主要有:
If .... Else
For
While
Choose Case
goto
Break
Continue

1. If
基本形式有:

复制代码
If ( i > 0 ) Then//i > 0 时的处理
ElseIf( i = 0 ) Then
    //ElseIf 非必须存在的,可以有也可以没有
Else
    //也非必须存在的
End If

If i > 0 Then
    //处理
    //这个就是没有 ElseIf ,Else 字句的
End If

If ( i > 0 ) Then // 处理
//这个语句只适用这种情况,没有end If ,且处理只有一条语句,这些语句也必须要写在后面才行,不可另起一行
复制代码

2. For
这个是pb中用的最多的循环了 ,基本形式

1
2
3
4
5
6
7
8
9
10
11
For i = 1 To 10
     //循环10次, 这里可以直接引用i ,i会自动累加1
     //其实这个 For 是有点省略的,它和 For i = 1 To 10 Step 1是等同的,也就是默认的Step 是1
     // For i = 1 To 10 Step 2 也就是每次跳2个,i取值 1 ,3,5,7,9 也就是循环5次 ,其他类似
     // For i = 10 To 1 这个是不执行的循环的
     // For i = 10 To 1 Step -1 这个就是倒序了,i的取值依次是 10,9,8,7,6,5,4,3,2,1
 
     i = 4
     //在程序中也可以改变i的值,第一次循环i = 1 ,执行到这句后i=4,第一次循环后自动i累加 i = 5
     //也就是这个循环i的取值依次是 1,5,6,7,8,9,10
Next

3. Choose Case
这个功能和If有点类似,直接看实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
i = 3
Choose Case i
     Case 1
         //如果i = 1 执行这里
     Case 2
         //如果i = 2 执行这里
     Case 3
         //如果i = 4 执行这里
     Case 4
         //如果i = 4 执行这里
     Case 10 To 20
         //如果i 取值在 [10,20] ,那么执行这里
     Case Is > 100
         //如果i > 100 执行这里
         //这里 Is 作为一个关键字出现的,他代表 Choose Case 后面的表达式
     Case Else
         //以上都没满足的就执行这里
         //这个也不是必须有的,也可以不用 Case Else
End Choose

4. Break 跳出
主要针对循环用得比较多,也就是跳出循环,例如:

For i = 1 To 10
     If i = 4 Then
         Break
     End If
Next
//i = 4时直接就跳出这个for循环了,继续执行下面的代码 ,也就是这个循环就执行了4次 ,i取值依次是1,2,3,4

5. Continue 

For循环中使用 ,跳出本次循环

1
2
3
4
5
6
7
8
9
j = 0
For i = 1 To 10
     If i = 4 Then
         Continue
     End If
     j ++
Next
//i = 4 时执行 Continue ,就跳出了本次循环,也就是不执行j++了,直接下一次循环也就是执行i=5那次循环了
//i取值一次是 1,2,3,4,5,6,7,8,9,10 。但是J++就执行了9次,i=4那次没执行 

6. Goto
跳转,可以实现执行时的跳跃,也就是现在正执行第10行代码,下次应该执行11行,只用goto就可以直接跳到20行,30行类似。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
j = 0
If i = 1 Then
     goto Retn
Else
     j ++
End If
 
j = j + 10
 
Return // 这个 return 是阻止代码向下执行,如果没有这句,那么不管i等于几 ,j的值都会是3
 
Retn:
     j = 3
 
//如果 i=1 ,那么就不执行 j = j+10 这一步了,因为直接跳过去了

7. While ,Until

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//DO UNTIL  The following DO UNTIL repeatedly executes the Beep function until A is greater than 15:
integer A = 1, B = 1
 
DO UNTIL A > 15
     Beep(A)
     A = (A + 1) * B
LOOP
 
//DO WHILE  The following DO WHILE repeatedly executes the Beep function only while A is less than or equal to 15:
integer A = 1, B = 1
 
DO WHILE A <= 15
     Beep(A)
     A = (A + 1) * B
LOOP
 
//LOOP UNTIL  The following LOOP UNTIL executes the Beep function and then continues to execute the function until A is greater than 1:
integer A = 1, B = 1
 
DO
     Beep(A)
     A = (A + 1) * B
LOOP UNTIL A > 15
 
//LOOP WHILE  The following LOOP WHILE repeatedly executes the Beep function while A is less than or equal to 15:
integer A = 1, B = 1
 
DO
     Beep(A)
     A = (A + 1) * B
LOOP WHILE A <= 15

Dropdown value should change as per another dropdown pb7

Friends,

PB 7

I am new to powerbuilder.

I have a dropdownlist box and dropdown data window in the same datawindow.

dropdowndatawindow is having grade i.e,, A,B,C,D and the dropdownlist box is holding Pass,Fail

when the user selects A or B then the dropdownlist should show pass

and when  the user select C or D then the dropdownlist should show Fail.

How can i achieve this?

thanks

There are several ways to code this in the DataWindow's ItemChanged event. Here is one way:

String ls_PassFail

If dwo.Name = 'marks_final_grade' Then

   Choose Case data   // What is the letter grade?

      Case 'A','B','C','D'

         ls_PassFail = 'Pass'

      Case Else

         ls_PassFail = 'Fail'

   End Choose

   This.SetItem(row,'marks_final_passfail',ls_PassFail)

End If

Return 0  

猜你喜欢

转载自blog.csdn.net/happymagic/article/details/79008015