2024 08 20 Jumping Statements
2024 08 20 Jumping Statements
In Java
Jump Statements In Java
● Java supports three jump statements: break, continue, and return. These
statements transfer control to another part of your program.
● By using break, you can force immediate termination of a loop even though
there are iterations to be completed.
● The for loop iterates from 1 to 15 and prints the number i before the break and
after the break statement. When the value of i is 6 then the break statement is
called, which terminates (or stops) the for loop.
● That is why no number greater than 6 is printed, although i can go upto to 15.
● Also note that when the break statement is called, the code below that break
statement will not be executed and goes directly to the next statement after
the for loop. i.e. LINE C is not executed, but LINE D. Hence before = 6 is
printed but not after = 6. Also for loop completed is printed since LINE D
which is after the for loop is executed.
THINGS TO TRY
● The output of the above code will be just value of i : 0, since break is
executed after first iteration.
Try for the below code.
int i = 1;
while (i < 5)
{
System.out.println("value of i : " + i);
break;
}
***
2. break can also be used inside nested loops (loop inside loop).
Using break in switch case Statement
Here rating is 1 hence LINE A and LINE D are executed. When break is
encountered after LINE A, the control directly goes to the next statement after
switch, which is LINE D, hence prints After switch.
When rating is 2 or 3, LINE B and LINE D are executed, when it is 4, LINE C and
LINE D will be executed.
THINGS TO TRY
● Remove break statements at LINE A, LINE B, LINE C and see the output.
The output will be as shown.
Poor
Average
Good
After switch
● Since there is no break all the cases will get executed.
● Change the data type of rating to float. It shows a compilation error, since
switch cannot function with float, double and long data types.
Using Java Break Statements as Java Goto
}
} OUTPUT
Here first, second and third are labels for various blocks. In the if condition, since
t is true, break is called on the label second. So, the first statement after the
second block will be called. That statement is LINE D, which prints After second
block - inside first block. After this LINE E is called.
If break first; is called instead of break second; then the execution directly goes to
LINE E without executing LINE D
THINGS TO TRY
● Remove the break statement at LINE A to print the statements in third and
second block.
● Try the below code.
for (int i = 0; i < 5; i++)
{
outer :
{ Since we used break
inner : statement in outer block only
{ the statements in inner and
System.out.println("Inside inner block"); outer block will get executed
}
and so the output of the
System.out.println("Inside outer block");
above code will become as
break;
} shown.
} Inside inner block
Inside outer block
Using break In Nested Loop Java Program
we will print combinations of numbers until the product is less than 15.
class PrintProductCombinationsTill_15{
public static void main(String arg[]){
int number1 = 7;
int number2 = 4;
outer : for (int i = 2; i <= number1; i++) // first for loop
{
for (int k = 2; k <= number2; k++) // second for loop OUTPUT
{
int product = i * k;
2x2=4
if (product > 15) // LINE A
break outer; // LINE B 2x3=6
System.out.println(i + " x " + k + " = " + product); 2x4=8
} 3x2=6
} 3x3=9
} 3 x 4 = 12
} 4x2=8
4 x 3 = 12
DESCRIPTION
Here the outer for loop iterates with i from 2 to 7. For every value of i, the inner
loop iterates with k from 2 to 4. When the product is less than 15, the break is not
executed. When it is greater than 15, break is executed, hence terminating both
the loops.
THINGS TO TRY
● If break; is called instead of break outer; at LINE B only the internal loop
will be terminated. The outer loop will continue to run which will give the
following output. 2x2=4
2x3=6
2x4=8
3x2=6
3x3=9
3 x 4 = 12
4x2=8
4 x 3 = 12
5 x 2 = 10
5 x 3 = 15
6 x 2 = 12
7 x 2 = 14
Java continue Statement
class PrintEvenNumbers
aOUTPUT
{
public static void main(String arg[])
{
processing 1
for(int i = 1; i < 5; i++)
processing
{ 2
System.out.println("processing " + i); // LINE A
2 is even
processing
boolean 3 odd = (i % 2 == 1); OUTPUT
processing
if( odd 4
) continue; // LINE B
processing 1
4 is even processing 2
System.out.println(i + " is even"); // LINE C
} 2 is even
} processing 3
} processing 4
4 is even
DESCRIPTION
● In the above program try to print odd numbers instead of even numbers.
● Try for the below code.
int i = 0;
while (i < 5)
{
System.out.println("value of i: " + i);
i++;
if (true)
continue; // LINE A
System.out.println("value after continue: " + i); // LINE B
}
THINGS TO TRY
After LINE A is executed, the return statement is called which will be prevent
LINE B from executing. That is why we see only Before the return in the output.
We can not call return statement in the middle of method body with out having a
if condition. If there is no if condition, return will be always called, so LINE B will
never execute. The java compiler detects this scenario and shows a compilation
error unreachable statement. To prevent that error we put an if condition, but
ensured that it will be always true.
THINGS TO TRY
● Change the value of variable b from true to false and see that both Before
the return and This won't execute are printed since the return is statement is
not called.
● Replace the statement if(t) return; with return; to see the compilation error
unreachable statement
Java for loops vs Java while loops vs Java do
while loops
● Loops repeatedly executes the same set of instructions until a termination
condition is met.
● We have three loops in Java
● Working : When the loop first starts, the initialization portion is executed. Next,
condition is evaluated. If the condition is true, then the body of the loop is
executed.If it is false, the loop terminates. Next, the iteration portion of the
loop is executed.
Java for loops vs Java while loops vs Java do
while loops
while Loop In Java:
while(condtion)
{
//body of loop
}
Java for loops vs Java while loops vs Java do
while loops
do while Loop In Java:
do-while loop is similar to while loop statement but do-while loop executes all the
statements in the block, at least once and then checks the condition at the end. If
the condition is true then all the statements are re-executed. This checking of the
condition and execution of the statements/blocks continues until the condition is
false.
do {
// body of loop
} while (condition);
Java for loops vs Java while loops vs Java do
while loops
a
THANK YOU