You are on page 1of 8

Answer key

CPT 168
Fall 2012
Homework #10
Chapter 4: 4.1-4.5, Checkpoint questions 4.1-4.20

4.1 What is a control structure?


A control structure is a logical design that controls the order in which a set of statements executes or
loops. Examples include for, while and do while loops in C++.

4.2 What is a decision structure?


A decision structure is a control structure that allows a program to perform actions only under certain
conditions. The condition is
tested (ex. someNumber <= 2 or
Prompt user for input
color == blue), and
performs an action or actions according to
someNumber
whether the condition (or
conditions) evaluate(s) to TRUE or
FALSE (Boolean).
Input someNumber

Ex.

False

Execute
statement(s)

someNumber <= 2

True

Execute
statement(s)

4.3 What is a single alternative decision structure?


A single alternative decision structure is one which provides only one alternative path of execution.
For example:
If color == blue Then // color being a string variable in this case
Display blue
End If
In the above case either the condition is True (the value of the variable color IS blue) and the
program displays blue. Otherwise (if the condition is not true) the decision structure is exited with no
action taken. It can also work the opposite way with the single path of execution being performed if
the condition evaluates to False, otherwise (if it evaluates to true) the decision structure would be
exited with no action taken.

4.4 What is a Boolean expression?


A Boolean expression is an expression that can be evaluated to True or False using a relational
operator (>, <, >=, <=, ==, !=)

4.5 What types of relationships between values can you test with relational operators?
Using relational operators you can test whether a value or expression is greater than another, whether
a value or expression is less than another, whether a value or expression is greater than OR equal to
another, whether a value or expression is less than OR equal to another, whether a value or
expression is equal to another, whether a value or expression is NOT equal to another.

4.6 Write a pseudocode If-Then statement that assigns 0 to x if y is equal to 20.


If y==20 Then
Set x = 0

End If

4.7 Write a pseudocode If-Then statement that assigns 0.2 to commission if sales is greater than
or equal to 10,000.
If sales >= 10000 Then
Set commission = 0.2
End If

4.8 How does a dual alternative decision structure work?


A dual alternative decision structure has two possible paths of execution, one if the condition
evaluates to true, and another if the condition evaluates to false. The dual alternative decision
structure executes one statement (or set of statements) no matter what the Boolean value of the
evaluation is. An example of this is the piece of flowchart shown in the answer to question 4.2.

4.9 What statement do you use in pseudocode to write a dual alternative decision structure?
You would use the If-Then-Else statement.
If someInteger < 10 Then
Display Thats too low!
Else
Display Thats too high!
End If

4.10 When you write an If-Then-Else statement, under what circumstances do the statements that
appear between Else and End If execute?
In an If-Then-Else statement, the statements between Else and End If would execute only if the
condition for the If portion did not evaluate as true.
Examples:
Set yourNumber = 33
If yourNumber > 34 Then
Display Your number is greater than 34.
Else
Display Your number is not greater than 34.

End If
In the above pseudocode example, the value of yourNumber is set to 33. The If condition evaluates
as False, therefore the Else statement would execute.
Set name = Fred
If Not (name == Fred)
Display Hey, youre not Fred!
Else
Display Hi Fred!
End If
In the second example above, name is set to Fred, therefore the If condition, Not (name ==
Fred) --the same as name != Fred -- evaluates to False, so the Else statement would execute
and display Hi Fred!

4.11 If the following pseudocode were an actual program, what would it display?
If z < a Then
Display z is less than a.
Else
Display z is not less than a.
End If
The program would display z is not less than a.
The computer evaluates the condition above using ASCII value of the character z (122, or for you
01111010) to the ASCII value of the character a (97, or in binary 01100001). 122 < 97
evaluates to False.
4.12 If the following pseudocode were an actual program, what would it display?
Declare String s1 = New York
Declare String s2 = Boston
If s1 > s2 Then
Display s2
Display s1
Else
Display s1
Display s2
End If
Boston
New York
So in short, the computer starts on the left and compares the first character of each string. If theyre
equal it moves on to the next character in each string. As soon as it gets to a character comparison

