Documentation
The Java™ Tutorials
Download Ebooks
Download JDK
Search Java Tutorials
Hide TOC
Language Basics
Variables
Primitive Data Types
Arrays
Summary of Variables
Questions and Exercises
Operators
Assignment, Arithmetic, and Unary Operators
Equality, Relational, and Conditional Operators
Bitwise and Bit Shift Operators
Summary of Operators
Questions and Exercises
Expressions, Statements, and Blocks
Questions and Exercises
Control Flow Statements
The if-then and if-then-else Statements
The switch Statement
The while and do-while Statements
The for Statement
Branching Statements
Summary of Control Flow Statements
Questions and Exercises
Trail: Learning the Java Language
Lesson: Language Basics
« Previous • Trail • Next »

# Questions and Exercises: Control Flow Statements

## Questions

1. The most basic control flow statement supported by the Java programming language is the ___ statement.
2. The ___ statement allows for any number of possible execution paths.
3. The ___ statement is similar to the `while` statement, but evaluates its expression at the ___ of the loop.
4. How do you write an infinite loop using the `for` statement?
5. How do you write an infinite loop using the `while` statement?

## Exercises

1. 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");

   1. What output do you think the code will produce if `aNumber` is 3?
   2. 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?
   3. Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
   4. Use braces, { and }, to further clarify the code.

Check your answers

« Previous • Trail • Next »

Previous page: Summary of Control Flow Statements
Next page: Classes and Objects