Flow control statements
The flow control statements allow you to conditionally execute statements, to repeatedly execute a block of statements, or to just change the sequential flow of control.
if/else statement
The if/else statement is used for decision-making -- that is, it decides which course of action needs to be taken. The syntax is:
if(boolean expression)
{
// do this if the expression is true
}
else
{
// do this if the expression is false
}
The else part in the if/else statement is optional. The curly braces are optional if the body is limited to a single statement. (Note that we cannot use numeric values to represent true and false as we do in C/C++.) For instance:
if (x == 5) {} // compiles, executes body if x is equal to 5
if (x = 0) {} // does not compile, because expression is non-boolean
if (x = true) {} // compiles, but the body is always executed
In the case of nested if/else statements, each else clause belongs to the closest preceding if statement that does not have an else.
switch statement
The switch statement is also used for decision-making, based on an integer expression. The argument passed to the switch and case statements should be int, short, char, or byte. The argument passed to the case statement should be a literal or a final variable. If no case matches, the default statement (which is optional) is executed.
When a break statement is encountered, the control moves out of the switch statement. If no break statement is given, all the case statements are executed until a break is encountered or the switch statement ends. For instance:
int x = 1;
switch(x)
{
case 1: System.out.println("one");
case 2: System.out.println("two");
}
// both one and two are printed
Notice the position of the default statement. Though the default statement is usually placed at the end of all the case options, it is not mandatory as you can see in the example below:
int i = 1;
switch(i)
{
default:
System.out.println("Default");
break;
case 0:
System.out.println("Zero");
break;
case 1:
System.out.println("One");
break;
}
Note that the control comes to the default statement only if none of the cases match.
However, it is a good practice to have the default statement at the end itself.
Loop statements
The three types of Java looping constructs are while, do-while, and for.
while loop
The syntax of the while loop is:
while(boolean expression) {}
The body of the while loop is executed only if the expression is true, so it may not be executed even once:
while(false){} // body never executed
while(1) {} // code will not compile, not a boolean
do-while loop
The syntax of the do-while loop is:
do { } while(boolean expression);
The body of the do-while loop is executed at least once because the test expression is evaluated only after executing the loop body. Also, don't forget the ending semicolon after the while expression.
for loop
The syntax of the for loop is:
for(expr1; expr2; expr3)
{
// body
}
expr1 is for initialization, expr2 is the conditional test, and expr3 is the iteration expression. Any of these three sections can be omitted and the syntax will still be legal:
for( ; ; ) {} // an endless loop
There can be more than one initialization expression and more than one iteration expression, but only one test expression.
break and continue
The break statement is used to exit from a loop or switch statement, while the continue statement is used to skip just the current iteration and continue with the next.
In the case of nested loops, the break statement exits from the innermost loop only. If a break keyword is combined with a label, a break statement will exit out of the labeled loop:
outer: for (int i = 0; i < 10; i++)
{
while(y > 0)
{
break outer;
}
}
// breaks from the for loop if y > 0
1 comment: