Java Break Statement

  1. Use a simple break statement in Java
  2. Use break statement with switch case in Java
  3. Use inner loop break statement in Java
  4. Generate Prime numbers using break statement in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Use a simple break statement in Java


Break_Ex1.java

public class Break_Ex1 {
	
	public static void main(String args[]) {
				
		for(int i=0; i<5; i++) {
			
			if(i == 3)
				break;
			
			System.out.println("The value is : " + i);
		}
	}
}

Sample Output

The value is : 0
The value is : 1
The value is : 2

Modify below code in for loop

for(int i=0; i<5; i++) {
						
	System.out.println("The value is : " + i);
}

Sample Output

The value is : 0
The value is : 1
The value is : 2
The value is : 3
The value is : 4




B. Use break statement with switch case in Java


Break_Ex2.java

import java.util.Scanner;
 
public class Break_Ex2 {
 
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter i value : ");
		int i = scan.nextInt();
		
		switch(i) {
		
			case 0 :
				System.out.println("\nValue : " + (i + 10));
				break;
			case 1 :
				System.out.println("\nValue : " + (i + 100));
				break;
			case 2 :
				System.out.println("\nValue : " + (i + 1000));
				break;
			case 3 :
				System.out.println("\nValue : " + (i + 10000));
				break;
			case 4 :
				System.out.println("\nValue : " + (i + 100000));
				break;
			default :
				System.out.println("\nValue : " + i);
				break;
		}
	}
}

Sample Output

Enter i value : 
3

Value : 10003

Modify below code in switch case

switch(i) {
		
	case 0 :
		System.out.println("\nValue : " + (i + 10));
	case 1 :
		System.out.println("\nValue : " + (i + 100));
	case 2 :
		System.out.println("\nValue : " + (i + 1000));
	case 3 :
		System.out.println("\nValue : " + (i + 10000));
	case 4 :
		System.out.println("\nValue : " + (i + 100000));
	default :
		System.out.println("\nValue : " + i);
}

Sample Output

Enter i value : 
2

Value : 1002

Value : 10002

Value : 100002

Value : 2




C. Use inner loop break statement in Java


Break_Ex3.java

public class Break_Ex3 {
 
	public static void main(String args[]) {
				
		for(int i=1; i <= 3 ; i++) {
			
			for(int j = 1; j <= 3; j++) {
				
				if(i == 3)
					break;
				System.out.println("i : " + i + ", j : " + j);
			}
		}		
	}
}

Sample Output

i : 1, j : 1
i : 1, j : 2
i : 1, j : 3
i : 2, j : 1
i : 2, j : 2
i : 2, j : 3

Modify below code in for loop

for(int i=1; i <= 3 ; i++) {
			
	for(int j = 1; j <= 3; j++) {
 
		System.out.println("i : " + i + ", j : " + j);
	}
}

Sample Output

i : 1, j : 1
i : 1, j : 2
i : 1, j : 3
i : 2, j : 1
i : 2, j : 2
i : 2, j : 3
i : 3, j : 1
i : 3, j : 2
i : 3, j : 3




D. Generate Prime numbers using break statement in Java


Break_Ex4.java

public class Break_Ex4 {
 
	public static void main(String[] args) {
		
		int i,j;
		
		System.out.println("Prime numbers between 1 to 50 : ");
		
		for (i = 1; i< 50; i++ ) {
			
			for (j = 2; j<i; j++ ) {
				
				if(i%j == 0) {
				
					break;
				}
			}
			if(i == j) {
			
				System.out.print("  " + i);
			}
		}
	}
}

Sample Output

Prime numbers between 1 to 50 : 
  2  3  5  7  11  13  17  19  23  29  31  37  41  43  47






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com