Sunday, January 22, 2012

JAVA Programming : Break, Continue And Return

JAVA Break, Continue And Return


Java has three type of branching control keywords: break, continue and return. These keywords are used to change the normal flow of execution of program.

Break keyword :
The break keyword is used to stop the execution of the loops. In many situations we need to get out of the loop before the loop execution is complete. Whenever a break keyword is encountered inside a loop the execution gets out of loop and start executing the next line of code after the loop. The break statement is of two types: labeled and unlabeled.

Unlabeled break statement:
Unlabeled break statement is used to get out of a single loop. If unlabeled break is used inside the nested loops it will exit only from the loop in which break is used. Lets see the example below:

public class UnlabeledBreak {
  public static void main(String[] args) {
    int i = 1;
    while (i < 10) {
      if (i == 5) {
        break;
      }
      System.out.println("i = " + i);
      i++;
    }
  }
}

Output of Program:

Labeled Break Statement: 
Labeled breaks statement is used when we need to break the nested loops. Lets understand the difference between Labeled and Unlabeled break statement with an example.
In this example we need to traverse a 2D array and print the elements of array until a particular element is found. To do this we have to use nested loops. Now to break and get out of the nested loops we need to use labeled break.

public class LabeledBreak {
  public static void main(String[] args) {
    int[][] a = {{1,2,3},{4,5,6},{7,8,9}};
    OuterLoop:
    for (int i = 0; i < a.length; i++) {
      for (int j = 0; j < a[0].length; j++) {
        System.out.println(a[i][j]);
        if(a[i][j]==6){
          break OuterLoop;
        }
      }
    }
    System.out.println("Program Ends");
  }
}

Output of Program:-


Continue Keyword:- Continue statement statement is used to skip a single iteration of a loop. Whenever a continue keyword is encountered inside a loop, the current iteration of loop ends just like a normal end of any iteration without executing the statement written after the continue statement inside the loop. Just like break statement continue statement is also of two types: labeled and unlabeled.

Unlabeled Continue Statement:

public class UnlabeledContinue {
  public static void main(String[] args) {
    int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    System.out.println("Numbers divisible by 3 or 5");  
    for (int i = 0; i < a.length; i++) {
      if(a[i]%3!=&& a[i]%5!=0){
        continue;
      }
      System.out.println(a[i]);
    }
  }
}

Output of Program:

Labeled Continue Statement:

public class ContinueLabel {
  public static void main(String args[]) {
    outer: 

    for (int i = 1; i < 10; i++) {
      for (int j = 1; j < 10; j++) {
        if (j > i) {
          System.out.println();
          continue outer;
        }
        System.out.print((i)+" ");
      }
    }
    System.out.println();
  }
}

Output of Program:-

Return Keyword:- Return statement is a bit different form break and continue as break and continue changes the normal control flow of loops but return statement works on the control flow of the method. Return statement stops execution of the method and transfer the control back to the caller of the method. The return statement can be used in two ways,  return statement that returns a value and another that does not return a value. Return statement must return the same type of value as defined in the declaration of method. If the method is defined as void than return statement will not return any value.

public class ReturnStatement {

  public static void main(String[] args) {
    ReturnStatement rs = new ReturnStatement();
    System.out.println("Largest Number is: " + rs.largestNumber(51015));
  }

  public int largestNumber(int num1, int num2, int num3) {
    int largest = 0;
    if ((num1 > num2&& (num1 > num3)){
      largest = num1;

    }
    else if ((num2 > num3&& (num2 > num1)){
      largest = num2;

    }
    else{
      largest = num3;

    }
    return largest;
  }
}

The use of return statement will be more clear in upcoming tutorials and we will be using it more and more now.


9 comments:

  1. Plz give email ,i want some ip java project with my sql as backend for my project have sumbit on 28 ,plz contact me ,my
    Email-shivamkumar75@gmail.com
    Plz give warna mere prac. Ke no to gaye.

    ReplyDelete
    Replies
    1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students. or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

      Delete
  2. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  3. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Digital Marketing Training in Chennai

    Digital Marketing Training in Bangalore

    digital marketing training in tambaram

    digital marketing training in annanagar

    ReplyDelete
  4. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    full stack developer training in annanagar

    full stack developer training in tambaram

    full stack developer training in velachery

    ReplyDelete
  5. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training institute in chennai
    python training in Bangalore
    python training institute in chennai

    ReplyDelete
  6. Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.

    Blueprism training in Chennai

    Blueprism training in Bangalore

    ReplyDelete
  7. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    Data Science training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

    ReplyDelete