Control Flow  «Prev  Next»


Lesson 2Overview of Java statements
ObjectiveIdentify and describe the programming statements that are provided by Java.

Overview of Java Statements

Since you are preparing for the Java Programmer Certification Exam and have made it this far in the course, I assume that you are familiar with the basics of Java's programming statements. The following Slide Show will help you to review them and identify which statements you need to study further. Subsequent lessons in this module will cover the important points you should know in order to do well on the exam.

1) Java Statements 1 2) Java Statements 2 3) Java Statements 3 4) Java Statements 4 5) Java Statements 5 6) Java Statements 6 7) Java Statements 7 8) Java Statements 8 9) Java Statements 9 10) Java Statements 10 11) Java Statements 11 12) Java Statements 12 13) Java Statements 13 14) Java Statements 14 15) Java Statements 15 16) Java Statements 16

Java Statements
In this module, I use the term statement block[1] to refer to either a single semicolon-terminated statement or a group of statements surrounded by braces.

Modern Java

For Loop Question

What will the following program snippet print?
public class ProgramSnippet {
 public static void main(String args[]) {
  int i = 0, j = 11;
  do {
   if (i > j) {
    break;
   } // end - if
   j--;
  }// end - do
  while (++i < 5);
   System.out.println(i + "  " + j);
 } // end -main
}

Select 1 option:
  1. 5 5
  2. 5 6
  3. 6 6
  4. 6 5
  5. 4 5

Answer: b
Explanation: ++i < 5 means, increment the value of i and then compare with 5.
Now, try to work out the values of i and j at every iteration.
To start with, i=0 and j=11. At the time of evaluation of the while condition, i and j are as follows:
  1. j = 10 and i=1 (loop will continue because i <5) (Remember that comparison will happen AFTER increment i because it is ++i and not i++.
  2. j = 9 and i=2 (loop will continue because i<5).
  3. j = 8 and i=3 (loop will continue because i<5).
  4. j = 7 and i=4 (loop will continue because i<5).
  5. j = 6 and i=5 (loop will NOT continue because i not <5).
So it will print 5 6. (It is print i first and then j).
[1] Statement block: A sequence of statements enclosed in brackets.