Monday, October 17, 2011

Statement Control

An Overview of Java Statements

  1. In programming, a statement is an instruction to do something.
  2. It controls the sequence of execution of a program.
  3. In Java, a statement is terminated with a semicolon and multiple
  4. statements can be written on a single line.
x = y + 1; z = y + 2;
In Java, an empty statement is legal and does nothing:
;
Expressions

Some expressions can be made statements by terminating them with a semicolon. For example,
x++ is an expression. However, this is a statement:
x++;
Statements can be grouped in a block. A block is a sequence of the following programming
elements within braces:
  1. statements
  2. local class declarations
  3. local variable declaration statements

Declaring and defining multiple variables in a single statement 

A comma separates each variable.
public class MainClass{

  public static void main(String[] arg){
   long a = 999999999L, b = 100000000L;

   int c = 0, d = 0;     
   
   System.out.println(a);
   System.out.println(b);
   System.out.println(c);
   System.out.println(d);
  }

}
999999999
100000000
0
0

Label a statement block


  1. A statement and a statement block can be labeled.
  2. Label names follow the same rule as Java identifiers and are terminated with a colon.
public class MainClass {

  public static void main(String[] args) {
    int x = 0, y = 0;
    sectionA: x = y + 1;
  }

}
And, here is an example of labeling a block:
public class MainClass {

  public static void main(String[] args) {
    start: {
      // statements
    }
  }

}
Label statement can be referenced by the break and continue statements.


Spreading a single declaration over several lines

public class MainClass {

  public static void main(String[] arg) {
    int a = 0,  // comment for a
    b = 0,      // comment for b
    c = 0,      // comment for c
    d = 0;

    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    System.out.println(d);
  }

}
How to write multiple assignments in a single statement

public class MainClass{

  public static void main(String[] argv){
    int a, b, c; 
    
    a = b = c = 777;
    
    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
  }

}
777
777
777
 
 
Combining both statements into one 
 
class Animal {
  public Animal(String aType) {
    type = aType;
  }
  public String toString() {
    return "This is a " + type;
  }
  private String type;
}
public class MainClass {
  public static void main(String[] a) {
    System.out.println(new Animal("a").getClass().getName())// Output the
                                                              // class name
  }
}
 
Statement Blocks 
 
  1. You can have a block of statements enclosed between braces.
  2. If the value of expression is true, all the statements enclosed in the block will be executed.
  3. Without the braces, the code no longer has a statement block.
public class MainClass {

  public static void main(String[] arg) {
    int a = 0;
    if (a == 0) {
      System.out.println("in the block");
      System.out.println("in the block");
    }
  }
}
sumber disini
 


No comments:

Post a Comment