There are mainly three categories of control statement in Java as:
- Selection Statement
- Iteration Statement
- Jump statement
§ Selection Statement:
There are two selection statements as:
if statement:
It is conditional branch statement. It can be used to route program execution through two different paths.
Syntax:
if(condition) {
statement;
} else{
statement;
}
Notes:
☛ condition is any expression that returns boolean value.
☛ If the condition is true then statement inside the if block will be execute. Otherwise else block statement will get executed.
☛ There is no case when both block will be executed.
For example:
class If{
public static void main(String ar[]){
int a,b; // declaration of variables
a=4; b=5; // initialization of vaiables
if(a<b){
a=0;
System.out.println(a);
} // end of if block
else{
b=0;
System.out.println(b);
}//end of else block
}
}
Nested ifs:
Nested ifs itself have many if statements.
For example:
class NestedIfs{
public static void main(String ar[]){
int a,b,i,j; // declaration of variables
a=4,b=5; // initialization of vaiables
if(a<0){
if(b>20){
i=10;
System.out.println(i);
}
if(b==20){
j=120;
System.out.println(j);
}else{
i=9;j=12;
}
} // end of if block
else{
b=0;
System.out.println(b);
}//end of else block
}
}
if-else-if ladder:
it is based upon the sequence of nested ifs. Here is syntax of if-else-if;
if(condition){ Statement;}
else if(condition){statement; }
else if(condition){statement;}
...
else{ statement;}
Notes:
☛ Every if condition is checked. if condition is true then only that if block will be executed and all if conditions are bypassed.
☛ If none of if condition is true then final else block will be executed.
For example:
class ifElseIf{
public static void main(String ar[]){
int month=4;
String season;
if(month==12 || month==1 || month==2){
season=”winter”;
} else if(month==3 || month==4 || month==5){
season=”spring”;
} else if(month==6 || month==7 || month==8){
season=”summer”;
} else if(month==9 || month==10 || month==11){
season=”Autumn”;
} else{
season=”bogus month”
}
System.out.println(season);
}
}
switch statement:
switch statement provides an easy way to dispatch execution to different part of the code based on the value of expression.
Syntax of switch statement:
switch(expression){
case value1:
//statement sequence
break;
case value2:
//statement sequence
break;
...
default:
//default statement sequence
}
Notes:
☛ Each of the values specified in the case statement must be type compatible with the expression.
☛ The value of the expression is matched with each of literal value in case statement. If match found, the code sequence of case statement is executed.
☛ If none of the constant matches value of expression then default case statement is executed.
☛ The break statement is used inside switch to terminate the execution and jump out of the switch.
For example:
class Switch{
public static void main(String ar[]){
int i=1;
switch(i){
case 1:
System.out.println(“i is less than 10”);
break;
case 2:
System.out.println(“I is greator than 10”);
break;
case 3:
System.out.println(“I is equal to the 10”);
break;
default:
System.out.println(“none of the above condition”);
}
}
}
§ Iteration Statement:
Iteration statements are for, while, do-while. These statements are used for creating loops.
while loop:
It repeats a statement or block while its controlling expression (condition) is true. Here is syntax:
while(condition){
// body of loop
}
For Example:
class While{
public static void main(String ar[]){
int n=10,m=12;
while(n<m){
System.out.println(m+” is greator than ”+ n);
m--;
}
}
}
for loop:
Syntax of for loop:
for( initialization; condition; iteration){
//body of for loop
}
Notes:
☛ When the loop starts, the initialization portion executes. And the initialization expression is executed only once.
☛ Next condition is evaluated. If this condition is true, then the body of loop is executed.
☛ If condition is false then loop terminates.
☛ Next iteration portion of loop will be executed.
For example:
class ForLoop{
public static void main(String ar[]){
int i;
for(i=0;i<6;i++){
System.out.println(i);
}
}
}
do-while loop:
The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Here is syntax:
do{
//body of loop
} while(condition);
For example:
class DoWhile{
public static void main(String ar[]){
int i=1;
do{
System.out.println(i);
i++;
}
While(i<5);
}
}
§ Jump Statement:
It supports three jump statements are as : break, continue, return. these statements are used to throw or transfer control from a part of code to another portion.
break:
break statement has three uses.
- Terminates a statement sequence in a switch statement.
- Used to terminate a loop.
- Used as goto.
here is an example for using break as a goto statement:
class BreakAsGoto{
public static void main(String ar[]){
outer: for(int i=0;i<2;i++){
if(i==1){
break outer;//exit from both loop
}
System.out.println("i = "+i);
}
System.out.println("loop completes");
}
}
Note:
☛ When the inner loop breaks to outer loop, both loop have been terminated.
☛ Here if you have noticed that label for statement is must as it has a block of code as its target. and you can not break any label which is not defined for an enclosing block.
continue:
A continue statement causes control to be transferred directly to conditional expression that controls loop.
For example:
class Continue{
public static void main(String ar[]){
for(int i=0; i<9;i++){
System.out.println(i);
if(i<9){
continue;
}
System.out.println("i is greator than "+i);
}
}
}
return:
The return statement is used to explicitly return from a method. And it causes program control to return to the caller of that method.
For example:
class Return{
public static void main(String ar[]){
boolean t=true;
System.out.println("before return");
if(t){
return;
}
System.out.println("After Return"); //this line never executes
//because if statement is true then return statement is
// executed and it return to the caller of this method.
//and an error "unreachable code" will be thrown.
}
}