short-circuit_20evaluation
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| short-circuit_20evaluation [2018/03/31 13:19] – external edit 127.0.0.1 | short-circuit_20evaluation [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| //by Richard Russell, December 2009//\\ \\ Consider a compound conditional test such as the following: | //by Richard Russell, December 2009//\\ \\ Consider a compound conditional test such as the following: | ||
| + | <code bb4w> | ||
| IF condition1 AND condition2 AND condition3 THEN | IF condition1 AND condition2 AND condition3 THEN | ||
| + | </ | ||
| It can be seen that if **condition1** is FALSE then the entire expression must be FALSE, irrespective of the results of the other conditions. Ideally, in such a case it would be better not to waste time evaluating **condition2** and **condition3** because they cannot affect the overall outcome. Similarly if **condition2** is false, ideally you shouldn' | It can be seen that if **condition1** is FALSE then the entire expression must be FALSE, irrespective of the results of the other conditions. Ideally, in such a case it would be better not to waste time evaluating **condition2** and **condition3** because they cannot affect the overall outcome. Similarly if **condition2** is false, ideally you shouldn' | ||
| + | <code bb4w> | ||
| IF condition1 IF condition2 IF condition3 THEN | IF condition1 IF condition2 IF condition3 THEN | ||
| + | </ | ||
| Here the **AND**s have simply been changed to **IF**s. If **condition1** is FALSE neither **condition2** nor **condition3** is evaluated. If **condition1** is TRUE but **condition2** is FALSE, **condition3** is not evaluated.\\ \\ A similar situation arises when the conditions are combined using **OR**:\\ \\ | Here the **AND**s have simply been changed to **IF**s. If **condition1** is FALSE neither **condition2** nor **condition3** is evaluated. If **condition1** is TRUE but **condition2** is FALSE, **condition3** is not evaluated.\\ \\ A similar situation arises when the conditions are combined using **OR**:\\ \\ | ||
| + | <code bb4w> | ||
| IF condition1 OR condition2 OR condition3 THEN | IF condition1 OR condition2 OR condition3 THEN | ||
| + | </ | ||
| Here, if **condition1** is TRUE (strictly, not FALSE) the entire expression must be TRUE, irrespective of the results of the other conditions, and it would be better not to waste time evaluating **condition2** and **condition3**.\\ \\ Unfortunately there isn't such a straightforward way of emulating this in BASIC as in the **AND** case. However it is possible to achieve the desired effect as follows:\\ \\ | Here, if **condition1** is TRUE (strictly, not FALSE) the entire expression must be TRUE, irrespective of the results of the other conditions, and it would be better not to waste time evaluating **condition2** and **condition3**.\\ \\ Unfortunately there isn't such a straightforward way of emulating this in BASIC as in the **AND** case. However it is possible to achieve the desired effect as follows:\\ \\ | ||
| + | <code bb4w> | ||
| IF condition1=FALSE IF condition2=FALSE IF condition3=FALSE THEN | IF condition1=FALSE IF condition2=FALSE IF condition3=FALSE THEN | ||
| ELSE | ELSE | ||
| REM Carry out the required actions here | REM Carry out the required actions here | ||
| ENDIF | ENDIF | ||
| + | </ | ||
| Effectively what we have done here is to use [[http:// | Effectively what we have done here is to use [[http:// | ||
short-circuit_20evaluation.1522502382.txt.gz · Last modified: 2024/01/05 00:16 (external edit)