LotusScript基本语法知识……选择和循环语句

1.if语句

if condition1 then

   statement1

elseif condition2 then

  statement2

else

  statement3

end if

2.select case语句

select case  selectexpr

Case conditionList
  Statements
Case conditionList
  Statements
  …
Case Else
Statements
End Select

3.for语句
for conntvar=first to Last [Step increment]
statements
next [countvar]

4.while语句
While condition
  Statements
Wend

5.Do While/until Loop语句
永远循环
Do
Statements
Loop
先检查条件,再循环
Do while condition
  Statements
Loop
Do until condition
  Statements
Loop
先循环,后检查条件
Do
  Statements
Loop while condition
Do
  Statements
Loop until condition

6. Forall
ForAll refVar in container
  statements
End ForAll
例子:
Sub Click(Source As Button)
Dim short(5) As Integer 
Forall x In short
  x=1
End Forall
End Sub
结果:给short数组的每个元素赋值

中途退出循环
Exit LoopType
说明:looptype: for, while, do

子事例、函数、声明、作用域

1. 子事例
子事例也就是子过程,在编程窗格中单击一个按钮的click子事例就会打开如下click子事例:
Sub Click(Source As Button)
messagebox “hello word!”
End Sub
你可以在这个子事例中写出代码,如messagebox “hello word!” ,这是系统默认建立的一个单击子事例,同样的你可以建立自己的子事例如下:
Sub Click(Source As Button)
messagebox “hello word!”
  dim name=”lotus script”
  OutputName(name) ‘调用子事例OutputName
End Sub
Sub OutputName(name as string) ‘建立的新子事例,功能是输出参数name的值
Messagebox name
Sub
  输出结果为:弹出窗口lotus script
子事例是没有返回值的,函数的使用和子事例差不多,但是函数有返回值。如果想在子事例中返回一个值的话可以定义一个全局变量,然后给这个变量赋值就能达返回值的功能,如何定义一个全局变量将在作用域中讲到。


2. 函数
程序员都知道函数的作用,我就不多说了,这里只说明一下定义和使用的格式
Sub Click(Source As Button)
Dim a As Integer 
Dim b As Integer 
Dim c As Integer 

a=3
b=4
c=Sun(a,b) '使用求和函数得到a和b 的和
Messagebox Cstr(a)+"+"+Cstr(b)+"="+Cstr(c)
End Sub
Function Sun(a As Integer ,b As Integer ) As Integer '定义函数Sun, 功能是返回两个参数的和
c=a+b
sun=c '给函数名赋值就是这个函数的返回值。
End Function 
输出结果:弹出对话框a+b


3. 控制变量是否在需要声明才有效
如果用户在脚本的options部分中设置了option declare, 那么就一定要声明所有的变量,不管它们是什么类型。默认是可以不声明的,如下例:
例1 (没有设置 option declare)
Sub Click(Source As Button)
TestVar=”hello word !”
Messagebox TestVar
End Sub
输出结果为:(弹出对话框)hello word!
例2 (设置了 option declare)
Option Declare
Sub Click(Source As Button)
TestVar=”hello word !”
Messagebox TestVar
End Sub
在保存的时候就会出现错误:Variable not declared:TestVar

4. 作用域
LotusScript的作用域分为三个,从小到大分别为:子事例或函数、对象(如按钮、域等)、窗体(如表单、视图等)
  (1)子事例或函数
  如果在子事例或函数中定义的变量只能在此子事例或函数中使用,在另外一个是不能用的,如下:
  Sub Click(Source As Button)
Dim TestVar As String
Testvar="hello word!"
End Sub
Sub outputStr
Messagebox testvar
End Sub
输出结果:弹出对话框为无值
因为TestVar是在Click子事例中定义的,所以只能在Click子事例中使用,在OutputStr子事例中是不起作用的。

(2) 对象(如按钮、域等)
  在对象中定义的变量中能在此对象中使用,包括这个对象的所有子事例,在另外一个对象中是不能用的,如下:
  Dim TestVar as String ‘在(Declarations)中定义
  Sub Click(Source As Button)
  Testvar="hello word!"
End Sub
Sub outputStr
  Messagebox testvar
End Sub
输出结果:弹出对话框hello word!
因为TestVar是在对象中定义的,所以在这个对象中的所有子事例或函数都起作用。


  (3) 窗体(如表单、视图等)
在窗体中定义的变量可以在这个窗体中的任何对象中使用,如下:
‘(Globals)test 窗体 
Dim TestVar as String

Button1(按钮)
  Sub Click(Source As Button)
  Testvar="hello word!"
End Sub

‘Button2(按钮)
  Sub Click(Source As Button)  
  Messagebox Testvar
End Sub

  执行操作,先按Button1,再按Button2 
  输出结果为:弹出窗口hello word!
  因为TestVar是在test窗体中定义的,所以对这个窗体中的所有对象都是起作用的,Button1和Button2都是这个窗体的两个对象,先按Button1是给TestVar赋值,再按Button2是输出TestVar的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值