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

PPT
4.CONTROL STATEMENTS_MB.ppt .
PDF
Java Programming - 03 java control flow
PPTX
Lecture - 5 Control Statement
PPTX
PPT
9 cm604.13
PPT
Control statements in java programmng
PPTX
Control flow statements in java
PPTX
Java 2.pptx
4.CONTROL STATEMENTS_MB.ppt .
Java Programming - 03 java control flow
Lecture - 5 Control Statement
9 cm604.13
Control statements in java programmng
Control flow statements in java
Java 2.pptx

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

PPT
Control statements
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
PPTX
DAY_1.2.pptx
PPTX
Java covers syntax, data types, object-oriented concepts, control flow, excep...
PPT
Java control flow statements
PPTX
When a break statement is encountered inside a loop, the loop is immediately ...
PPTX
control statements
PPTX
Control Statements in Java
PDF
csj-161127083146power point presentation
PPTX
Pj01 5-exceution control flow
PPTX
Control statements in java
PPTX
dizital pods session 5-loops.pptx
PPSX
Elements of Java Language - Continued
PDF
Android Application Development - Level 3
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPTX
Java chapter 3
PDF
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
PPTX
Session 03 - Elements of Java Language
PPT
05. Control Structures.ppt
PDF
Control structures in Java
Control statements
Programming in java - Concepts- Operators- Control statements-Expressions
DAY_1.2.pptx
Java covers syntax, data types, object-oriented concepts, control flow, excep...
Java control flow statements
When a break statement is encountered inside a loop, the loop is immediately ...
control statements
Control Statements in Java
csj-161127083146power point presentation
Pj01 5-exceution control flow
Control statements in java
dizital pods session 5-loops.pptx
Elements of Java Language - Continued
Android Application Development - Level 3
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Java chapter 3
Multiple Choice Questions on JAVA (object oriented programming) bank 4 -- loops
Session 03 - Elements of Java Language
05. Control Structures.ppt
Control structures in Java
Ad

More from RithwikRanjan (13)

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
PPTX
INTERNSHIP PRESENTATION.pptx
PPTX
internship.pptx
PPTX
AM.pptx
PPT
4_A1208223655_21789_2_2018_04. Operators.ppt
PPTX
sam_technical_seminar.pptx
PPTX
sam_report.pptx
PPTX
TQM-Module 5.pptx
PPTX
CIM MODULE 2.pptx
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
PPT
0_A1590026209_21789_20_2018_0 Lecture.ppt
PPT
A457405934_21789_26_2018_Inheritance.ppt
PPT
A1869984431_21789_28_2018_Abstract Class.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
INTERNSHIP PRESENTATION.pptx
internship.pptx
AM.pptx
4_A1208223655_21789_2_2018_04. Operators.ppt
sam_technical_seminar.pptx
sam_report.pptx
TQM-Module 5.pptx
CIM MODULE 2.pptx
A2003822018_21789_17_2018_09. ArrayList.ppt
0_A1590026209_21789_20_2018_0 Lecture.ppt
A457405934_21789_26_2018_Inheritance.ppt
A1869984431_21789_28_2018_Abstract Class.ppt
Ad

Recently uploaded (20)

PPTX
Cyber Hygine IN organizations in MSME or
PPTX
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
PDF
Slides: PDF The World Game (s) Eco Economic Epochs.pdf
PDF
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
PPTX
AI_Cyberattack_Solutions AI AI AI AI .pptx
PPTX
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
PDF
Exploring The Internet Of Things(IOT).ppt
PDF
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
PPTX
Reading as a good Form of Recreation
PDF
The_Decisive_Battle_of_Yarmuk,battle of yarmuk
PPTX
Partner to Customer - Sales Presentation_V23.01.pptx
PPTX
MY PRESENTATION66666666666666666666.pptx
PDF
Computer Networking, Internet, Casting in Network
PPTX
COPD_Management_Exacerbation_Detailed_Placeholders.pptx
PPTX
The-Importance-of-School-Sanitation.pptx
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PPTX
Viva Digitally Software-Defined Wide Area Network.pptx
PPTX
Internet Safety for Seniors presentation
PDF
Alethe Consulting Corporate Profile and Solution Aproach
PPTX
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx
Cyber Hygine IN organizations in MSME or
KSS ON CYBERSECURITY INCIDENT RESPONSE AND PLANNING MANAGEMENT.pptx
Slides: PDF The World Game (s) Eco Economic Epochs.pdf
Buy Cash App Verified Accounts Instantly – Secure Crypto Deal.pdf
AI_Cyberattack_Solutions AI AI AI AI .pptx
module 1-Part 1.pptxdddddddddddddddddddddddddddddddddddd
Exploring The Internet Of Things(IOT).ppt
Session 1 (Week 1)fghjmgfdsfgthyjkhfdsadfghjkhgfdsa
Reading as a good Form of Recreation
The_Decisive_Battle_of_Yarmuk,battle of yarmuk
Partner to Customer - Sales Presentation_V23.01.pptx
MY PRESENTATION66666666666666666666.pptx
Computer Networking, Internet, Casting in Network
COPD_Management_Exacerbation_Detailed_Placeholders.pptx
The-Importance-of-School-Sanitation.pptx
Alethe Consulting Corporate Profile and Solution Aproach
Viva Digitally Software-Defined Wide Area Network.pptx
Internet Safety for Seniors presentation
Alethe Consulting Corporate Profile and Solution Aproach
1402_iCSC_-_RESTful_Web_APIs_--_Josef_Hammer.pptx

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