Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Break in Loop

Break in Loop : ek control flow statement hai jo Java mein istemaal hota hai ek loop ko achanak se terminate karne ke liye. Aam taur par ise switch statements aur loop constructs jaise ki for, while, aur do-while loops mein istemaal kiya jaata hai.

Yahaan break ke kuch mukhya upyog hain:

1. Loops Ke Andar:

Jab kisi loop ke andar break statement aati hai, tab woh loop turant khatam ho jaata hai, aur program control agle statement par chala jaata hai jo loop ke baad aata hai.

Example:

				
					for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break; // Loop tab khatam ho jaata hai jab i 5 ke barabar ho jaata hai
    }
    System.out.println(i);
}
				
			

Is Example: mein, loop tab khatam ho jaata hai jab i 5 ke barabar ho jaata hai, aur program loop ke baad ki statement ko execute karta hai.

2. Switch Statements Ke Andar:

break statements ko switch statements ke andar bhi istemaal kiya jaata hai. Jab ek break statement switch block ke andar aata hai, tab program control switch block se baahar nikal jaata hai.

Example:

				
					int choice = 2;
switch (choice) {
    case 1:
        System.out.println("Choice is 1");
        break;
    case 2:
        System.out.println("Choice is 2");
        break; // Is case ke execute hone ke baad switch block se bahar nikalta hai
    case 3:
        System.out.println("Choice is 3");
        break;
    default:
        System.out.println("Invalid choice");
}
				
			

Conciseness ke liye Ternary Operator

Saral if-else scenarios ke liye, concise aur padhne layak code ke liye, ternary operator ka use consider karein.

				
					result = (condition) ? trueValue : falseValue;
				
			

Is Example mein, jab choice 2 hota hai, tab us case ka block execute hota hai aur fir break statement switch block se bahar nikal jaata hai.

break statement ek prabhavshaali tool hai Java program mein execution flow ko niyantrit karne ke liye, jo aapko kuchh sharton par loops aur switch blocks ko chhodne ki anumati deta hai.

Certainly! Here are more examples of how the break statement is used in Java:

1. Using break in a while Loop:

				
					int i = 0;
while (true) {
    if (i == 5) {
        break; // Loop terminates when i is equal to 5
    }
    System.out.println("Value of i: " + i);
    i++;
}
				
			

2. Using break in a do-while Loop:

				
					int i = 0;
do {
    if (i == 3) {
        break; // Loop terminates when i is equal to 3
    }
    System.out.println("Value of i: " + i);
    i++;
} while (i < 5);
				
			

 Using break in a switch Statement with a Default Case:

				
					int day = 5;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    case 4:
        System.out.println("Thursday");
        break;
    case 5:
        System.out.println("Friday");
        break;
    default:
        System.out.println("Weekend");
        break; // Exits the switch block after executing the default case
}
				
			

4. Using Nested Loops with break:

				
					for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.println("i: " + i + ", j: " + j);
        if (j == 1) {
            break; // Inner loop terminates when j is equal to 1
        }
    }
}
				
			

These examples demonstrate various scenarios where the break statement is used to control the flow of execution in Java programs, either terminating loops prematurely or exiting switch blocks based on certain conditions.

Leave a comment