Questions and Exercises: Control Flow Statements
Questions
- The most basic control flow statement supported by the Java programming language is the ___ statement.
- The ___ statement allows for any number of possible execution paths.
- The ___ statement is similar to the
while
statement, but evaluates its expression at the ___ of the loop.
- How do you write an infinite loop using the
for
statement?
- How do you write an infinite loop using the
while
statement?
Exercises
-
Consider the following code snippet.
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");
- What output do you think the code will produce if
aNumber
is 3?
- Write a test program containing the previous code snippet; make
aNumber
3. What is the output of the program? Is it what you predicted? Explain why the output is what it is; in other words, what is the control flow for the code snippet?
- Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
- Use braces, { and }, to further clarify the code.
Check your answers