[MUSIC] If you watched the last video, then you have an understanding of comparisons in REXX, else you might be scratching your head a little bit in this section. Because now we're going to apply the use of comparisons in the logic or control instructions that directly processing of a program. REXX supports both an if then and an if then else structure. In other words, the else portion is optional. If then else in REXX works in much the same way as it does in almost every other programming language, a comparison is performed. If the result of the comparison is true, then the path marked by the Then keyword is followed, and the path marked by the else keyword is skipped if it exists. If the comparison is false, the then path is skipped and the path marked by the else keyword is followed if it exists. Note that you're only allowed one instruction following a then or an else keyword, thus in REXX there is no end if instruction. And note the indentation in the example. Indentation is strictly up to the programmer, but it really helps to make your program a bit more readable. Quite often we'll want to execute more than one instruction after then or an else keyword. And those cases we can get around the one instruction restriction by a bracketing, a group of instructions between the do and end instructions. This is called the do and group and it's quite the hack. You see the do end group is logically treated as only one instruction after the then what else by REXX, even though it may contain many instructions which will actually be executed. And there's no limit to the number or type of instructions that may be included between the do and the end. So much for your single instruction rule Mr. if then else. Now, sometimes you want to perform more than one comparison in order to decide a processing path. For example, if A equals B and C equals D, then you want to do something. This can be done in REXX by combining comparisons with the logical operator characters, you must use the operator symbols shown on the screen. These operators perform Boolean logical operations upon comparison results, so one or zero, in order to produce final one or zero results. So we have ampersand which means and, both terms must be true in order for the final result to be true. If either or both of the terms are false, the final result was false. We got the pipe symbol which means or, either term or both terms may be true for the final result to come out as true. If both terms are false, the final result is false. Ampersand, ampersand which means exclusive or one and only one term must be true for the final result to be true. If both terms are false or if both terms are true, final result is false. And backslash which means not. A logical expression is the combination of comparisons and logical operators. The comparisons give a result of 1 or 0 then the logical operators act on those Its results. In an expression the order of evaluation an operator precedence is important. We can make our intentions obvious or change the order of operations with parentheses. You may nest if instructions inside of other instructions. In fact, you may nest up to 250 if's deep but practically you should try not to get too hung up in this type of structure. It gets very confusing very quickly. And REXX takes a lot of overhead to process this type of construct. Note in the example the due and bracketing keywords and the indentation. Although REXX doesn't care one way or the other, you should try to line up your end statements so that they begin in the same column as the corresponding do statements. And you should always try to indent subordinate clauses. How do you know which else matches to which if in a complex series of statements? REXX has a strict rule, the else binds to the nearest if at the same level of execution. Therefore in the first example above, the first else bias to the second if and the second else binds to the first if this is much easier to see by using indentation, as in the second example. The select structure allows you to replace multiple nested if then else's, with a much more straightforward construct. Select also uses less overhead than multiple if then else's. Some other programming language call this a case structure, and the implementation may be slightly different. In this structure, each of the tests is a separate and independent logical expression. The first test is performed and if the result is true, it's corresponding instruction is executed. And the test is false, then the next test is performed, and so on all the way down the line. When the test result is true, the corresponding instruction is performed and then the select structure is exited, then more testing is done. So this means that the order in which the tests are written becomes quite important. If there is some tests that you want to make sure that you do every time, it must be coded first. If a test is only to be done as a last resort, then it must be coded last. In REXX, the select construct offers a safety net, so if all of the tests are found to be false, then you can have REXX execute a set of instructions at the bottom of the structure. The select instruction name is usually placed on a line by itself. You follow this with the keyword when which identifies a logical expression. Following the logical expression is the keyword then. Just as the if construct only one instruction is allowed following the then keyword unless you hack up now, I mean code a do and group. There is no restriction on what instruction you can coded after the then, so selects may be nested just like if's. The key word otherwise after all of the when key words is your safety net. So if none of the when logical expressions result in a true condition, the instructions following the otherwise are executed. The otherwise keyword and its corresponding instructions are actually optional. But if no otherwise is coded, and none of the logical expressions result in a true condition, your exact will fail with a runtime error. The select structure must find something to execute, if it doesn't, your exec will fail. So logically otherwise should always be coded. That's great big a REXX rule number two a, every select must have a corresponding end, just like do now the multiple ends example above is extremely common. If you're lucky, the indentation is done properly. Can you imagine how difficult it would be to debug this program, especially if all those n statements started in the same column. Also, the corresponding do or select instructions can sometimes be several screens, perhaps hundreds of lines above the end instruction to more easily match and end instruction with its corresponding do or select instruction code a short comment on the end instruction. It's easy to do, it'll only take a second or two and I'll definitely save you a lot of headaches later. Now in the next video we're going to get a bit loopy. [MUSIC]