This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathswitch.lcdoc
More file actions
130 lines (104 loc) · 4.57 KB
/
switch.lcdoc
File metadata and controls
130 lines (104 loc) · 4.57 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Name: switch
Type: control structure
Syntax:
switch <switchExpression>
case <caseValue>
<statementList>
[break]
[default
<defaultStatementList>]
end switch
Syntax:
switch
case <caseCondition>
<statementList>
[break]
[default
<defaultStatementList>]
end switch
Summary:
Chooses among several possible <value(glossary)|values> for an
<expression>, and <execute|executes> a set of <statement|statements>
that depends on the <value(function)>.
Introduced: 1.0
OS: mac, windows, linux, ios, android
Platforms: mobile
Parameters:
switchExpression:
Any expression.
caseValue:
Any <expression>. (If the <caseValue> evaluates to the same <value> as
the <switchExpression>, the condition is matched for that <case>
section.)
caseCondition (bool):
Any <expression> that <evaluate|evaluates> to true or false. (If the
<caseCondition> <evaluate|evaluates> to true, the condition is matched
for that <case> section.)
statementList:
Of one or more <LiveCode> <statement|statements>, and can also include
<if>, <switch>, <try>, or <repeat>
<control structure|control structures>.
defaultStatementList:
Of one or more <LiveCode> <statement|statements>.
Description:
Use the <switch> <control structure> to select among multiple possible
conditions, performing a different set of actions for each possibility.
**Form:** The <switch> <control structure> begins with the word switch
on a single line, with an optional <switchExpression>.
The <switch> line is followed by one or more <case> sections. Each
<case> section begins with the <case> <keyword>, followed by either a
<caseValue> (if a <switchExpression> was included on the <switch> line)
or a <caseCondition> (if no <switchExpression> was included). If the
<caseValue> is equal to the <switchExpression>, or the <caseCondition>
<evaluate|evaluates> to true, LiveCode begins <execute|executing> the
following <statement|statements>.
The <case> sections may be followed by an optional <default> section. If
no <break> <statement> has been encountered yet in the <switch>
<control structure>, the <statement|statements> in the <default> section
are executed.
The <switch> structure ends with an <end switch> <statement>.
Flow of control in a <switch> <control structure|structure> is less
complicated than it looks. In general, when LiveCode enters a <switch>
<control structure>, it looks for the first <case> section whose
<caseValue> is equal to the <switchExpression>, or whose <caseCondition>
is true. When a matching condition is found, all <statement|statements>
following it are executed--even statements in another <case>
section--until either a <break> <statement> is encountered or the
<switch> <control structure> ends.
This means that if you do not end a <case> section's <statementList>
with a <break> <statement>, the <statement|statements> in all the
following <case> sections (and the <default> section) are
<execute|executed> even if those <case> sections don't have a matching
<caseValue> or a true <caseCondition>. Occasionally, this behavior is
useful. However, in most cases, you should place a <break> <statement>
at the end of each <statementList>. This ensures that only one
<statementList> is <execute|executed>, and the rest are skipped.
This also means that you can attach more than one <caseValue> or
<caseCondition> to the same <statementList>, simply by placing one
<case> line above the next. The following example beeps if the
<current card> is either the last or first <card>, and goes to the
next <card> otherwise:
switch (the number of this card)
case 1
case (the number of cards)
-- both of the above case conditions execute the following
-- statements:
beep
break
default
go next card
end switch
There is no limit to the number of <case> sections you can include in a
<switch> <control structure>, although the more <case> sections there
are, the more <expression|expressions> LiveCode must <evaluate> and the
more slowly the <switch> <control structure|structure>
<execute|executes>.
>*Note:* The <switch> <control structure> is implemented internally as a
> <command> and appears in the <commandNames>.
References: switch (control structure), if (control structure),
break (control structure), repeat (control structure),
try (control structure), value (function), commandNames (function),
current card (glossary), value (glossary), execute (glossary),
statement (glossary), keyword (glossary), expression (glossary),
control structure (glossary), evaluate (glossary), command (glossary),
default (keyword), case (keyword), card (keyword), end switch (keyword)