next up previous
Up: Return

Structured Programming

Group: Captain: Recorder: Reflector:

Motivation

In order for a program to be adaptable to changing conditions, it must be able to take actions depending on whether certain conditions apply.

In order for a program to do more work than it takes to write the program, it is essential that the program can repeat statements.

Understanding conditional execution and repeated statements will make you a better programmer, hence a more desirable engineer.

Objectives

1.
Learn about if statements.
2.
Learn about logical expressions.
3.
Learn about for loops.
4.
Learn about while loops.
5.
Improve program neatness.
6.
Strengthen previous knowledge.

Performance Criteria

1.
Success in modifying your previous program (first save it with a different name!) so that it first asks for the steel content of the part (instead of asking first for the cost of steel per unit weight.). If there is no steel content, (in other words, if the user enters zero for the steel content of the part), the program should not ask for the cost of steel. The same for the brass in the part. Remember, the intention is to reduce unnecessary work for the user.

Hint: This program will require you to use if statements.

Have the program approved by the instructor before proceeding with the next program. (10%)

2.
Success in modifying your previous program (first save it with a different name!) so that it provides a menu allowing the user to select between computing a part's material cost or quiting. When the program is executed, the following menu appears:
            P    Compute the material cost of a part
            Q    Quit

    Choice: _

When the user chooses ``P'', the program next asks him for the weights of steel and brass of the part, and the cost of those materials. (Just like the old program.) Then it prints out the material cost of the part. Then it prints ``Goodbye'' and terminates.

When the user selects ``Q'', the program prints ``Goodbye'' and terminates.

