4.1 Introduction to Decision Structures
A logical design that controls the order in which a set of instructions is done is called a decision structure. The simplest of these is a sequence structure where one line follows the next. Sometimes you need a program to do something only if a certain condition is met. For that you need a decision structure. Its simplest form is to ask a question and go off to do something if true or if not continue on. To flowchart it you would use a diamond with arrows coming out the right side for true and the bottom for false. This is a single alternative decision structure. Combining Structures
It is possible to combine structures. Based on the decision structure above you may do a sequence structure afterwards if it is true.
Writing a decision structure in pseudocode
The word if is usually starts it off followed by the condition and then the steps with and endif.
If it is snowy then
Warm up the car
Drive slowly
Etc.
Endif
Boolean expressions and relational operators
To be able to make a decision we have to know if the condition is met. To do this we use Boolean expressions. They are:
Expression | Meaning |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
The >= and <= operators
These are met if the value on the left is greater than (>) or less than (<) the value on the right. They are also met if the values are equal
The == operator
This is met if both sides are equal. It is not to be confused with = which in most programming languages is used to assign a value to a variable.
The != operator
This is used to check the value on the left to against the value on the right and see if they are not equal
Putting it all together
[This section just reworded the previous section]
Programming style with the If-Then statement
In general the if and the endif are aligned with each other and the statements that meet the condition are indented slightly between the two.
4.2 Dual Alternative Decision Structures
It is possible to do one set of instructions if a statement is true and another if they are false before moving on to the next steps. This is called a dual alternative decision structure and is programmed as an If …Then… Else…Endif. A flow chart would have the true and false paths off the sides of the diamond before they meet up again at the end. Writing it is generally with the If, the End and the Endif all lined up. The statements of action would be indented in their proper structures. 4.3 Comparing Strings
Comparing strings is done the same way that comparing numbers are done. Remember that they will generally be case sensitive and that you will most likely be doing == and !=.Other String Comparisons
Since strings are stored as the number equivalent of the letters they represent, it is possible to compare the characters to see which one has a greater value. Remember that:
- Capital A thru Z are 65 to 90
- Lower case a thru z are 97 to 122
- 0 to 9 as strings are 48 to 57
- A blank is 32
4.4 Nested Decision Structures
If it is necessary to check two conditions, then a decision structure can be placed in side another structure. If the outside one is true the inside one will be tested. If the inside one is true it will do the tasks, if not it will drop out of the loop. Programming styles and the nested decision structure
In order to make the code readable, your pseudocode should be structured as before, but the internal loop should be indented as well to show its separation from the outside loop.
Testing a series of conditions
By nesting decision structures in several layers we can make a decision based on a series of values. As soon as one is true, drop out but if it false then test the next one.
The if-then-else statement
In order to make the above series of conditions easier to program, many languages use the if-then-else commands. Basically you check for a condition and if it is true, leave the loop, otherwise you will go to the next statement which will start with elseif. Pseudocode should have the IF, ElseIF and EndIf commands lined up with the others indented in their respective sections.
4.5 The Case Structure
Similar to the nested decision structure is the case structure. A value is compared in a series of tests and the first one it meets it will use those statements as the steps to do and then exit to the end. Since it will do the same thing as a nested decision structure, it is not necessary in a programming language but does make it easier to program. 4.6 Logical Operators
In order to test two values at once, in place of a nested decision structure we can use Boolean logical operators. Operator | Meaning |
AND | Both values must be true or the statement is false |
OR | One value or the other must be true to make it true |
NOT | Reverses the value of an expression making false true and true false. |
No comments:
Post a Comment