examples of break and continue in java

0

break vs continue

break statement: breaks the entire loop.

continue statement: breaks the current round only.

---------------------

these are examples of it:

All Contributions

using continue statement, write java program prints even numbers between 0 to 100

---------------

answer:

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    for(int i=0;i<=100;i++)
	    {
	        if(i%2!=0)
	        continue;
	        System.out.println(i);
	    }
	}
}

example of break statement:

write java program prompts user to enter unlimited numbers, and when user enters the value -1 then program ends and print bye

-------------

answer:

import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    int value;
	    while(true)//inifinite loop
	    {
	     System.out.println("enter a number: (press -1 to exit)");
	     Scanner input=new Scanner(System.in);
	     value=input.nextInt();
	     if(value==-1)
	     break;
	     else System.out.println("you entered "+value);
	    }
	    System.out.println("bye");
	}
}

total contributions (2)

New to examplegeek.com?

Join us