IF/SELECT: Difference between revisions

2,759 bytes added ,  4 years ago
(Bolded)
 
(5 intermediate revisions by the same user not shown)
Line 1:
'''IF''' statements are the primary conditional instructions. Conditional instructions allow programs to change based on the status of variables within the system. Unconditional instructions such as register instructions, I/O instructions, or simple Motion Instructions will always do exactly what they are written to do when the line is reached in the program. Conditional instructions will only perform their associated action in the event the equation they contain is found to be true.
'''IF''' statements are the primary conditional instructions. They evaluate whether a condition is true, and execute a Jump or Call if it is.
 
== IF Statements ==
If it is not true, the instruction will occur as if the line were blank.
IF statements are the simplest conditional instruction. They contain an equation to be evaluated and an action to be taken if it is true.
 
{| class="wikitable"
!
! colspan="3" |Equation to be Evaluated
! colspan="2" |What happens If Equation is True
|-
! IF
! Variable
! Relational Operator
! Math
! Variable
! Action
Line 23 ⟶ 28:
| ON
| JMP
| LBL2LBL[2]
|}
When reached, they evaluate whether the equation is true.
Additionally, when selecting the action field, another condition can be added with the '''AND''' or '''OR''' selection. When wanting to make a large list of possible conditions for each variable, you'll likely want to use the '''SELECT''' option.
 
If the condition is found to be true, it can execute either a [[JMP/LBL|Jump]] or [[CALL|Call]].
[[File:Inst-if orig.gif|thumb|How to add an "IF" instruction. ]]
If it is not true, the instruction will occur as if the line were blank.
 
 
Example: IF R[1] = 1 JMP LBL[2]
 
If this line is reached while R[1] contains 4, nothing will happen. It will skip over the if statement without doing anything.
 
If this line is reached while R[1] contains 1, then "R[1] = 1" is true. This means it will perform the action it is given, which was jump to label 2.
 
 
Additionally, when selecting the action field by cursoring to it or touching it on the touchscreen, another condition can be added with the '''AND''' or '''OR''' selection.
{| class="wikitable"
! IF
! Variable
! Relational Operator
! Variable
! Modifier
! Variable
!Math
!Variable
!Action
!Destination
|-
| IF
| R[1]
| =
| 1
| AND
| R[2]
|>
|AI[10]
|JMP
|LBL[2]
|}
=== Relational Operators ===
When selecting an IF statement, the first choice to be made is the relational operator within the equation to be evaluated.
{| class="wikitable"
|+Relational Operator Types
!Symbol
!Name
!Evalues To "True" When
|-
|=
|Equal To
|The left variable is equal to the right variable.
|-
|<>
|Not Equals
|The left variable is not equal to the right variable.
|-
|<
|Less Than
|The left variable is less than the right variable.
|-
|<=
|Less Than or Equal To
|The left variable is less than or equal to the right variable.
|-
|>
|Greater Than
|The left variable is greater than the right variable.
|-
|>=
|Greater Than or Equal To
|The left variable is greater than or equal to the right variable.
|-
|(...)
|Mixed Logic
|
|}
 
=== Actions ===
In the event the equation evaluates as true, an action will occur. The following table lists the possible actions an IF statement can take.
{| class="wikitable"
|+Action Types
!Action
!Destination
!Description
|-
|[[JMP/LBL|JMP]]
|LBL[#]
|The program will jump to the labeled line and continue from there.
|-
|[[CALL]]
|Program
|A program is CALLED. When the called program reaches the [END] instruction, it will return to the next line after the IF statement.
|-
|Call
|Program()
|
|}
 
== SELECT Statements ==
When wanting to make a large list of possible conditions for a single variable, you'll likely want to use the '''SELECT''' option. SELECT is outside the scope of the MH-142 course but is useful none the less.
{| class="wikitable"
! SELECT
Line 51 ⟶ 153:
|
|=
|14
|Call
|Program3