0% found this document useful (0 votes)
3 views31 pages

2024 08 20 Jumping Statements

The document explains the use of jump statements in Java, specifically break, continue, and return, which control the flow of execution in loops and methods. It provides examples demonstrating how these statements work in for loops, switch cases, and nested loops, highlighting their impact on program execution. Additionally, it compares different types of loops in Java, including for, while, and do-while loops.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

2024 08 20 Jumping Statements

The document explains the use of jump statements in Java, specifically break, continue, and return, which control the flow of execution in loops and methods. It provides examples demonstrating how these statements work in for loops, switch cases, and nested loops, highlighting their impact on program execution. Additionally, it compares different types of loops in Java, including for, while, and do-while loops.

Uploaded by

prajwal sri tej
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Jump 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.

● These statements can be used inside loops, switch statements and in


methods to move the next execution to some other expression.
● break can be used to exit a loop or to come out of the switch statement or to
use as a form of goto.
● continue can be used inside a loop to skip the current iteration. return can be
used to explicitly return from a method.
Using Break In for Loop To Exit

● By using break, you can force immediate termination of a loop even though
there are iterations to be completed.

● e.g., In the following program which prints numbers from 1 to 15 can be


terminated, without completing the 15 iterations by using a break statement.
class BreakForLoop{ OUTPUT
public static void main(String arg[]){ before = 1
for(int i = 1; i <= 15; i++){ after = 1
before = 2
System.out.println("before = " + i ); // LINE A
after = 2
if(i == 6) break; // LINE B before = 3
System.out.println("after = " + i ); // LINE C after = 3
} before = 4
System.out.println("for loop completed."); // LINE D after = 4
before = 5
} after = 5
} before = 6
for loop completed.
DESCRIPTION

● 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

● Check the output for the below code.


for (int i = 0; i < 5; i++)
{
System.out.println("value of i : " + i);
break;
}

● 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;
}
***

1. break can also be used inside while loop.

2. break can also be used inside nested loops (loop inside loop).
Using break in switch case Statement

● break can be used to come out of the switch statement.


class BreakSwitch{
public static void main(String arg[]){
int rating = 1; // rating out of 4
switch(rating){
case 1: System.out.println("Poor"); // LINE A
break;
case 2:
case 3: System.out.println("Average"); // LINE B
break;
case 4: System.out.println("Good"); // LINE C
break;
}
System.out.println("After switch"); // LINE D
OUTPUT
}
} Poor
After switch
DESCRIPTION

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

● Java does not support goto to avoid complex code.


● When we use lots of goto's it will be very difficult to understand that code.
● break when used with label's can be a substitute for goto.

● label is the name that identifies a block of code.


Simulate Go To using break
class GoToUsingBreak{ System.out.println("This won't execute - inside third
a
public static void main(String arg[]){ block - after break"); // LINE B
boolean t = true; }
first: {
second: System.out.println("This won't execute - inside second
{ block"); // LINE C
third: }
{
System.out.println("Before the break"); System.out.println("After second block - inside first
// LINE A block"); // LINE D
}
if(t) break second;
// break out of second block System.out.println("After first block"); // LINE E

}
} OUTPUT

Before the break


After second block - inside
first block
After first block
DESCRIPTION

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

● Comment or remove LINE A in the above program to see a compilation


error since, break should be in a if block if it is not the last sentence.

● 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

● continue stops a particular iteration in a loop.


● e.g., If a loop has 15 iterations and having 5 lines of code, if in iteration 10 the
continue statement is encountered at line 3, then the lines below that i.e.
● line 4 and 5 will not be executed, but it continues with iteration 6,7 etc.
Print even numbers

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

Here i is iterated from 1 to 5, when i = 1, it first prints processing 1 and then


variable odd is evaluated which will be true. So continue will be called at LINE B.
This will cause the control to go back to the first statement which is LINE A. LINE
C will not be executed.
Next when i becomes 2, it prints processing 2 and odd will become false, so
continue will not be called. So LINE C executes, printing 2 is even. These
iterations continue until i becomes 5 and prints the rest of output.
THINGS TO TRY

● 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

The output for the above code is :


value of i: 0
value of i: 1
value of i: 2
value of i: 3
value of i: 4
We can see LINE B is not executed because of continue at LINE A.

Remove if condition at LINE A in the above sample code to see a compilation


error since continue should be in a if block if it is not the last sentence.
Java return Statement

● return statement is used to explicitly return from a method. It causes the


program control to transfer back to the caller of the method.
● return can be used to return from any part of the method code.
class ReturnStatement
{
public static void main(String arg[])
{
boolean t = true;

System.out.println("Before the return"); // LINE A

if(t) return; // return to caller

System.out.println("This won't execute"); // LINE B OUTPUT


}
} Before the return
DESCRIPTION

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

for Loop In Java


for(initialization; condition; iteration)
{
}

● 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 loop is Java's most fundamental loop statement. It repeats a statement


or block untill the controlling expression is true.

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

You might also like