4.1
A decision structure allows a program to perform actions only under certain conditions – also known as selection structures. In its simplest form a specific action is performed only if a certain condition exists. If the condition does not exist the action is not performed.
Single alternative decision structure – because it provides only one alternative path of execution. If the condition In the diamond symbol is true we take the alternative path. Otherwise we exit the structure.
Writing a Decision Structure in Pseudocode
If condition Then Statement Statement Statement
End If
Boolean Expressions and Relational Operators
Booleans Expressions – expressions that can be evaluated as either true or false. Typically is formed with a relational operator. A relational operator determines where a specific relationship exists between two values.
Checkpoint
4.1 What is a control structure?
A logical design that controls the order in which a set of statements executes.
4.2 What is a decision structure?
A logical design that can execute only under certain circumstances
4.3 What is a single alternative decision structure?
Design structure that provides only one path of execution. If the condition in the diamond symbol is true, we take the alternative path. Otherwise, we exit the structure.
4.4 What is a Boolean Expression?
Expressions that can be evaluated as either true or false.
4.5 What types of relationships between values can you test with relational operators?
> Greater than, < Less Than, >= Greater than or equal to, <= Less than or equal to, == Equal to, != Not equal to
4.6 Write a pseudocode If-Then statement that assigns 0 to x if y is equal to 20.
If y=20 Set 0 =x
End If
4.7 Write a pseducode If-Then statement that assigns 0.2 to commission if sales is greater than or equal to 10,000.
If sales >=10,000 Set commission =0.2
End I
A dual alternative decision structure will execute one group of statements if its Boolean expression is true, or another group if its Boolean expression is false.
Dual alternative decision structure – has two possible paths of execution – one path is taken if a condition is true and other path is taken if the condition is false.
In Pseudocode we write a dual alternative structure as an If-Then-Else statement.
If condition Then Statement Statement Etc.
Else
Statement Statement Etc.
End If
In general format the condition is any Boolean expression. If the expression is true, the statements that appear next are executed up to the line that reads Else. If the expression is false the statements that appear between Else and End
Checkpoint
4.8 How does a dual alternative structure work?
One path is taken if the condition is true if it is false the other path Is taken
4.9 What statement do you use in pseudocode to write a dual alternative decision structure?
If-Then-Else statement
4.10 When you write an If-Then-Else statement, under what circumstances do the statements that appear between Else and End If execute?
When the statement is false
4.3 Comparing Strings
Most programming languages allow you to compare strings. This allows you to create decision structures that test the value of a string.
Checkpoint
4.11
If the following pseudocode were an actual program what would it display? z Is not less than a
4.12
S1
S2
4.4 Nested Decision Structures
To test more than one condition, a decision structure can be nested inside another decision structure.
The If-Then-Else If Statement
IF condition_1 Then Statement Statement Etc.
Else If condition_2 Then Statement Statement Etc.
Insert as many Else If clauses as necessary
Else
Statement Statement Etc.
End If
Checkpoint
4.13 How does a dual alternative structure work?
To qualify two conditions must exist. If the first condition is true we need to test the