When the user enters anything else than ``P'' or ``Q'', the program rings the bell, (won't work on the PC) and prints an error message. Then it prints ``Goodbye'' and terminates.

Hint: There are three possibilities for the user's choice from the menu: (1) the user selects 'P' or 'p'; (2) the user selects 'Q' or 'q'; and (3) the user selects any other (wrong) key. Use an if (...) {...} else if (...) {...} else {...} construct to process these three possibilities differently.

Hint: What are the conditions behind the if and the else if?

Hint: What are the statements you only want to do if the user enters 'P' or 'p'? In which part of the if (...) {...} else if (...) {...} else {...} ladder do they go? What are the statements you only want to do if the user enters 'Q' or 'q'? What are the statements you only want to do if the user enters a wrong key? In which part of the if (...) {...} else if (...) {...} else {...} ladder do they go? What are the statements you always want to do? Where do they go?

Have the program approved by the instructor before proceeding with the next program. (20%)

3.
Success in modifying your previous program (first save it with a different name!) so that it keeps repeating the menu until the user selects 'Q'. This allows the program to find the material cost of more than one part. (User presses 'P', program computes a part; menu repeats; user presses 'P' again, program computes another part; etcetera.) Note that the menu should also repeat if the user presses a wrong key, after printing the error message.

Hint: Use a while (...) {...} loop to keep repeating things. What would be the condition to keep looping around in the while?

Hint: To find the start of the while, ask yourself: what is the first thing I want to do repeatedly?

Hint: To find the end of the while, ask yourself: from what location should I jump back up?

Have the program approved by the instructor before mailing. (20%)

4.
Program neatness, including lots of clear and meaningful comments, clear and meaningful variable names, and clear indentations. (20%)

5.
The knowledge demonstrated by your definitions of the vocabulary. (10%)

6.
The depth of understanding demonstrated by your answers to the critical thinking questions. (20%)

Resources

1.
Your class notes.
2.
The textbook.
3.
The online help facilities of Windows and the C++ compiler.

Plan

1.
Skim through the lessons ``Teaching Your Program to Make Decisions'' and ``Repeating One or More Statements'' of the book.
2.
Modify your program, compile and execute it.
3.
Write and test the final program, calling it menu.cpp.
4.
E-mail the program to the instructor and the group members.
5.
See what vocabulary you can define.
6.
See what critical thinking questions you can answer.
7.
Take the test and submit it.

Vocabulary

Select the best answer:

1.
Compound statements.
(a)
Statements enclosed between curly brackets, taking the place of a single statement.
(b)
Statements having a combination of artihmetic operations.
(c)
Statements having a condition in them.

2.
What are if statements?
(a)
Statements that only execute certain substatements if a condition is true.
(b)
Statements that are executed repeatedly.

3.
What are logical expressions?
(a)
Expressions that can have only the values true and false.
(b)
Expressions that are written in the logical order.

4.
Logical expressions
(a)
are like arithmetic expressions, but they evaluate to true or false instead of to a number.
(b)
cannot be used inside the parentheses of if statements.
(c)
cannot be used inside the parentheses of else if statements.
(d)
cannot be used inside the parentheses of for statements.
(e)
cannot be used inside the parentheses of while statements.

5.
Logical operators
(a)
combine logical values like arithmetic operators (+, -, *, /) combine numbers.
(b)
cannot be used inside the parentheses of if statements.
(c)
cannot be used inside the parentheses of else if statements.
(d)
cannot be used inside the parentheses of for statements.
(e)
cannot be used inside the parentheses of while statements.

6.
What does C++ do if you give it a number where it wants a true or false value?
(a)
It refuses to compile.
(b)
It crashes during execution.
(c)
It stupidly takes any nonzero value to be ``true", and zero to be ``false".

7.
if/else statement.
(a)
A way to execute different statements or compound statements for a series of possible conditions.
(b)
A way to repeat statements multiple times.
(c)
A way to execute one statement or compound statement if a condition is true, another if it is false.

8.
if/else if/.../else ladder.
(a)
A way to execute different statements or compound statements for a series of possible conditions.
(b)
A way to repeat statements multiple times.
(c)
A way to execute one statement or compound statement if a condition is true, another if it is false.

9.
for loops.
(a)
Allow you to repeat a statement or compound statement using a counter.
(b)
Allow you to repeat a statement or compound statement until a condition is no longer true.
(c)
Allow you to do both, but you should really use a while loop in the second case.

10.
while loops.
(a)
Allow you to repeat a statement or compound statement using a counter.
(b)
Allow you to repeat a statement or compound statement until a condition is no longer true.
(c)
Allow you to do both, but you should really use a for loop in the first case.

Critical Thinking Questions

Select the best answer:

1.
What is the difference in results from the left and right program parts:
    if (1 == 2)                     if (1 == 2) {
       cout << "Strange ";             cout << "Strange ";
       cout << "Mathematics";          cout << "Mathematics";}
(a)
Strange Mathematics / Strange Mathematics.
(b)
Mathematics / No output.
(c)
No output / No output.

2.
Which program part which is correct:
    if (1 == 2)                     if (1 == 2)
       cout << "Strange ";             cout << "Strange ";
                                       cout << "Mathematics";
    else                            else
       cout << "Plain ";               cout << "Plain ";
       cout << "Mathematics";          cout << "Mathematics";
(a)
Left.
(b)
Right.
(c)
Both are correct, but left is shorter.

3.
For if ... if ... else ..., does the else belong to the first or second if? (The dots are the conditions and statements or compound statements)
(a)
First.
(b)
Second.
(c)
This is not allowed.

4.
How could you safely reformulate the previous construct so that confusion is not possible?
(a)
if ... else if ... else ...
(b)
if ... else ... if ... else ...
(c)
if ... { if ... else ...}

5.
Which program part produces correct output for any a, b:
    if (a<b || a==b)                 if (a<b && a==b)
       cout << "a is not > b";          cout << "a is not > b";
    else cout << "a > b";            else cout << "a > b";
(a)
Left.
(b)
Right.
(c)
Both.

6.
The condition a>b is equivalent to:
(a)
b<a.
(b)
b<a || b==a.
(c)
Both.

7.
The condition a<=b is equivalent to:
(a)
b>a || b==a
(b)
! (b<=a)
(c)
Neither

8.
The condition a<b is equivalent to:
(a)
b>a || b==a
(b)
! (b<=a)
(c)
Both.

9.
The expression a<1 || a>3 && a!=0 is equivalent to:
(a)
a<1 || (a>3 && a!=0)
(b)
(a<1 || a>3) && a!=0

10.
So, is the previous expression true when a is zero?
(a)
Yes.
(b)
No.
(c)
It is too soon to tell.

11.
What does the next program part write:
  for (i=2; i<5; i++) cout << i;
(a)
1 2 3 4 5
(b)
2 3 4 5
(c)
2 3 4
(d)
2 4
(e)
2
(f)
5
(g)
6
(h)
Nothing.

12.
What does the next program part write:
    for (i=2; i<5; i++); cout << i;
(a)
1 2 3 4 5
(b)
2 3 4 5
(c)
2 3 4
(d)
2 4
(e)
2
(f)
5
(g)
6
(h)
Nothing.

13.
What does the next program part write:
    for (i=2; i<5; i=i+2) cout << i;
(a)
1 2 3 4 5
(b)
2 3 4 5
(c)
2 3 4
(d)
2 4
(e)
2
(f)
5
(g)
6
(h)
Nothing.

14.
What does the next program part write:
    for (i=2; i<5; i=i+3) cout << i;
(a)
1 2 3 4 5
(b)
2 3 4 5
(c)
2 3 4
(d)
2 4
(e)
2
(f)
5
(g)
6
(h)
Nothing.

15.
What does the next program part write:
    for (i=5; i<5; i=i+3) cout << i;
(a)
1 2 3 4 5
(b)
2 3 4 5
(c)
2 3 4
(d)
2 4
(e)
2
(f)
5
(g)
6
(h)
Nothing.

16.
How many exclamation marks does the next program part write:
    for (i=2; i<5; i--) cout << "!";
(a)
None.
(b)
One.
(c)
Infinitely many.

17.
What does the next program part write:
    for (i=2; i>0; i--) cout << i;
(a)
0 1 2
(b)
2 1 0
(c)
2 1
(d)
1 0
(e)
1
(f)
Nothing.

18.
Can you leave the initialization, test, and increment away from a for loop? If so, what would happen?
(a)
No, this will not compile.
(b)
Yes, but the statement in the loop will not execute even once.
(c)
Yes, but I will lose credit for poor coding.

19.
What is the result of i=0;while (i<2) cout << i++;?
(a)
This will not compile.
(b)
0 1 2
(c)
0 1
(d)
1 2
(e)
1

20.
What is the result of while (1==1) cout << '!';?
(a)
The cout statement is not executed even once.
(b)
It writes a single exclamation mark.
(c)
It keeps writing exclamation marks until you manage to kill off the process.

21.
What is the result of while (1) cout << '!';?
(a)
This will not compile, since a condition, not a number, is needed within the parentheses of the while.
(b)
The cout statement is not executed even once.
(c)
It writes a single exclamation mark.
(d)
It keeps writing exclamation marks until you manage to kill off the process.

22.
What is the result of while (0) cout << '!';?
(a)
This will not compile, since a condition, not a number, is needed within the parentheses of the while.
(b)
The cout statement is not executed even once.
(c)
It writes a single exclamation mark.
(d)
It keeps writing exclamation marks until you manage to kill of the process.

Exercises
1.
What sort of possible improvements would you see for your program?

Evaluation

1.
Give two strengths of your group.
2.
Indicate two areas for improvement.
3.
What two new insights did your group obtain?
4.
Give two strengths of the class so far.
5.
Indicate two areas for improvement of the class.
6.
What two new insights did you obtain about the class?

next up previous
Up: Return
Author: Leon van Dommelen