Statements

Related slides on offer

Figure 136. Statements: General syntax Slide presentation

Statement's body terminated by ;

{statement};

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

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

Notice the trailing ;.


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

Discouraged by good coding practices:

  • Poor readability

  • Hampers debugging


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

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

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

    void main() {
       int i = 1;             // i visible in current method
       IO.println("i=" + i);
       someMethod();          // Method call
    }

Figure 142. Nested blocks and variable scopes Slide presentation
Code
static void main() {

  double initialAmount = 500;

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

Figure 143. 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:

void main() {
   int a = 25;
      {
          int a = 30;
          IO.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:
static void main() {

    ...

}
Inner block:
{
   int a = 30;
   IO.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 144. Principle of swapping two variables Slide presentation

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

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

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

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

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

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

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

IO.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 146, “Swapping two variables using a block ” to perform a cyclic permutation of three variables:

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

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

// ... To be implemented ...

IO.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;

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

{// Permute i, j and k cyclically

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

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