Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

Program Control

The following statements allow for program flow control:

  • if
  • switch
    "
    switch (grade) {
        case ‘A’:
    	System.out.println(“Great job -- an A!”);
    	break;
        case ‘B’:
    	System.out.println(“Good job -- a B!”);
    	break;
        case ‘C’:
    	System.out.println(“Your grade was a C.”);
    	break;
        default: System.out.println(“An F -- consider cheating!”);
    }
    "
    

    The break statements cause the flow of the program to break out of the case statement. It causes program flow to jump to the code outside the next closing bracket.

  • for - Syntax:
    for (initialization; test; increment) {
        statement;
    }
    
  • while - Syntax
    while (expression)
        statement;
    

    Example:

    while (n > 0) {
       n--;
       System.out.println("Value of n = ", + n);
    }
    
  • continue - Used to jump to a particular location during code execution. It can be called only from inside a loop. Syntax:

    continue labelname

    If the label name is not included, the loop condition is evaluated for possible exit.

Conditional Operator

" For example, the following conditional tests the values of myScore and yourScore, returns the larger of the two as a value, and assigns that value to the variable ourBestScore:

int ourBestScore = myScore > yourScore ? myScore : yourScore;

This use of the conditional operator is equivalent to the following if...else code:

int ourBestScore;
if (myScore > yourScore)
    ourBestScore = myScore;
else
    ourBestScore = yourScore;

"