There are mainly three categories of control statement in Java as:

  1. Selection Statement
  2. Iteration Statement
  3. Jump statement

 

  1. Selection Statement :

There are two selection statements that Java supports:

  1. 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;

}

     Note:

  • condition is any expression that returns boolean value.
  • if statement works like as: If the condition is true the statement inside the if block is executed. Otherwise else block statement is 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

// initialization of vaiables

a=4;

b=5;

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.

Here is an example:

class  NestedIfs{

public static void main(String ar[]){

int a,b,i,j; // declaration of variables

// initialization of vaiables

a=4,b=5;

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);

}

}

  1. 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

}

      Note points:

  • 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”);

}

}

}

  1. Iteration Statement:

Iteration statements are for, while, do-while. These statements are used for creating loops.

  1. 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–;

}

}

}

  1. for loop:

Syntax of for loop:

for( initialization; condition; iteration){

//body of for loop

}

     Note: points to be noted in for loop as:

  • 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 is executed.

For example:

class ForLoop{

public static void main(String ar[]){

int i;

for(i=0;i<6;i++){

       System.out.println(i);

}

}

}

  1. 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);

}

}

  1. Jump Statement:

Java supports three jump statements are as : break, continue, return. these statements are used throw or transfer control from part of code to another portion.

     1. break:

break statement has three uses.

         a. it terminates a statement sequence in a switch statement.

b. it can be used to terminate a loop.

c. it can be 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 u have to notice that label the 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.

   2.  continue:

A continue statement causes control to be transferred directly to conditional expression that controls loop. here is an 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);

}

}

}

   3.  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.

}

}

7 Replies to “Control Statements in Java and their Examples”

  1. I see your blog needs some fresh posts. Writing
    manually takes a lot of time, but there is tool for this boring task,
    search for; unlimited content Wrastain’s tools

  2. I see you don’t monetize your website, don’t waste your traffic, you can earn additional
    bucks every month because you’ve got high quality content.
    If you want to know how to make extra bucks, search for: best adsense alternative Wrastain’s tools

  3. I see your blog needs some fresh posts. Writing manually takes a lot of time,
    but there is tool for this boring task, search for; Wrastain’s tools for content

Leave a Reply

Your email address will not be published. Required fields are marked *