Statements

Related slides on offer

Figure 133. Statements: General syntax Slide presentation

Statement's body terminated by ;

{statement};

Figure 134. Statement examples: Declaring and assigning variables Slide presentation
Variable declaration:
int a;
Value assignment:
a = 33;
Combined declaration and assignment:
int a = 33;

Figure 135. Expression vs. statement Slide presentation
Expression:
a - 4
Statement:
b = a - 4;

Notice the trailing ;.


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

Discouraged by good coding practices:

  • Poor readability

  • Hampers debugging


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

Figure 138. Blocks Slide presentation
double initialAmount = 34;
{ // first block
  final double interestRate = 1.2; // 1.2%
  System.out.println("Interest:" + initialAmount * interestRate / 100);
}
{ // second block
  final double interestRate = 0.8; // 0.8%
  System.out.println("Interest:" + initialAmount * interestRate / 100);
}
  • Defining scopes

  • Unit of work

  • if: Conditional block execution.

  • for / while: Repeated block execution.