1. Break Statement practice program-1
This program prints numbers from 1 to 10. It uses the break statement to terminate the loop when the number reaches 5.
public class BreakExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}Output:
1
2
3
4
2. Break Statement practice program-2
This program searches for a number in an array. It stops searching using the break statement once the element is found.
import java.util.Scanner;
public class BreakExample2 {
public static void main(String[] args) {
int[] numbers = {12, 25, 36, 48, 59};
Scanner sc = new Scanner(System.in);
System.out.print("Enter number to search: ");
int key = sc.nextInt();
boolean found = false;
for (int num : numbers) {
if (num == key) {
found = true;
break;
}
}
if (found)
System.out.println("Element Found");
else
System.out.println("Element Not Found");
sc.close();
}
}Input and Output
Enter number to search: 36
Element Found
3. Continue Statement practice program-1
This program prints only odd numbers from 1 to 10. It skips even numbers using the continue statement.
public class ContinueExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}Output:
1
3
5
7
9
4. Continue Statement practice program-2
This program prints numbers from 1 to 15. It skips all numbers that are divisible by 3 using continue.
public class ContinueExample2 {
public static void main(String[] args) {
for (int i = 1; i <= 15; i++) {
if (i % 3 == 0) {
continue;
}
System.out.println(i);
}
}
}Output:
1
2
4
5
7
8
10
11
13
14
5. Return Statement practice program-1
This program stops execution when the entered number is negative. It uses the return statement to exit the method immediately.
import java.util.Scanner;
public class ReturnExample1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num < 0) {
System.out.println("Negative Number");
return;
}
System.out.println("Positive Number");
sc.close();
}
}Input and Output:
Enter a number: 2
Positive NumberCode language: JavaScript (javascript)
6. Return Statement practice program-2
This program checks whether a number is even. It returns the result from a separate method using the return statement.
import java.util.Scanner;
public class ReturnExample2 {
static boolean isEven(int n) {
return n % 2 == 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = sc.nextInt();
if (isEven(num))
System.out.println("Even Number");
else
System.out.println("Odd Number");
sc.close();
}
}Input and Output:
Enter Number: 16
Even NumberCode language: JavaScript (javascript)
7. Labeled Break
This program demonstrates a labeled break statement. It exits both loops immediately when the condition is satisfied.
public class LabeledBreak {
public static void main(String[] args) {
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break outer;
}
System.out.println(i + " " + j);
}
}
}
}Output:
1 1
1 2
1 3
2 1
8. Labeled Continue
This program demonstrates the labeled continue statement. It skips the remaining inner loop and continues with the next outer loop iteration.
public class LabeledContinue {
public static void main(String[] args) {
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + " " + j);
}
}
}
}Output:
1 1
2 1
3 1
9. Break in While Loop
This program prints numbers using a while loop. It terminates the loop when the value becomes 8 using break.
public class BreakWhile {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 8) {
break;
}
System.out.println(i);
i++;
}
}
}Output:
1
2
3
4
5
6
7
10. Continue in While Loop
This program prints only odd numbers from 1 to 10. It skips even numbers using the continue statement inside a while loop.
public class ContinueWhile {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
i++;
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
}
}Output:
1
3
5
7
9
