Java/Jump statements

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
* Java Selection Statements
* Java Iteration Statements
* Java Jump Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
Java Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

[edit] Java break Keyword

In Java, the break statement has two uses. First, as you have seen, it terminates a statement sequence in a switch statement. Second, it can be used to exit a loop.


public BreakExample {
 
  public static void main(String[] args) {
 
    for(int i = 0; i < 10; i++) {
       if(i == 5) {
         System.out.println("Terminating the loop...");
         break;
       }
       System.out.println("Still in the loop");
    }
  }
}

[edit] Java continue Keyword

Continue statements are very useful in some situations where we want to check a some condition and if the condition is true then only the block of code sould be executed otherwise it should skip the code and force to continue with the loop without processing the remainder of the code.

public class ContinueExample {
 
	public static void main(String[] args) {
 
		for(int i = 0; i < 5; i++) {
 
			if(i <= 2) {
				continue;
			}
 
			// this line will be executed only when
			// the value of i is greater than 2. If it
			// is not then it will skip this code.
			System.out.println(i + " is greater than 2");
		}
	}
}

Output

3 is greater than 2
4 is greater than 2

[edit] Java return Keyword

Return statement simply returns from a method. That means that the execution control is returned back to the caller of the method. It is ideally the last statement that is executed in a method.

Try this code

public ReturnExample {
 
  public static void main(String args[]) {
 
    boolean flag = true;
 
    System.out.println("The value of the flag has been set as true.");
 
    if(flag) return;
 
    System.out.println("This line will never be printed on the screen");
  }
}


Previous Next