The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Language Basics

Questions and Exercises: Control Flow

Questions

  1. Look at the SortDemo(in a .java source file) program. What control flow statements does it contain?
  2. What's wrong with the following code snippet:
    if (i = 1) {
        /* do something */
    }
    
  3. Look at the WhileDemo(in a .java source file) program and the DoWhileDemo(in a .java source file) program. What would the output be from each program if you changed the value of each program's copyFromMe string to golly gee. this is fun. Explain why you think each program will have the predicted output.

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 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 and reduce the possibility of errors by future maintainers of the code.
Check your answers.(in the Learning the Java Language trail)

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.