SlideShare a Scribd company logo
Control Flow Statements
Branching Statements
Looping Statements
Jump Statemetns
Branching Statements
• If statement
• If else Statement
• If..else…if statement
• Nest if…else statement
• Switch Statement
<<goTo first slide>>
If Statement
• An if statement consists of a Boolean expression followed by one or more
statements. If the Boolean expression evaluates to true then the block of
code inside the if statement will be executed. If not the first set of code
after the end of the if statement (after the closing curly brace) will be
executed.
if(Boolean expression)
{
//Some code should be work
}
public static void main(String[] y)
{
int my_int=25;
if(my_int==25)
{
System.out.println("the condition is true");
}
}
Go to branch statement slide
If else Statement
• An if statement can be followed by an optional else statement,
which executes when the Boolean expression is false.
if(Boolean expression)
{ //some code excut }
else
{ //some code excut }
public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{ System.out.println("the condition is true"); }
else
{ System.out.println("the condition is not true"); }
}
• Go to branch statement slide
If…else…if statement
• An if statement can be followed by an optional else if...else statement,
which is very useful to test various conditions using single if...else if
statement.
When using if , else if , else statements there are few points to keep in mind.
• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the else.
• Once an else if succeeds, none of the remaining else if's or else's will be
tested.
if(Boolean Expression 1)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else if (Boolean Expression 2)
{ //some code }
else
{ //some code }
public static void main(String[] y)
{
int my_int=25;
if(my_int<25)
{
System.out.println("the condition is true");
}
else if(my_int>25)
{System.out.println("the else if condition is true for 25 >
25");
}
else if(my_int==25)
{
System.out.println("the condition is true for 25==25");
}
else
{
System.out.println("any of the condition is not ture");
}
}
Go to branch statement slides
Nest if…else Statement
• It is always legal to nest if-else statements which means you can use one if
or else if statement inside another if or else if statement.
if(boolean expression 1)
{
if(boolean expression 2)
{ //some code }
else
{ //some code }
}
public static void main(String[] y)
{
int course_value=90;
String course_name="java";
String Course_type="core & advanced";
if(course_value==90)
{
if(course_name=="java")
{
if(Course_type=="core & advanced")
{
System.out.println("the nested condition is
true");
}
}
}
else
{
System.out.println("the nested conditions are not true");
}
}
Go to Branch statement slide
Switch Statement
• Switch case statements are a substitute for long if statements that
compare a variable to several "integral" values.. The value of the variable
given into switch is compared to the value following each of the cases, and
when one value matches the value of the variable, the computer
continues executing the program from that point.
switch(expression)
{
case value1: {//some code}break;
case value2: {//some code}break;
default: {//some code}break;
}
public static void main(String[] y)
{
int x=10;
/*
* x value is compared with case values
*/
switch(x)
{
case 10:{System.out.println("the x value is 10");}break;
case 30:{System.out.println("the x value is 30");}break;
case 40:{System.out.println("the x value is 40");}break;
}
}
Go to branch statement slide
LOOP control Statements
• loop is a sequence of instruction s that is continually repeated until a
certain condition is reached.
There are four types of loops:
• For loop
• For each loop
• While loop
• Do..While loop
<<goto first slide>>
For loop
• The for is an entry-entrolled loop and is used when an action is to
repeated for a predetermined number of times
for(initial value; test condition; increment)
{ //some code }
public static void main(String[] y)
{
for(int i=0; i<5; i++)
{System.out.println("hello world "+i);}
}
Go to loop slide
Foreach Statement
• A ForEach style loop is designed to cycle through a collection of
objects,such as an array, in strictly sequential fashion, from start to
finish.Implement a foreach loop by using the keyword foreach, java adds
the foreach compability by enhancing the for statement.
• for(type itr-var:collection)statement-block
int[] a={1,2,3,4};
for(int x:a)
{
System.out.println("array of a["+x+"] "+a[x]);
}
Go to loop slide
While loop(Entry Control loop)
• It repeates a statement or block while its controlling expression(any boolean
Expression) is true
• The body of the loop will be executed as long as the conditional expression is
true.
while(condition)
{
//body of code
}
public static void main(String[] y)
{
while(x<5)
{ /*x++ increment value of x;*/
System.out.println("the value of x is "+x);
x++;
}
}
Go to loop slide
Do-while loop
• A while loop is initially false then the body of the loop will not be executed at
all .The do-while loop always executes its body at least once because its
conditional expression is at the bottom of the loop
do
{ //body of code
}while(boolean expression);
System.out.println("the do While loop");
int x=1;
do
{
/*x++ increment value of x;
*/
System.out.println("the value of x is "+x);
x++;
}while(x<5);
Go to loop slided
Jump Statements
• Break
• Continue
• Return
<<goto first slide>>
Jump Statements
• Break
• In java the break statement has 3 uses
• Terminates a statement sequence in a switch statement
• It can be used to exit loop
• It can be used more civilized form of goto statement
• Using Break to exit the loop
By using break, you can force immediate termination of a loop .when break
statement is encountered inside a loop the loop is terminated and
program control resumes at the next statement following loop
public static void main(String[] y)
{
for(int i=0;i<10;i++)
{
if(i==5)
{
System.out.println("condition break");
break;
}
System.out.println("the value of i is::"+i);
}
}
• Using break in the form of GOTO
• Syntax Break label_name;
public static void main(String[] y)
{
first:{
secound:{
third:{
System.out.println("third block");
if(true)
{
break secound;
}
}
System.out.println("secound block");
}
System.out.println("first block");
}
}
go to jump statements
Continue
• A continue statement causes control to be transferred directly to the
conditional expression that controls the loop
public static void main(String[] y)
{
for(int i=0;i<5;i++)
{
if(true)
{
System.out.println("the if block");
continue;
}
System.out.println("the for loop");
}
}
Using continue with labels
• Specify the labels with continue statement
public static void main(String[] y)
{
outer:for(int i=0;i<=2;i++){
for(int j=0;j<=1;j++)
{
System.out.println("value of J is::"+j);
continue outer;
}
System.out.println("the value of i is::"+i);
}
/*
for continue labels it should be used with loops only not for blocks
*/
}
Go to jump statements
Return
• The return statement is used to explicitly return from a method.That is,it
causes program control to transfer back to the caller of the method.At any
time in a method the return statement can be used to cause execution to
branch back to the caller of the method.Thus,the return statement can be
used to cause execution to branch back to the caller ot the method.Thus, the
return statement immediately terminates the method in which it is executed.
public static void main(String[] y)
{
System.out.println("before return statement");
if(true)
{
return;
}
System.out.println("after return statement");
/*
here return causes execution to return to the java run-time system
*/
}
class Demo_break
{
int demo()
{
return(1);
}
public static void main(String[] y)
{
Demo_break d=new Demo_break();
System.out.println("the return value
is"+d.demo());
}
}
• goto jump statement
• Find the errors in the following
1. If(x+y=z&&y>0)
2. If(x>0);
A=b+c;
Else
A=a*b;
3. If(x<0)||(q<0)
Class ifelse
{ int result=55; char grade;
if(result>90) grade=‘A’;
else if(result>80)grade=“B”;
else if(result>70) grade=“c”;
else grade=“F”;
else grade=“g”;
System.out.println(“the grade is “+grade);
}
See the output
• Correct the code to print the output “correct
value”
public static void main(String[] args) {
int i=2;
if(i=2)
{
System.out.println("correct value");
}
else
{
System.out.println("not correct value");
}
Control flow statements in java

More Related Content

What's hot (20)

PPT
9. Input Output in java
Nilesh Dalvi
 
PDF
Files in java
Muthukumaran Subramanian
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PDF
Method Overloading In Java
CharthaGaglani
 
PPTX
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
Multithreading in java
Monika Mishra
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Operators in java
Then Murugeshwari
 
PPTX
6. static keyword
Indu Sharma Bhardwaj
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
Data types in java
HarshitaAshwani
 
PDF
Java threads
Prabhakaran V M
 
9. Input Output in java
Nilesh Dalvi
 
String and string buffer
kamal kotecha
 
Interface in java
PhD Research Scholar
 
Method Overloading In Java
CharthaGaglani
 
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Control Statements in Java
Niloy Saha
 
Control structures in java
VINOTH R
 
Inheritance in java
Tech_MX
 
Type casting in java
Farooq Baloch
 
Multithreading in java
Monika Mishra
 
Static keyword ppt
Vinod Kumar
 
Inner classes in java
PhD Research Scholar
 
Operators in java
Then Murugeshwari
 
6. static keyword
Indu Sharma Bhardwaj
 
Strings in Java
Abhilash Nair
 
Data types in java
HarshitaAshwani
 
Java threads
Prabhakaran V M
 

Viewers also liked (20)

PPTX
Switch statement, break statement, go to statement
Raj Parekh
 
PPT
Strings Arrays
phanleson
 
PPT
4.1 sequentioal search
Krish_ver2
 
PPTX
Java SE 8
Murali Pachiyappan
 
PPTX
Byte array to hex string transformer
Rahul Kumar
 
PPSX
Break and continue statement in C
Innovative
 
PDF
5 2. string processing
웅식 전
 
PDF
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
IDES Editor
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PPTX
Switch case and looping statement
_jenica
 
PPT
algorithm
kokilabe
 
PPTX
TypeScript by Howard
LearningTech
 
PDF
Multi string PV array
NIT MEGHALAYA
 
PDF
10. switch case
Way2itech
 
PDF
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
PPTX
java programming- control statements
jyoti_lakhani
 
PDF
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
PPTX
Nomenclature of Organic Compounds (IUPAC)
Lexter Supnet
 
PPTX
Epics and User Stories
Manish Agrawal, CSP®
 
Switch statement, break statement, go to statement
Raj Parekh
 
Strings Arrays
phanleson
 
4.1 sequentioal search
Krish_ver2
 
Byte array to hex string transformer
Rahul Kumar
 
Break and continue statement in C
Innovative
 
5 2. string processing
웅식 전
 
Design, Simulation and Verification of Generalized Photovoltaic cells Model U...
IDES Editor
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
Switch case and looping statement
_jenica
 
algorithm
kokilabe
 
TypeScript by Howard
LearningTech
 
Multi string PV array
NIT MEGHALAYA
 
10. switch case
Way2itech
 
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
java programming- control statements
jyoti_lakhani
 
Java 8 new features or the ones you might actually use
Sharon Rozinsky
 
Nomenclature of Organic Compounds (IUPAC)
Lexter Supnet
 
Epics and User Stories
Manish Agrawal, CSP®
 
Ad

Similar to Control flow statements in java (20)

PPT
Control statements
raksharao
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPTX
control statements
Azeem Sultan
 
PPT
Control statements
CutyChhaya
 
PPT
4.CONTROL STATEMENTS_MB.ppt .
happycocoman
 
PPT
05. Control Structures.ppt
AyushDut
 
PPTX
6.pptx
HarishNayak47
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PDF
Control flow statements in java web applications
RajithKarunarathne1
 
PPT
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
PDF
Control structures in Java
Ravi_Kant_Sahu
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
Control structures
Gehad Enayat
 
PPTX
Java chapter 3
Abdii Rashid
 
DOCX
Java loops
ricardovigan
 
PPTX
Pj01 5-exceution control flow
SasidharaRaoMarrapu
 
PPTX
Loop
prabhat kumar
 
PPT
Java control flow statements
Future Programming
 
Control statements
raksharao
 
Lecture - 5 Control Statement
manish kumar
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
control statements
Azeem Sultan
 
Control statements
CutyChhaya
 
4.CONTROL STATEMENTS_MB.ppt .
happycocoman
 
05. Control Structures.ppt
AyushDut
 
java notes.pdf
RajkumarHarishchandr1
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Control flow statements in java web applications
RajithKarunarathne1
 
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Control structures in Java
Ravi_Kant_Sahu
 
csj-161127083146power point presentation
deepayaganti1
 
Control structures
Gehad Enayat
 
Java chapter 3
Abdii Rashid
 
Java loops
ricardovigan
 
Pj01 5-exceution control flow
SasidharaRaoMarrapu
 
Java control flow statements
Future Programming
 
Ad

More from yugandhar vadlamudi (15)

ODP
Toolbarexample
yugandhar vadlamudi
 
ODP
Singleton pattern
yugandhar vadlamudi
 
PPT
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
DOCX
JButton in Java Swing example
yugandhar vadlamudi
 
PPTX
Collections framework in java
yugandhar vadlamudi
 
PPTX
Packaes & interfaces
yugandhar vadlamudi
 
PPTX
Exception handling in java
yugandhar vadlamudi
 
DOCX
JMenu Creation in Java Swing
yugandhar vadlamudi
 
DOCX
Adding a action listener to button
yugandhar vadlamudi
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPTX
Operators in java
yugandhar vadlamudi
 
PPTX
Inheritance
yugandhar vadlamudi
 
PPTX
Closer look at classes
yugandhar vadlamudi
 
PPTX
java Applet Introduction
yugandhar vadlamudi
 
PPTX
Class introduction in java
yugandhar vadlamudi
 
Toolbarexample
yugandhar vadlamudi
 
Singleton pattern
yugandhar vadlamudi
 
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
JButton in Java Swing example
yugandhar vadlamudi
 
Collections framework in java
yugandhar vadlamudi
 
Packaes & interfaces
yugandhar vadlamudi
 
Exception handling in java
yugandhar vadlamudi
 
JMenu Creation in Java Swing
yugandhar vadlamudi
 
Adding a action listener to button
yugandhar vadlamudi
 
Dynamic method dispatch
yugandhar vadlamudi
 
Operators in java
yugandhar vadlamudi
 
Inheritance
yugandhar vadlamudi
 
Closer look at classes
yugandhar vadlamudi
 
java Applet Introduction
yugandhar vadlamudi
 
Class introduction in java
yugandhar vadlamudi
 

Recently uploaded (20)

PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 

Control flow statements in java

  • 1. Control Flow Statements Branching Statements Looping Statements Jump Statemetns
  • 2. Branching Statements • If statement • If else Statement • If..else…if statement • Nest if…else statement • Switch Statement <<goTo first slide>>
  • 3. If Statement • An if statement consists of a Boolean expression followed by one or more statements. If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not the first set of code after the end of the if statement (after the closing curly brace) will be executed. if(Boolean expression) { //Some code should be work } public static void main(String[] y) { int my_int=25; if(my_int==25) { System.out.println("the condition is true"); } } Go to branch statement slide
  • 4. If else Statement • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. if(Boolean expression) { //some code excut } else { //some code excut } public static void main(String[] y) { int my_int=25; if(my_int<25) { System.out.println("the condition is true"); } else { System.out.println("the condition is not true"); } } • Go to branch statement slide
  • 5. If…else…if statement • An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. When using if , else if , else statements there are few points to keep in mind. • An if can have zero or one else's and it must come after any else if's. • An if can have zero to many else if's and they must come before the else. • Once an else if succeeds, none of the remaining else if's or else's will be tested. if(Boolean Expression 1) { //some code } else if (Boolean Expression 2) { //some code } else if (Boolean Expression 2) { //some code } else { //some code }
  • 6. public static void main(String[] y) { int my_int=25; if(my_int<25) { System.out.println("the condition is true"); } else if(my_int>25) {System.out.println("the else if condition is true for 25 > 25"); } else if(my_int==25) { System.out.println("the condition is true for 25==25"); } else { System.out.println("any of the condition is not ture"); } } Go to branch statement slides
  • 7. Nest if…else Statement • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. if(boolean expression 1) { if(boolean expression 2) { //some code } else { //some code } }
  • 8. public static void main(String[] y) { int course_value=90; String course_name="java"; String Course_type="core & advanced"; if(course_value==90) { if(course_name=="java") { if(Course_type=="core & advanced") { System.out.println("the nested condition is true"); } } } else { System.out.println("the nested conditions are not true"); } } Go to Branch statement slide
  • 9. Switch Statement • Switch case statements are a substitute for long if statements that compare a variable to several "integral" values.. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. switch(expression) { case value1: {//some code}break; case value2: {//some code}break; default: {//some code}break; }
  • 10. public static void main(String[] y) { int x=10; /* * x value is compared with case values */ switch(x) { case 10:{System.out.println("the x value is 10");}break; case 30:{System.out.println("the x value is 30");}break; case 40:{System.out.println("the x value is 40");}break; } } Go to branch statement slide
  • 11. LOOP control Statements • loop is a sequence of instruction s that is continually repeated until a certain condition is reached. There are four types of loops: • For loop • For each loop • While loop • Do..While loop <<goto first slide>>
  • 12. For loop • The for is an entry-entrolled loop and is used when an action is to repeated for a predetermined number of times for(initial value; test condition; increment) { //some code } public static void main(String[] y) { for(int i=0; i<5; i++) {System.out.println("hello world "+i);} } Go to loop slide
  • 13. Foreach Statement • A ForEach style loop is designed to cycle through a collection of objects,such as an array, in strictly sequential fashion, from start to finish.Implement a foreach loop by using the keyword foreach, java adds the foreach compability by enhancing the for statement. • for(type itr-var:collection)statement-block int[] a={1,2,3,4}; for(int x:a) { System.out.println("array of a["+x+"] "+a[x]); } Go to loop slide
  • 14. While loop(Entry Control loop) • It repeates a statement or block while its controlling expression(any boolean Expression) is true • The body of the loop will be executed as long as the conditional expression is true. while(condition) { //body of code } public static void main(String[] y) { while(x<5) { /*x++ increment value of x;*/ System.out.println("the value of x is "+x); x++; } } Go to loop slide
  • 15. Do-while loop • A while loop is initially false then the body of the loop will not be executed at all .The do-while loop always executes its body at least once because its conditional expression is at the bottom of the loop do { //body of code }while(boolean expression); System.out.println("the do While loop"); int x=1; do { /*x++ increment value of x; */ System.out.println("the value of x is "+x); x++; }while(x<5); Go to loop slided
  • 16. Jump Statements • Break • Continue • Return <<goto first slide>>
  • 17. Jump Statements • Break • In java the break statement has 3 uses • Terminates a statement sequence in a switch statement • It can be used to exit loop • It can be used more civilized form of goto statement • Using Break to exit the loop By using break, you can force immediate termination of a loop .when break statement is encountered inside a loop the loop is terminated and program control resumes at the next statement following loop
  • 18. public static void main(String[] y) { for(int i=0;i<10;i++) { if(i==5) { System.out.println("condition break"); break; } System.out.println("the value of i is::"+i); } }
  • 19. • Using break in the form of GOTO • Syntax Break label_name; public static void main(String[] y) { first:{ secound:{ third:{ System.out.println("third block"); if(true) { break secound; } } System.out.println("secound block"); } System.out.println("first block"); } } go to jump statements
  • 20. Continue • A continue statement causes control to be transferred directly to the conditional expression that controls the loop public static void main(String[] y) { for(int i=0;i<5;i++) { if(true) { System.out.println("the if block"); continue; } System.out.println("the for loop"); } }
  • 21. Using continue with labels • Specify the labels with continue statement public static void main(String[] y) { outer:for(int i=0;i<=2;i++){ for(int j=0;j<=1;j++) { System.out.println("value of J is::"+j); continue outer; } System.out.println("the value of i is::"+i); } /* for continue labels it should be used with loops only not for blocks */ } Go to jump statements
  • 22. Return • The return statement is used to explicitly return from a method.That is,it causes program control to transfer back to the caller of the method.At any time in a method the return statement can be used to cause execution to branch back to the caller of the method.Thus,the return statement can be used to cause execution to branch back to the caller ot the method.Thus, the return statement immediately terminates the method in which it is executed. public static void main(String[] y) { System.out.println("before return statement"); if(true) { return; } System.out.println("after return statement"); /* here return causes execution to return to the java run-time system */ }
  • 23. class Demo_break { int demo() { return(1); } public static void main(String[] y) { Demo_break d=new Demo_break(); System.out.println("the return value is"+d.demo()); } } • goto jump statement
  • 24. • Find the errors in the following 1. If(x+y=z&&y>0) 2. If(x>0); A=b+c; Else A=a*b; 3. If(x<0)||(q<0)
  • 25. Class ifelse { int result=55; char grade; if(result>90) grade=‘A’; else if(result>80)grade=“B”; else if(result>70) grade=“c”; else grade=“F”; else grade=“g”; System.out.println(“the grade is “+grade); } See the output
  • 26. • Correct the code to print the output “correct value” public static void main(String[] args) { int i=2; if(i=2) { System.out.println("correct value"); } else { System.out.println("not correct value"); }