Java Continue Statement

  1. Use a simple continue statement in Java
  2. Use continue statement with a label in Java
  3. Continue Statement usage in Java
  4. Generate Pascal triangle with continue 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 continue statement in Java


Continue_Ex1.java

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

Sample Output

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




B. Use continue statement with a label in Java


Continue_Ex2.java

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

Sample Output

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




C. Continue Statement usage in Java


Continue_Ex3.java

public class Continue_Ex3 {
 
	public static void main(String[] args) {
		
		for(int i=0; i<10; i++) { 
			
			System.out.print(i + " "); 
			
			if (i%2 == 0) 
				continue; 
			
			System.out.println(""); 
		} 
	}
}

Sample Output

0 1 
2 3 
4 5 
6 7 
8 9 




D. Generate Pascal triangle with continue statement in Java


Continue_Ex4.java

public class Continue_Ex4 {
	
	public static void main(String args[]) { 
		
		outer: 
			for (int i=1; i<=5; i++) { 
				
				for(int j=1; j<=5; j++) { 
					
					if(j > i) { 
						
						System.out.println(); 
						continue outer; 
					} 
					System.out.print(" " + (i * j)); 
				} 
			} 
	System.out.println(); 
	} 
}

Sample Output

 1
 2 4
 3 6 9
 4 8 12 16
 5 10 15 20 25






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com