Statements

Related slides on offer

Figure 135. Statements: General syntax Slide presentation

Statement's body terminated by ;

{statement};

Figure 136. Statement examples: Declaring and assigning variables Slide presentation
Variable declaration:
int a;
Value assignment:
a = 33 * b;
Method invocation
System.out.println("Hello");          

Figure 137. Expression vs. statement Slide presentation
Expression:
3 * (a - 4)
Statement:
b = 3 * (a - 4);

Notice the trailing ;.


Figure 138. Multiple statements per line Slide presentation
a = b + 3; b = a - 4;

Discouraged by good coding practices:

  • Poor readability

  • Hampers debugging


Figure 139. Debugging multiple statements per line Slide presentation
Debugging multiple statements per line

Figure 140. Method local variable scope Slide presentation
  • Method scope being delimited by the { ... } block

  • A variable's visibility is restricted to its block:

    public class X {
    
      public static void main(String[] args) {
        int i = 1; // Visible in current method
        System.out.println("i=" + i);
        someMethod(); // Method call
      }
    }

Figure 141. Nested blocks and variable scopes Slide presentation
Code
public static void main(String[] args) {

  double initialAmount = 500;

  { // first block
    final double interestRate = 2.0; // 2.0 %
    System.out.println(""First block interest:" + 
         initialAmount * interestRate / 100";
  }
  System.out.println("Second block");
  { // second block
    final double interestRate = 1.0; // 1.0 %
    System.out.println("Second block interest:" + 
         initialAmount * interestRate / 100);
  }
}
Result
First block interest:10.0
Second block interest:5.0

Figure 142. Block purposes Slide presentation
  • Defining variable scopes

  • Unit of work

  • if: Conditional block execution.

  • for / while: Repeated block execution.


exercise No. 51

Blocks and variable glitches

Q:

We consider:

public static void main(String[] args) {
   int a = 25;
      {
          int a = 30;
          System.out.println(a);
      }
}

This code does not compile due to an Variable 'a' is already defined in the scope error.

Why is that? Both definitions happen in different blocks.

A:

We do indeed have two blocks:

main() block:
public static void main(String[] args) {

    ...

}
Inner block:
{
   int a = 30;
   System.out.println(a);
}

However the inner block is nested inside the main block and thus inherits all variable declarations. It thus cannot redefine or shadow variable a from its enclosing parent.

Figure 143. Principle of swapping two variables Slide presentation

Figure 144. Swapping two variables Slide presentation
Code
int i = 1,
    j = 2;

System.out.println("Before: i = " + i + ", j = " + j);               

// Swapping i and j
int tmp = i;
i = j;
j = tmp;

System.out.println("After: i = " + i + ", j = " + j);
// Variable tmp still visible
Result
Before: i = 1, j = 2
 After: i = 2, j = 1

Figure 145. Swapping two variables using a block Slide presentation
Code
int i = 1,
    j = 2;

System.out.println("Before: i = " + i + ", j = " + j);               

{
   // Swapping i and j
   int tmp = i;
   i = j;
   j = tmp;
}

System.out.println("After: i = " + i + ", j = " + j);
// Variable tmp out of scope
Result
Before: i = 1, j = 2
After: i = 2, j = 1

exercise No. 52

Cyclic variable permutation

Q:

Extend Figure 145, “Swapping two variables using a block ” to perform a cyclic permutation of three variables:

Code
int i = 1,
    j = 2,
    k = 3;

System.out.println("Before: i = " + i + ", j = " + j + ", k = " + k);

// ... To be implemented ...

System.out.println("Before: i = " + i + ", j = " + j + ", k = " + k);
Result
Before: i = 1, j = 2, k = 3
Before: i = 2, j = 3, k = 1

A:

int i = 1,
    j = 2,
    k = 3;

System.out.println("Before: i = " + i + ", j = " + j + ", k = " + k);

{// Permute i, j and k cyclically

    int tmp = i;
    i = j;
    j = k;
    k = tmp;
}

System.out.println("Before: i = " + i + ", j = " + j + ", k = " + k);