Branching Statements
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
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
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
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.
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
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.
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.
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)