![]() If/then/else conditional statements, like for loops, represent a fundamental change to the control flow of programs. Just like for-loops, in which the interpreter waits for you to finish typing in code between do and done, the if/then/else construct isn't executed until you've typed in fi (the closing statement for an if statement). Is this confusing the hell out of you? It probably should, as if/then/else constructs, like for-loops, aren't meant to be typed out at the interactive prompt. Thus, the code after if/then is executed, with this result: num is actually equal to 42 However, if you set num to 42 beforehand, then the condition in the if statement was met, as $num evaluates to 42, which is equal to 42. ![]() So only the command in the "else branch" is executed, with this result: num is not 42 If you didn't set the variable num to 42 beforehand, then the condition in the if statement, ( $num is equal to 42), would evaluate to false. If not, then do something else."įor example, type out the following sequence: if ] then # if/then branch echo 'num is actually equal to 42' else # else branch echo 'num is not 42' fi In English/pseudocode, the control flow might be described like this: To create a branch in our program, is to create an alternative sequence of commands that may be ignored, based on certain conditions at run-time. How to write a program that can branch into more than one path of execution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |