Lab 7: Prelab Questions


Selection: Written Questions

Question #1: The if statement is used for ____________ and comes in three forms: ____________, ____________, and ____________.

Question #2: A(n) ____________ is treated by the compiler as a single statement, even though is consists of several statements.

Question #3: In addition to the if statement, C++ also provides the blank statement for the same purpose.

Question #4: The expression in a switch statement must be ____________.

Question #5: Only ____________ tests are allowed for the case clauses of a switch statement.

Selection: Programming Exercises

Suppose i and j are both ints set to some values.

Code #1: Write a boolean expression that evaluates to true if and only if i is less than j.

Code #2: Using the boolean expression from the previous question, write an if statement that prints "Too small!" when i is less than j.

Code #3: Adding to the previous question, write a new if statement that also prints "Too big!" when i is not less than j.

Code #4: Adding to the previous question, write yet another new if statement that prints "Just right." (not "Too big!") when i is equal to j.

Code #5: Why can't you turn any of these if statements into switch statements?
 

Repetition: Written Questions

Question #1: Which of the C++ loops is a pretest loop?

Question #2: Which of the C++ loops is a posttest loop?

Question #3: Which of the C++ loops provide both zero and one-trip behavior?

Question #4: Which of the C++ loops is best for counting?

Question #5: The ____________ statement allows you exit a loop in the middle.

Repetition: Programming Exercises

Suppose that i and j are int variables initialized to some values.

Code #1: Write a while loop that repeats while i is less than j. Use an empty compound statement as the body of the loop.

Code #2: Write a do loop that repeats while i is less than j. Use an empty compound statement as the body of the loop.

Code #3: Write a counting for loop that counts from 4 to 55. Use an empty compound statement as the body of the loop.

Code #4: Write an if-break statement that breaks when i is less than j.

Code #5: Incorporate the if-break statement of the previous question into a forever loop. Use a compound statement as the body of the loop.


Lab Exercise
Report errors to Larry Nyhoff (nyhl@cs.calvin.edu)