Java #9 - Control Statements
Selection statement | Iteration statement | Jump statement
A programming language uses conditional statements (boolean expressions) to determine the execution path.
Selection Statement
Java supports two selection statements: if and switch. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.
if
The if statement is used to route program execution through a different path.
if (condition){ // The condition is any expression that returns a boolean value.
statement1; // When condition is true this block is executed
}
else if(condition2){
statement2;
}
else{
statement3; // When condition is false, this block is executed.
}
Example
public class IfElseDemo {
public static void main(String[] args) {
int number = 10;
if(number % 2 == 0){
System.out.println(number + " is even number.");
}
else{
System.out.println(number + " is odd number.");
}
}
}
switch
The switch statement is used to dispatch execution to different parts of your code based on the value of an expression.
switch(expression){
case value1:
//statement
break;
case value2:
//statement
break;
case value3:
//statement
break;
default:
}
- expression must resolve to byte, short, int, char, String or an enumeration (From JDK 7)
- Each value specified in the case statements must be unique constant expression (such as literal value). Duplicate case values are not allowed.
- The type of each value must be compatible with the type of expression.
- The break statement is optional, if you omit the break, execution will continue on into the next case.
- The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of Boolean expression.
Example
public class SwitchDemo {
public static void main(String[] args) {
int month = 4;
String season;
switch (month){
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Invalid month";
}
System.out.println("April is in the season " + season + ".");
}
}
Iteration Statement
Java iteration statements are: for, while and do-while. A loop repeatedly executes the same set of instructions until the termination condition is met.
while
while(condition){ // condition can be any boolean expression
//statements
}
do-while
do{
// statements
}while(condition);
for
for(initialization;condition;itreation){
// statements
}
- When the loop first starts, the initialization portion of the loop is executed. The initialization expression is executed only once.
- Next, the condition is evaluated, if the condition is true then the body of the loop is executed.
- Next, the iteration portion of the loop is executed.
- The loop then iterates, first evaluating the conditional expression, then executing the body of the loop and then executing the iteration expression. This process repeats until the controlling expression is false.
The For-Each version of the for loop
A for-each loop is designed to cycle through a collection of objects, such as array, from start to finish.
for(type itr-var:collection){ // type must be same as the element stored in the collection.
//statements
}
Example
public class ForEachDemo {
public static void main(String[] args) {
int nums[] = {1,2,3,4,5,6,7,8,9,10};
int sum = 0;
for ( int x : nums){
sum += x;
}
System.out.println(sum);
}
}
Jump Statements
break
It is used to terminate loop and switch block. When break statement is encountered inside a loop, the loop is terminated and program control resumes the next statement following the loop.
continue
- It is used for an early iteration of a loop, i.e. to continue running the loop but stop processing the remainder of the code in its body for this particular iteration.
- In while and do-while loops, the continue statement causes control to be transferred to a conditional expression that controls the loop.
- In a for loop, control first goes to the iteration portion of for statement and then to the conditional expression.
return
The return statement is used to explicitly return from a method. It causes program control to transfer back to the caller of the method.
Example
public class JumpStatementDemo {
public static void main(String[] args) {
for(int i = 0; i < 100; i++){
if(i % 2 == 0) continue; // if i is divisible by 2, iteration will be skipped
if(i > 15) break; // loop will be terminated when i > 15
System.out.println(i);
}
}
}