Friday, October 15, 2010

QA On Conditional Statements


Q The if statement seems like the one that's most useful. Is it possible to use only if statements in programs and never use the others?

A
It's possible to do without else or switch, and many programmers never use the ternary operator ?. However, else and switch often are beneficial to use in your programs because they make them easier to understand. A set of if statements chained together can become unwieldy.

Q An if statement is described as a single statement or as a conditional statement followed by another statement to handle if the condition is true. Which is it?

A
The point that might be confusing is that
if statements and other conditionals are used in conjunction with other statements. The if statement makes a decision, and the other statements do work based on the decision that is made. The if statement combines a conditional statement with one or more other types of Java statements, such as statements that use the println() method or create a variable.

Q During this hour, opening and closing brackets { and } are not used with an if statement if it is used in conjunction with only one statement. Is this mandatory?

A
No. Brackets can be used as part of any
if statement to surround the part of the program that's dependent on the conditional test. Using brackets is a good practice to get into because it prevents a common error that might take place when you revise the program. If you add a second statement after an if conditional and don't add brackets, unexpected errors will occur when the program is run.

Q Will the Java compiler javac catch the error when an = operator is used with a conditional instead of an ==?

A
Often no, and it results in a real doozy of a logic error. These errors only show up when a program is being run and can be discovered only through observation and testing. Because the
= operator is used to assign a value to a variable, if you use name = "Fernando" in a spot in a program where you mean to use name == "Fernando", you could wipe out the value of the name variable and replace it with Fernando. When the value stored in variables changes unexpectedly, the result is subtle and unexpected errors that you must debug.

Q Does break have to be used in each section of statements that follow a case?

A
You don't have to use
break. If you do not use it at the end of a group of statements, all of the remaining statements inside the switch block statement will be handled, regardless of the case value they are being tested with.

Q What's the difference between System.out.println() and System.out.print()?

A
The
println() statement displays a line of text and ends the line with a newline character. The newline character has the same behavior as the carriage return key on a manual typewriter. It causes the next text to begin displaying at the leftmost edge of the next line. The print() statement does not use a newline character, making it possible to use several print() statements to display information on the same line.

Quiz

The following questions will see what condition your knowledge of conditions is in.

Questions

1. Conditional tests result in either a true or false value. Which variable type does this remind you of?

(a) None. They're unique.
(b) The
long variable type.
(c) The
boolean type.

2.
Which statement is used as a catch-all category in a
switch block statement?

(a)
default
(b)
otherwise
(c)
onTheOtherHand

3.
What's a conditional?

(a) The thing that repairs messy split ends and tangles after you shampoo.
(b) Something in a program that tests whether a condition is true or false.
(c) The place where you confess your sins to a neighborhood religious figure.

Answers

1. c. The boolean variable type can only equal true or false, making it similar to conditional tests.

2.
a.
default statements will be handled if none of the other case statements matches the switch variable.

3.
b. The other descriptions are conditioner and confessional.

Activities

To improve your conditioning in terms of Java conditionals, review the topics of this hour with the following activities:

·         Remove the break statement from one of the lines in the ClockTalk program, and then compile it and see what happens when you run it. Try it again with a few more break statements removed.

·         Create a short program that stores a value of your choosing from 1 to 100 in an integer variable called grade. Use this grade variable with a conditional statement to display a different message for all A, B, C, D, and F students. Try it first with an if statement, and then try it with a switch statement.

No comments:

Post a Comment