that is not equal it determines greater than/less than using the ASCII values for the two characters
and does not compare any further. If one string runs out of characters before another during the
comparison/evaluation (so up until then the strings have been equal) the shorter one is determined to
be the lesser value.
Ex.
Georgetown > George
The computer would First compare the ASCII values for G and G, then for e and e, followed by o and
o, r and r, g and g, e and e, and finally t and (oops, out of characters), therefore Georgetown
> George would evaluate True.
In the code for the question, the computer would compare the ASCII value for B (66) to the ASCII
value for N (78) and determine that Boston < New York. Because of the set order of the ASCII
code values we (people, that is, rather than computers) can also easily look at certain string
comparisons and know which is the higher value.
A-Z: Capital letters all have a lower ASCII value than lowercase letters. Starting at A, each following
letter has a higher value than the previous one. So A < B which is < C which is < D, etc. The same
goes for a-z (lowercase) a < b < c, d etc. Any capital letter has a lower ASCII value than any
lowercase letter.
A number (talking about string comparisons by character, so 0-9) has a lower value than any
alphabetic character (referring only to the American alphabet/ASCII code)

4.13 How does a dual alternative decision structure work?


A dual alternative decision structure has two possible paths of execution. One path is taken if the
condition evaluates True, the other if the condition evaluates False. See the answer to 4.8. Its the
same question!

4.14 What statement do you use in pseudocode to write a dual alternative decision structure?
You use the If-Then-Else statement to write a dual alternative decision structure. (See 4.9. Again,
same exact question.)

4.15 When you write an If-Then-Else statement, under what circumstances to the statements that
appear between the Else clause and the End If clause execute?

In an If-Then-Else statement, the statements between Else and End If would execute only if the
condition for the If portion did not evaluate as true. See the answer to 4.10 for examples.
4.16 Convert the following pseudocode to an If-Then-Else If statement:
If number == 1 Then
Display One
Else
If number == 2 Then
Display Two
Else
If number == 3 Then
Display Three
Else
Display Unknown
End If
End If
End If

4.17 What is a multiple alternative decision structure?


A multiple alternative decision structure allows you to test the value of a variable or expression and
then use that value to determine which statement or statements to execute. The case or switch
structure is a multiple alternative decision structure.
Ex.

Choose Menu
Item

Choice
1

Choice
2

Choice
3

Default

The flowchart snippet above would work well for a program in which the user was presented with a
menu and prompted to enter a menu choice:
1.
Computeperimeter
2.
Computearea
3.
Computediameter
Enter1,2,or3:
The Default pathway could be a message displaying Invalid input or a statement to
exit the program.

4.18 How do you write a multiple alternative decision structure in pseudocode?


Select (testExpression)
Case oneValue
Display You chose item 1.
Set someVar = 20
Case twoValue
Display You chose item 2.
Set someVar = 30
Case threeValue
Display You Chose item2.
Set someVar = 40
Default:
Display Invalid choice.
End Select

4.19 What does the case structure test, in order to determine which set of statements to execute?
The case structure compares the value of the testExpression (usually a variable, but can be an
expression) with the values that follow each Case statement from top to bottom. When it finds a Case
value that matches it branches to that Case statement, executes the statements for that Case and
then exits the case structure. If it doesnt find a matching Case it branches to the Default statement
and executes the statements for the Default Case.

4.20 You need to write a multiple alternative decision structure, but the language you are using will not
allow you to perform the test you need in a Select Case statement. What can you do to achieve the
same results?
You can use nested decision structures to achieve the same results.
Ex.
If testExpression == oneValue Then
Display You chose item 1.
Set someVar = 20
Else
If testExpression == twoValue Then
Display You chose item 2.
Set someVar = 30
Else
If testExpression == threeValue Then
Display You Chose item2.
Set someVar = 40
Else
Display Invalid choice.
End If
End If
End If

You might also like