Comp. Sci Exercises 7.05-07

Describe one-way selection.

Basically, one-way selection says to perform some indicated action if a condition is true.
If the condition is false, continue to march as if the control structure did not exist.

What is the output of this program if the user enters 800000?

Christmas bonus: 1000.0

What is the output of this program if the user enters 499999?

Christmas bonus: 0.0

Why should indentation be used with control structures?

It makes the program more readable.

Look at program OneWaySelection02.java. Why does this program have strange output?

The if statement only controls the line immediately following it.

Look at program OneWaySelection03.java. How does this cure the problem of the previous program?

An extra set of braces are used to create a "block" around all of the statements we want controlled by the if statement.

Describe two-way selection.

With two-way selection there are precisely two paths. The computer will either take one path,
If the condition is true, or take the other path if the condition is false.

What statement is used with if for two-way selection?

else

What is the output of this program if the user enters 1600?

You are admitted

What is the output of this program if the user enters 400?

You not are admitted

Write the Java statement that will do this action:
If your grade is 70 or better, print "You passed!"
otherwise print "You failed!

if (grade >= 70)
System.out.println("You passed");
else
System.out.println("You failed");

Describe Multiple-Way Selection.

Multiple-Way Selection occurs whenever you have more than 2 choices.

Does Multiple-Way Selection use a conditional statement?

No

Which data types work with switch?

int, char and String

With switch there are usually at least _______ possible outcomes.

3

Refer to the previous question.
Each of the possible outcomes starts with what reserved word?

case

The output of program MultiWaySelection01.java looks very strange.
How does MultiWaySelection02.java cure this problem?

A break commands is placed between every case.

When will the program execute the default case?

When no other match in found.

Does the default case need a break command? Why or Why not?

No, because it is already at the end of the switch structure.

Can the cases in a switch structure have multiple statements?

Yes

What is the output of this program if the user enters 'A'?

90 .. 100 Average
Excellent!

What is the output of this program if the user enters 'Q'?

No Match Found

What is the output of this program if the user enters 'b'?

No Match Found

Explain how program MultiWaySelection04.java is more flexible than MultiWaySelection03.java.

It allows for both capital and lowercase letters.

What is the output of this program if the user enters 9?

Freshman

What is the output of this program if the user enters 10?

Sophomore

What is the output of this program if the user enters 11?

Junior

What is the output of this program if the user enters 7?

You are not in high school.

What is the output of this program if the user enters Greg?

This is Mr. Schram's younger son.

What is the output of this program if the user enters Heidi?

This is Mr. Schram's younger daughter.