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 series of images 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.


Overview of Java Statements

class StatementSampler {

	public static void main(String[] args) {
		String s;
		s = "Java 2 Certification";
		System.out.print(s);
		if (s.length() > 10)
			System.out.println("!");
		else
			System.out.println(".");
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case 'J':
				String time = "Time";
				int j = 0;
				while (j < time.length()) {
					System.out.print(time.charAt(j));
					j++;
				}
			}
		}
	}
}
1) Assignment Statement in Java: The value of the variable is updated with the result of the expression.


2) Method invocation statement: The print() method of the out object is invoked, passing the value of s as an argument.
2) Method invocation statement: The print() method of the out object is invoked, passing the value of s as an argument.

3) Declaration statement: The s variable is declared as being off the String type.
3) Declaration statement: The s variable is declared as being of the String type.

4) if-statement: The condition in an if statement must be a Boolean expression. The else clause may be omitted.
class StatementSampler {

  public static void main(String[] args) {
      String s;
      s = "Java 2 Certification";
      System.out.print(s);
      if (s.length() > 10)
          System.out.println("!");
      else
          System.out.println(".");
      for (int i=0; i < s.length(); i++) {
          char c = s.charAt(i);
          switch (c) {
              case 'J':
                  String time = "Time";
                  int j = 0;
                  while (j < time.length()) {
                      System.out.print(time.charAt(j));
                      j++;
                  }
          }
      }
  }
}
4) if-statement: The condition in an if statement must be a Boolean expression. The else clause may be omitted.

5) Switch statement: The switch value and the case values must evaluate to int values after numeric promotion.
5) Switch statement: The switch value and the case values must evaluate to int values after numeric promotion.

6) Switch statement (continued)- If a break statement is executed, execution continues after the switch statement.
6) Switch statement (continued)- If a break statement is executed, execution continues after the switch statement.

7)For statement: The code surrounded by the for statement is executed for values of i from 0 to the length of s minus 1.
7)
for (int i=0; i < s.length(); i++) {
    char c = s.charAt(i);
    switch (c) {
        case 'J':
            String time = "Time";
            int j = 0;
            while (j < time.length()) {
                System.out.print(time.charAt(j));
                j++;
            }
            break;
        case '2':
            String two = "Two";
            int k = 0;
            char ch;
            do {
                k++;
                ch = two.charAt(k);
                if (ch == 'w')
                    continue;
                System.out.print(ch);
            } while (k < two.length());
    }
}

7) For statement: The code surrounded by the for statement is executed for values of i from 0 to the length of s minus 1.

8) End of for statement: This is where the for statement ends.
8) End of for statement: This is where the for statement ends.


9) While statement: First, the condition is evaluated. If the condition is true, the nested statements are executed. If it is false, the statement after the while statement is executed. When the nested statements have executed, the condition is evaluated again.
9) While statement: First, the condition is evaluated. If the condition is true, the nested statements are executed. If it is false, the statement after the while statement is executed. When the nested statements have executed, the condition is evaluated again.

10) Do statement: The nested statements are executed. The condition is evaluated. If the condition is true, the nested statements are executed again. If the condition is false, the next statement after the do statement is executed.
10) Do statement: The nested statements are executed. The condition is evaluated. If the condition is true, the nested statements are executed again. If the condition is false, the next statement after the do statement is executed.

11) Break statements: Breaks without labels terminate execution of an enclosing switch, for, while, or do statement. Breaks with labels terminate execution of an enclosing statement with a matching label.
11) Break statements: Breaks without labels terminate execution of an enclosing switch, for, while, or do statement. Breaks with labels terminate execution of an enclosing statement with a matching label.

12) Continue statements: The continue statement terminates the current loop iteration (for, while, or do statement). A label may be used to identify the loop whose iteration is being terminated.
12) Continue statements: The continue statement terminates the current loop iteration (for, while, or do statement). A label may be used to identify the loop whose iteration is being terminated.

13) Return statement: Return statements return the value of an expression of the method's return value. If a method's return type is void, then the return statement does not return a value.
13) Return statement: Return statements return the value of an expression of the method's return value. If a method's return type is void, then the return statement does not return a value.

14) Throw statement: A throw statement throws an exception, which is an object of a subclass of java.lang.Throwable.
14) Throw statement: A throw statement throws an exception, which is an object of a subclass of java.lang.Throwable.

15) Try-catch statements: An exception that is thrown in the try clause may be caught and handled by a catch clause.
15) Try-catch statements: An exception that is thrown in the try clause may be caught and handled by a catch clause.

16) Synchronized statements: The lock on the object being synchronized must be acquired before the synchronized statement is executed.
16) Synchronized statements: The lock on the object being synchronized must be acquired before the synchronized statement is executed.

Modern Java
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.

SEMrush Software