0% found this document useful (0 votes)
32 views18 pages

Branching Statements

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)
32 views18 pages

Branching Statements

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/ 18

CSE310: Programming in Java

Topic: Branching Statements

By
Ravi Kant Sahu
Asst. Professor, LPU
Outlines [Expected Time: 1 Hours]

• break Statement
• continue Statement
• return Statement

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
break Statement

 break statement has three uses:


 terminates a statement sequence in a switch statement
used to exit a loop
used as a “civilized” form of goto.

 The break statement has two forms:


labeled
unlabeled.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Unlabeled break
 An unlabeled break is used to terminate a for, while, or do-while loop and switch statement.

Example 1:
public void breakTest()
{
for(int i=0; i<100; i++)
{
if(i == 10) break;
System.out.println("i: " + i);
}
System.out.println("Loop completed");
}

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
public void switchTest() {
for(int i=0; i<5; i++)
switch(i) {
case 0:
System.out.println("i is zero."); break;
case 1:
System.out.println("i is one."); break;
case 2:
System.out.println("i is two."); break;
default:
System.out.println("i is greater than 2.");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class BreakDemo{
public static void main(String [] rk)
{
outer:
for(int i=0; i<3; i++){
System.out.println("Outer "+ i);
inner:
for(int j=0; j<3; j++)
{
System.out.println("Inner "+j);
if(i== j+1)
break outer;
System.out.println("Bye");
}
}
}
} Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class ContinueDemo {
public static void main(String[] rk) {
String str = “she saw a ship in the sea”;
int size = str.length();
int count = 0;
for (int i = 0; i < size; i++)
{
if (str.charAt(i) != ‘s’)
continue;
count++;
}
System.out.println(“Number of s in “+ str + “ = ”+ count); } }

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Labeled continue Statement

 A labeled continue statement skips the current iteration


of an outer loop marked with the given label.

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class ContinueLabel {
public static void main(String [] rk) {
outer: for (int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(j > i) {
System.out.println(“Hi”);
continue outer; }
System.out.print(" " + (i * j));
}
}
System.out.println(“Done”); } }
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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;

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
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;

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 1
public static void main(String [] rk){
outer:
for(int i=0; i<3; i++)
{
inner:
for(int j=0; j<3; j++)
{
System.out.println(i + ", "+ j);
if(j==2) break inner;
if(i==j) continue outer;
}
System.out.println("Bye");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 2
class BreakTest{
public static void main(String [] rk)
{
hello:
for(int a=1; a<3; a++)
System.out.print("Hello");
int i = 1;
if(i==1)
break hello;
System.out.print("Not reachable");
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Brainstorming 3
public static void main(String [] rk)
{ int n=5;
outer: for(int a=1; a<5; a++)
{ int i=0, j=0; System.out.println();

space: while(true) {
System.out.print(" "); i++;
if(i==n-a) break space; }

star: while(true) {
System.out.print(" * "); j++;
if(j==a) continue outer;
}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

You might also like