
4.1.2 Flow Decision
Flow Decision也是UiPath提供的分支结构活动之一。和If活动一样,Flow Decision也是根据Condition中布尔表达式返回的结果不同,选择执行不同的逻辑。与If活动不同的是,If活动既可用于序列,又可用于流程图中,而Flow Decision只能用于流程图中。Flow Decision活动在流程图中以连线的方式连接两个不同条件的分支活动,如图4-11所示。

图4-11 Flow Decision活动条件分支示意图
Flow Decision属性可以通过其属性面板查看与设置,如图4-12所示。详细属性说明如表4-1所示。

图4-12 Flow Decision属性面板
表4-1 具体属性说明

【例4.2】请用户输入一个年份,然后系统判断用户输入的年份是否为闰年。具体判断条件如下:
□普通年能被4整除且不能被100整除为闰年,如2004年是闰年,1901年不是闰年;
□世纪年能被400整除的是闰年,如2000年是闰年,1900年不是闰年。
具体实现步骤如下所示。
1)进入Studio界面,点击Process创建一个流程,命名为4_2_FlowDecision,如图4-13所示。
2)进入Main,在Activities面板的搜索框内输入flow chart,拖入一个FlowChart,如图4-14所示。

图4-13 新建流程

图4-14 活动面板搜索FlowChart
3)选中第2步拖入的FlowChart,然后在Variables面板内新建一个变量inputYear,类型为Int32,如图4-15所示。

图4-15 新建inputYear变量
4)双击第2步拖入的FlowChart,进入FlowChart视图内,按第2步的搜索方式在Activities面板的搜索框内输入input dialog,并将Input Dialog活动拖入FlowChart内,然后由Start连线至Input Dialog活动,如图4-16所示。

图4-16 拖入Input Dialog活动
5)点击Input Dialog,查看其属性面板。将Label属性值改为“"请输入年份"”,Title属性值改为“"录入框"”,Result属性值选择第3步建立的变量inputYear,如图4-17所示。请留意,只要是已经申明过的变量,一定是能选择出来的,否则有可能是由于在建立变量时没有选择合适的作用范围Scope。
6)拖入一个Flow Decision,并由Input Dialog连线至Flow Decision,如图4-18所示。

图4-17 设置Input Dialog活动属性

图4-18 拖入Flow Decision活动
7)点击Flow Decision,查看其属性,按图4-19所示进行更改。

图4-19 设置Flow Decision属性
8)拖入一个Message Box,并将其连接到Flow Decision的闰年分支,如图4-20所示。

图4-20 设置True分支-闰年
9)选中Message Box,查看其属性并按图4-21所示进行更改。
10)按照第8步的操作,再拖入一个Message Box连接至Flow Decision的非闰年分支,将其Text属性改为“inputYear.ToString+"是非闰年"”,DisplayName属性改为“非闰年”,如图4-22所示。

图4-21 设置True分支的Message Box属性

图4-22 设置False分支的Message Box属性
11)最后流程图的整体效果如图4-23所示。

图4-23 整体流程示意图
12)点击Run File(快捷键Ctrl+F6)执行程序,在对话框中输入1900,点击OK按钮,查看结果如图4-24所示。

图4-24 执行结果