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.
if
statements.
for
loops.
while
loops.
Hint: This program will require you to use if
statements.
Have the program approved by the instructor before proceeding with the next program. (10%)
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%)
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%)
menu.cpp
.
Select the best answer:
if
statements?
true
and false
.
true
or false
instead of to a number.
if
statements.
else if
statements.
for
statements.
while
statements.
if
statements.
else if
statements.
for
statements.
while
statements.
if/else
statement.
if/else if/.../else
ladder.
for
loops.
while
loop in the second case.
while
loops.
for
loop in the first case.
Select the best answer:
if (1 == 2) if (1 == 2) { cout << "Strange "; cout << "Strange "; cout << "Mathematics"; cout << "Mathematics";}
Strange Mathematics
/ Strange Mathematics
.
Mathematics
/ No output.
if (1 == 2) if (1 == 2) cout << "Strange "; cout << "Strange "; cout << "Mathematics"; else else cout << "Plain "; cout << "Plain "; cout << "Mathematics"; cout << "Mathematics";
if ... if ... else ...
, does the else
belong
to the first or second if
? (The dots are the conditions and
statements or compound statements)
if ... else if ... else ...
if ... else ... if ... else ...
if ... { if ... else ...}
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>b
is equivalent to:
b<a
.
b<a || b==a
.
a<=b
is equivalent to:
b>a || b==a
! (b<=a)
a<b
is equivalent to:
b>a || b==a
! (b<=a)
a<1 || a>3 && a!=0
is equivalent to:
a<1 || (a>3 && a!=0)
(a<1 || a>3) && a!=0
a
is zero?
for (i=2; i<5; i++) cout << i;
for (i=2; i<5; i++); cout << i;
for (i=2; i<5; i=i+2) cout << i;
for (i=2; i<5; i=i+3) cout << i;
for (i=5; i<5; i=i+3) cout << i;
for (i=2; i<5; i--) cout << "!";
for (i=2; i>0; i--) cout << i;
for
loop? If so, what would happen?
i=0;while (i<2) cout << i++;
?
while (1==1) cout << '!';
?
cout
statement is not executed even once.
while (1) cout << '!';
?
while
.
cout
statement is not executed even once.
while (0) cout << '!';
?
while
.
cout
statement is not executed even once.