站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Answers to Questions and Exercises: Control Flow Statements - Java Tutorial 5.0 英文版

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

Trail: Learning the Java Language
Lesson: Language Basics

Answers to Questions and Exercises: Control Flow Statements

Answers to Questions

  1. Question: Look at the SortDemo (in a .java source file) program. What control flow statements does it contain?
    Answer: SortDemo contains four control statements. In order: two for statements, an if statement, and another for statement.
  2. Question: What's wrong with the following code snippet:
    if (i = 1) {
        /* do something */
    }
    

    Answer: It should use == instead of =. If you try to compile the preceding code, the compiler warns you that a boolean value is required instead of an int value.
  3. Question: 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.
    Answer: NewWhileDemo (in a .java source file) and NewDoWhileDemo (in a .java source file). The modified WhileDemo displays a blank line because the first letter is a g, and thus the loop is not entered and no characters are appended to copyToMe. The modified DoWhileDemo displays golly because the first g is ignored, and the second one terminates the loop without adding the second g to the string.

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. Exercise: What output do you think the code will produce if aNumber is 3?
      Solution:
      second string
      third string
      
    2. Exercise: 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?
      Solution: NestedIf (in a .java source file)
      second string
      third string
      
      3 is greater than or equal to 0, so execution progresses to the second if statement. The second if statement's test fails because 3 is not equal to 0. Thus, the else clause executes (since it's attached to the second if statement). Thus, second string is displayed. The final println is completely outside of any if statement, so it always gets executed, and thus third string is always displayed.
    3. Exercise: Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
      Solution:
      if (aNumber >= 0)
          if (aNumber == 0)
              System.out.println("first string");
          else
              System.out.println("second string");
      
      System.out.println("third string");
      
    4. Exercise: Use braces { and } to further clarify the code and reduce the possibility of errors by future maintainers of the code.
      Solution: Adhering to the Sun code conventions:
      if (aNumber >= 0) {
          if (aNumber == 0) {
              System.out.println("first string");
          } else {
              System.out.println("second string");
          }
      }
      
      System.out.println("third string");
      

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

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