Basics, part 2.

A statement is a line of code. It is like a sentence in English, but for computers to execute.
Types of Statements:
A statement is either an expression statement or a control flow statement.
- Expressions: variable declaration is an expression statement in which you create a variable. The following code snippet demonstrates several lines of variable declarations:

int a; 
int a = 5;
boolean b = false.

Method invocations (when you execute a method) is another type of expression.
- Control Flow: control flow or conditional statement uses if-else statements, switch statements, or loops to alter the flow of a program. Generally, a program executes each line of code one-by-one in order (from top to bottom). A control flow statement, however, can change this behavior and direct a program on different paths.
The easiest example is the use of an if-statement. If the condition is met for the if-statement then the indicated block of code will execute, otherwise, it will not:

if (true) { b = 5 + 9; } 

Grouping Statements:
You can group several statements together using curly braces ( {…} ). You can place the curly brace anywhere a single statement can be placed. Typically, control flow statements use curly braces to group individual statements.

if (condition) { b = 5 + 9; b++ … } else { … } 

NOTE: You can place the curly brace on the same line or on a new line. For example, the above snippet can be written in any of the following ways:
Java Statements can be written any of the following ways:

if (condition) { 
b = 5 + 9; b++ …
} else {

-OR if (condition) { b = 5 + 9; b++ … } else {…}

Generally, you’ll follow one convention and not mix the two.
Switch Statements:
The switch statement is a special format of a conditional or control flow statement. Instead of writing several if-statements based on a single value, you can use the switch statement and write cases for each value. The data type of the value can be any primitive or String type.

switch (value) { case 1: … case 2: … case 3: … } 

For each case, you typically use a break statement to exit the rest of the switch block.

switch (value) { 
case 1: System.out.println(“…”);
case 2: …
case 3: …

You can also specify a default case to execute if none of the other values are met.

switch (value) { case 1: … case 2: … case 3: … default: … } 

An example of usage of a switch statement follows:

int a = 6; 
case 5: System.out.println(“16”);
case 6: System.out.println(“Done”);
default: switch(a) { }

For Loops:
The for-loop is another control flow statement that is used to repeatedly execute a block of code a specified number of times. Instead of writing the same statement multiple times, you would use a for-loop. An example of the syntax of the for-loop is as such:

for (initialization; termination; increment){ 
//statements to execute

You can see that the signature of a for-loop has 3 different blocks. Each block corresponds to a different point in time during the execution of the for-loop. Initialization Block - is the first block of the for-loop. It executes only once and prior to any statements or other blocks associated with the for-loop. Typically, you’ll create a variable that acts as a counter in this statement Termination Block- block executes during every “turn” or iteration of the for-loop. It decides whether or not to run the code within the body of the for-loop. NOTE: If this block evaluates to false before the first “turn” or iteration of the for-loop, then the code within the body of the for-loop will not execute. Increment Block - is executed at the end of each iteration of the for-loop. Typically, you update the variable that you defined as a counter in the initialization block.
The below code demonstrates a for-loop that will execute its body 6 times.

for (int i = 0; i 1) 

NOTE: The above code’s condition evaluates to false on the first iteration of the loop. Since it is a do-while loop, though, the body of the loop will execute at least once.

Break Statements:
The break statement is another control flow statement that is used to exit the current context of the code. It is used only within loops and switch statements and consists of only the Java keyword break followed by a semicolon.
Example Break Statement Use

for (int i = 1; i < 8; i++){ 
if (i == 5) break;

NOTE: If you try to use a break statement outside of a loop or switch statement, then you’ll get a compiler error.