Figure 1 program control instruction
If .. and then instruction.
The IF instruction contains three branches, namely Then, Else and ElsIF.
Figure 2 IF statement
Figure 3 floating-point number comparison conditions
IF statement should be the most commonly used statement in SCL, which is often used for conditional judgment. The judgment type can be Boolean, floating-point and so on. Basically, all types can be judged, as long as the two variables compared are of the same type.
In Figure 2, AByte is a byte type, because 1 is between 0 ... 255, so 1 will be automatically converted to a byte type. What would it look like if the Abyte type was 256? Readers can write their own programs and try out the results, which will deepen your understanding of data types.
In the above program, first, for example, whether AByte is equal to 1, if yes, the value of Outvalue is 1, and then compared with 2, if it is equal to 2, the value of OuValue is 2, and if both meet the conditions, the output value of OuValue is 5.
The above IF conditional statement can also be expressed by CASE statement. The comparison type of CASE is not as wide as that of IF, and shaping can only be used as the comparison condition.
Case teaching
Figure 4 CASE expression of if statement
From the CASE statement, we find that the statement is clearer and simpler than the IF expression, so if you need to use shaping conditions as a judgment, try to use CASE to realize it, which will improve efficiency.
Used for guidance
The FOR instruction is usually used to batch data of array type. The last lecture gave an example. Here's a slightly more complicated example.
Figure 5 FOR loop statement
In the FOR program, we assign a value to each index value of the array, and at the same time judge the parity of the index value to assign a value.
Figure 6 Loop statement 2
In the above program, we found that there is a keyword Continue. When the FOR loop executes to this statement, the program below Continue will not be executed. Therefore, when iCout is less than 15, the AArray[iCount]:=iCount assignment statement will not be executed.
Another difference is that a keyword BY is added after the FOR statement. BY indicating the execution interval according to the setting of by. If it is later set to 1, all 0...99 will be executed; If set to 2, the execution order is 0, 2, 4, 6 and 8.
We can also use the WHILE loop to implement the FOR loop statement above.
WHILE instruction
Figure 7 WHILE instruction
The expression of WHILE loop and the basic type of FOR loop, but pay attention to one thing. Here, the loop variable is progressive manually. Without this sentence, the value of iCount is always 0, and the loop condition icount.
We also noticed a keyword EXIT. When this instruction is executed, the WHILE loop will stop executing, and the FOR instruction will also stop executing. So the above program, when it runs, is that the assignment of array is only executed until the end of AArray[8 1].
Go to instruction
Goto instruction is mainly used to perform jump process, which can bring convenience to program operation and is generally not recommended. Improper operation will upset the logic of the whole program.
In fig. 7, if iCount is greater than 90, the program will jump out of the WHILE loop, execute program #iCount=0 at the designated label of label 13, and continue to execute the WHILE loop from the beginning. Of course, this operation is meaningless. This is just an example.