SlideShare a Scribd company logo
1 of 18
18CS653: Programming in Java
Topic: Branching Statements
Outlines
• break Statement
• continue Statement
• return Statement
[Expected Time: 1 Hours]
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
Unlabeled break
 An unlabeled break is used to terminate a for, while, or do-while
loop and switch statement.
Example 1:
class BreakTest
{
public static void main(String agrs[])
{
for(int i=0; i<100; i++)
{
if(i == 10) break;
System.out.println("i: " + i);
}
System.out.println("Loop completed");
}
}
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.");
}
}
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.
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");
}
}
}
}
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.
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); } }
Labeled continue Statement
 A labeled continue statement skips the current iteration
of an outer loop marked with the given label.
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”); } }
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;
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");
}
}
}
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");
}
}
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;
}
}
}
6_A1944859510_21789_2_2018_06. Branching Statements.ppt

More Related Content

Similar to 6_A1944859510_21789_2_2018_06. Branching Statements.ppt

JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 

Similar to 6_A1944859510_21789_2_2018_06. Branching Statements.ppt (20)

شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Thread
ThreadThread
Thread
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
UNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptxUNIT 2 LOOP CONTROL.pptx
UNIT 2 LOOP CONTROL.pptx
 
Templates
TemplatesTemplates
Templates
 
Control statements
Control statementsControl statements
Control statements
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Nested loops
Nested loopsNested loops
Nested loops
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Exception handling
Exception handlingException handling
Exception handling
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Comp102 lec 6
Comp102   lec 6Comp102   lec 6
Comp102 lec 6
 
Java Day-5
Java Day-5Java Day-5
Java Day-5
 

More from RithwikRanjan

More from RithwikRanjan (13)

A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
 
INTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptxINTERNSHIP PRESENTATION.pptx
INTERNSHIP PRESENTATION.pptx
 
internship.pptx
internship.pptxinternship.pptx
internship.pptx
 
AM.pptx
AM.pptxAM.pptx
AM.pptx
 
4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt4_A1208223655_21789_2_2018_04. Operators.ppt
4_A1208223655_21789_2_2018_04. Operators.ppt
 
sam_technical_seminar.pptx
sam_technical_seminar.pptxsam_technical_seminar.pptx
sam_technical_seminar.pptx
 
sam_report.pptx
sam_report.pptxsam_report.pptx
sam_report.pptx
 
TQM-Module 5.pptx
TQM-Module 5.pptxTQM-Module 5.pptx
TQM-Module 5.pptx
 
CIM MODULE 2.pptx
CIM MODULE 2.pptxCIM MODULE 2.pptx
CIM MODULE 2.pptx
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
 
0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt0_A1590026209_21789_20_2018_0 Lecture.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
 
A1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.pptA1869984431_21789_28_2018_Abstract Class.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
 

Recently uploaded

6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
@Chandigarh #call #Girls 9053900678 @Call #Girls in @Punjab 9053900678
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
Diya Sharma
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
6.High Profile Call Girls In Punjab +919053900678 Punjab Call GirlHigh Profil...
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
(INDIRA) Call Girl Pune Call Now 8250077686 Pune Escorts 24x7
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Katraj ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Katraj ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 

6_A1944859510_21789_2_2018_06. Branching Statements.ppt

  • 1. 18CS653: Programming in Java Topic: Branching Statements
  • 2. Outlines • break Statement • continue Statement • return Statement [Expected Time: 1 Hours]
  • 3. 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
  • 4. Unlabeled break  An unlabeled break is used to terminate a for, while, or do-while loop and switch statement. Example 1: class BreakTest { public static void main(String agrs[]) { for(int i=0; i<100; i++) { if(i == 10) break; System.out.println("i: " + i); } System.out.println("Loop completed"); } }
  • 5. 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."); } }
  • 6. 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.
  • 7. 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"); } } } }
  • 8. 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.
  • 9. 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.
  • 10. 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); } }
  • 11. Labeled continue Statement  A labeled continue statement skips the current iteration of an outer loop marked with the given label.
  • 12. 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”); } }
  • 13. 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;
  • 14. 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;
  • 15. 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"); } } }
  • 16. 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"); } }
  • 17. 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; } } }