Professional Documents
Culture Documents
CPT 168
Fall 2012
Homework #10
Chapter 4: 4.1-4.5, Checkpoint questions 4.1-4.20
Ex.
False
Execute
statement(s)
someNumber <= 2
True
Execute
statement(s)
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.
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.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.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
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.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