A browser with JavaScript enabled is required for this page to operate properly.
Trail: Learning the Java Language
Lesson: Language Basics
Questions and Exercises
Home Page > Learning the Java Language > Language Basics

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


Problems with the examples? Try Compiling and Running the Examples: FAQs.
Complaints? Compliments? Suggestions? Give us your feedback.

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