0% found this document useful (0 votes)
59 views24 pages

Branching Statements

The document discusses three branching statements in Java - break, continue, and return. It provides examples of how each statement works within for, while, and do-while loops. The break statement terminates the current block, labeled or unlabeled. The continue statement skips to the next iteration of the current loop. The return statement exits the current method and returns an optional value. Labeled versions of break and continue can target specific blocks.

Uploaded by

Meena Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views24 pages

Branching Statements

The document discusses three branching statements in Java - break, continue, and return. It provides examples of how each statement works within for, while, and do-while loops. The break statement terminates the current block, labeled or unlabeled. The continue statement skips to the next iteration of the current loop. The return statement exits the current method and returns an optional value. Labeled versions of break and continue can target specific blocks.

Uploaded by

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

CSE310: Programming in Java

Topic: Branching Statements


Outlines
• break Statement
• continue Statement
• return Statement
break Statement

 break statement:
 terminates a statement sequence in a switch statement
used to exit a loop

 The break statement has two forms:


labeled
unlabeled.
Unlabeled break
 An unlabeled break is used to terminate a for, while, or do-while loop and
switch statement.

Example 1:
class Example
{
public static void main(String[] args)
{
for(int i=0; i<100; i++)
{
if(i == 10)
break; System.out.println("i: " + i);
}
System.out.println("Loop completed");
}
}
Labeled break Statement

 Java defines an expanded form of the break statement.


break label;
 By using this form of break, we can break out of one or more blocks of
code.
 When this form of break executes, control is transferred out of the named
block.
 When this break statement is encountered with the label/name of the
loop, it skips the execution any statement after it and takes the control
right out of this labelled loop.

And, the control goes to the first statement right after the loop.
Concept
class Example Output:
{
public static void main(String[] args)
{ Outer 0
outer:
for(int i=0; i<3; i++)
Inner 0
{ Bye
System.out.println("Outer "+ i);
inner:
Inner 1
for(int j=0; j<3; j++) Bye
{
System.out.println("Inner "+j);
Inner 2
if(i== j+1) Bye
break outer;
System.out.println("Bye");
Outer 1
} Inner 0
}
}
}
NOTE

 The break statement terminates the labeled statement;


it does not transfer the flow of control to the label.

 Control flow is transferred to the statement immediately


following the labeled (terminated) statement.
continue Statement
 The continue statement skips the current iteration of a for,
while , or do-while loop.

 The unlabeled form skips to the end of the innermost


loop's body and evaluates the boolean expression that
controls the loop.
class Example Output:
{ 1
public static void main(String[] args)
{ 2
int i; 3
for(i=1;i<=10;i++) 4
{
if(i==5) 6
continue; 7
System.out.println(i); 8
}
} 9
} 10
Labeled continue Statement
 A labeled continue statement skips the current iteration of an
outer loop marked with the given label.
 In Labelled Continue Statement, we give a label/name to a
loop
When this continue statement is encountered with the
label/name of the loop, it skips the execution any statement
within the loop for the current iteration and continues with the
next iteration and condition checking in the labelled loop.
Concept
public class Main
Example
{ Output:
public static void main (String[]args)
{ i=0 j=0
loop:
for (int i = 0; i < 2; i++) i=0 j=1
{
for (int j = 0; j < 5; j++)
i=1 j=0
{ i=1 j=1
if (j == 2)
continue loop; Out of the loop
System.out.println ("i =" + i + " j =" + j);
}
}
System.out.println ("Out of the loop");
}
}
return Statement
 The return statement exits from the current method, and
control flow returns to where the method was invoked.

 The return statement has two forms: one that returns a


value, and one that doesn't.

 To return a value, simply put the value (or an expression


that calculates the value) after the return keyword.
return ++count;
return Statement
 The data type of the returned value must match the type of
the method's declared return value.

 When a method is declared void, use the form of return


that doesn't return a value.
return;
Output??
public class Main
{ A. 0,0
public static void main (String[]args)
{ 1,0
label: B. 1,1
for(int i=0; i<2; i++)
C. 0,0
{
for(int j=0; j<2; j++) D. 0,1
{ 1,1
System.out.println(i + ", "+ j);
if(j!=2)
continue label;
}
}
}
}
Ans: A
Output??
public class Main
A. 1
{
public static void main (String[]args) B. 01
{
label:
C. 10
for (int i = 0; i < 2; i++) D. 0
{
for (int j = 0; j < 2; j++)
{
if (j>0)
break label;
System.out.print(i+" ");
}
}
}
}
Ans:D
public class Main
Output??
{ A. 2
public static void main (String[]args)
{ B. 1 3 5
label1:
for (int i = 1; i <=2; i++) C. 1 3
{
label2: D. 3 5
for (int j = 2; j <=5; j++)
{
System.out.print(j+" ");
if (j%2==0)
break label1;
else
continue label2;
}

}
}
}
Ans:A
Output??
public class Main
{ A. 1
public static void main (String[]args)
{ B. 2
int i=1,j=1;
label1: C. 1 2
while(i<=2)
{ D. 2 2
i++;
while(j<=2)
{
j++;
System.out.println(i);
if(i==j)
break label1;
}
}
}
}
Ans:B

You might also like