break
).
continue
).
return
).
break:
Stops the loop completely when a condition is true.
continue:
Skips the current loop step and moves to the next one
return:
Ends the method and sends a result back, if needed.
break
statement is used to exit a loop or a switch
statement before it has completed its normal execution.
break
statement can be used to terminate loops (for
, while
, do-while
) prematurely when a specific condition is met.
switch
statements to exit a particular case and prevent the execution of subsequent cases.
break
statement stops the loop or case
execution and moves the control to the first statement outside the loop or switch
block.
break;
public class BreakExample
{
public static void main(String[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
System.out.println("Loop stopped at: " + i);
break; // Exit the loop when i equals 5
}
System.out.println("Number: " + i);
}
}
}
Number: 1 Number: 2 Number: 3 Number: 4 Loop stopped at: 5
public class BreakSwitchExample
{
public static void main(String[] args)
{
int day = 3;
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("Thrusday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Wednesday
break
statement only terminates the innermost loop it is part of.
break
wisely to avoid abrupt terminations that can make the program logic harder to follow.
continue
statement is used to skip the current iteration of a loop and move to the next iteration without completing the remaining code in the loop for that iteration.
for
loop, the increment/decrement step is executed next.
while
or do-while
loop, the condition is checked again.
continue;
public class ContinueExample
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
System.out.println("Skipping number: " + i);
continue; // Skip the rest of the code in this iteration
}
System.out.println("Number: " + i);
}
}
}
Number: 1 Number: 2 Skipping number: 3 Number: 4 Number: 5
public class ContinueWhileExample
{
public static void main(String[] args)
{
int number = 1;
while (number <= 5)
{
if (number == 3)
{
System.out.println("Skipping number: " + number);
number++; // Increment the number to avoid an infinite loop
continue; // Skip the rest of the code in this iteration
}
System.out.println("Number: " + number);
number++;
}
}
}
Number: 1 Number: 2 Skipping number: 3 Number: 4 Number: 5
continue
statement works with for
, while
, and do-while
loops.
continue
carefully, as it can sometimes make the program harder to understand.
return
statement is used to exit from a method and optionally send a value back to the method's caller.
return
keyword sends a value back to the caller.
return
: return
stops the method early.
void
: return ends the method without returning a value.
int
, String
, boolean
etc), and the method sends this value back using the return
keyword.
public int addNumbers(int a, int b)
{
return a + b; // Returns the sum of a and b
}
return
statement is used without a value to stop the method's execution and return control to the caller.
public void checkAge(int age)
{
if (age < 18)
{
return; // Exits the method early if age is less than 18
}
System.out.println("You are an adult.");
}
void
Return Type
public void greet()
{
System.out.println("Hello, World!");
return; // Stops the method here, no value is returned
}
return value; // For methods with return types, to send a value back.
return; // For void methods, to exit the method.
public class ReturnExample
{
public static void main(String[] args)
{
System.out.println("Result: " + addNumbers(5, 3)); // Calling method
}
public static int addNumbers(int a, int b)
{
int sum = a + b;
return sum; // Return the sum to the caller
}
}
Result: 8
public class ReturnVoidExample
{
public static void main(String[] args)
{
checkAge(16); // Testing with an age less than 18
// checkAge(20); // Testing with an age greater than or equal to 18
System.out.println("Voting Ended.");
}
public static void checkAge(int age)
{
if (age < 18)
{
return; // Exits the method early if age is less than 18
}
System.out.println("You can vote");
}
}
Voting Ended.
return
statement, no other statements can be written in that method because the control has already been returned to the caller.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.