SlideShare a Scribd company logo
Control flow statements
Rajith Karunarathne
Java Control Statements
Java Control statements control the order of execution in a java program, based
on data values and conditional logic.
There are three main categories of control flow statements;
 Selection statements: if, if-else and switch
 Loop statements: while, do-while and for
 Transfer statements: break, continue, return, try-catch-finally
We use control statements when we want to change the default sequential order of
execution
Selection statements
The If Statement
 The if statement executes a block of code only if the specified expression is true.
 If the value is false, then the if block is skipped and execution continues with the rest of the
program.
 You can either have a single statement or a block of code within an if statement.
 Note that the conditional expression must be a Boolean expression
The simple if statement syntax:
if (<conditional expression>)
<statement action>
Example
public class IfStatement {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a is greater than b");
if (a < b)
System.out.println("b is greater than a");
}
}
Selection statements…
The If-else Statement
 If the statements in the if statement fails, the statements in the else block are executed.
 You can either have a single statement or a block of code within if-else blocks
The if-else statement syntax:
if (<conditional expression>)
<statement action>
else
<statement action>
Example
public class IfElseStatement {
public static void main(String[] args) {
int a = 10, b = 20;
if (a >b) {
System.out.println("a is greater than b");
} else {
System.out.println("b is greater than a");
}
}
}
Selection statements…
Switch Case Statement
 It is a multi-way branch with several choices
 It compares the value of the controlling expression to the values of each case label
 It includes a default label to use in cases where there are no matches
The Switch-Case statement syntax:
switch (<expression>) {
case label1: <statement1>; break;
…
case labeln: <statementn>; break;
default: <statement>;
}
Example
public class SwitchCaseStatement {
public static void main(String[] args) {
int status = -1;
switch (status) {
case 1: System.out.println(“You have entered number 1");
break;
case 2: System.out.println("You have entered number 2");
break;
case 3: System.out.println("You have entered number 3");
break;
default: System.out.println("You have entered something else");
}
}
}
Iteration statements
For Loops
 The for loop is a looping construct which can execute a set of instructions a specified number
of times.
 It’s a counter controlled loop.
The syntax of the loop is as follows:
for (<initialization>; <loop condition>; <increment expression>)
<loop body>
Example
public class ForLoop {
public static void main(String[] args) {
System.out.println("Printing Numbers from 1 to 10");
for (int count = 1; count <= 10; count++) {
System.out.println(count);
}
}
}
Iteration statements…
While Statement
 This is a looping construct control statement that executes a block of code while a condition
is true.
 You can either have a single statement or a block of code within the while loop.
 The loop will never be executed if the testing expression evaluates to false.
 The loop condition must be a boolean expression.
The while loop syntax:
while (<loop condition>)
<statements>
Example
public class WhileLoop {
public static void main(String[] args) {
int count = 1;
System.out.println("Printing Numbers from 1 to 10");
while (count <= 10) {
System.out.println(count++);
}
}
}
Iteration statements…
Do-while Loop Statement
 The do-while loop is similar to the while loop, except that the test is performed at the end of
the loop instead of at the beginning.
 This ensures that the loop will be executed at least once.
The do-while loop syntax:
do
<loop body>
while (<loop condition>);
Example
public class DoWhileLoop {
public static void main(String[] args) {
int count = 1;
System.out.println("Printing Numbers from 1 to 10");
do {
System.out.println(count++);
} while (count <= 10);
}
}
Transfer statements
Continue Statement
 A continue statement stops the iteration of a loop (while, do or for) and causes execution to
resume at the top of the nearest enclosing loop.
 You use a continue statement when you do not want to execute the remaining statements in the
loop.
 You can use the label in your continue statement (optional).
 It is usually only used when returning to the outermost loop in a series of nested loops.
The continue statement syntax:
continue; // the unlabeled form
continue <label>; // the labeled form
Example
public class ContinueExample {
public static void main(String[] args) {
System.out.println("Odd Numbers");
for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0)
continue; // Rest of loop body skipped when i is even
System.out.println(i);
}
}
}
Transfer statements…
Break Statement
 The break statement transfers control out of the enclosing loop ( for, while, do or switch
statement).
 Use a break statement when you want to jump immediately to the statement following the
enclosing control structure.
 You can also provide a loop with a label, and then use the label in your break statement.
The break statement syntax:
break; // the unlabeled form
break <label>; // the labeled form
Example
public class BreakExample {
public static void main(String[] args) {
System.out.println("Numbers 1 - 10");
for (int i = 1; ; ++i) {
if (i == 11)
break; // Rest of loop body skipped when i is eleven
System.out.println(i);
}
}
}

More Related Content

PPT
Control statements
CutyChhaya
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPT
C++ chapter 4
SHRIRANG PINJARKAR
 
PPTX
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
PPTX
07 flow control
dhrubo kayal
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
Control statements
CutyChhaya
 
Lecture - 5 Control Statement
manish kumar
 
C++ chapter 4
SHRIRANG PINJARKAR
 
Unit-02 Selection, Mathematical Functions and loops.pptx
jessicafalcao1
 
07 flow control
dhrubo kayal
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Control statements in java
Madishetty Prathibha
 
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 

Similar to Control flow statements in java web applications (20)

PPTX
control statements
Azeem Sultan
 
PPTX
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PPTX
Flow of control C ++ By TANUJ
TANUJ ⠀
 
PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Chapter 5 java
Ahmad sohail Kakar
 
PPTX
C Programming Control Structures(if,if-else)
poonambhagat36
 
PPT
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
PPTX
C-Programming Control statements.pptx
SKUP1
 
PPTX
C-Programming Control statements.pptx
LECO9
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
PPTX
Btech i pic u-3 handling input output and control statements
Rai University
 
PPTX
Mca i pic u-3 handling input output and control statements
Rai University
 
DOCX
Java loops
ricardovigan
 
PPTX
handling input output and control statements
Rai University
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
control statements
Azeem Sultan
 
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
control-statements, control-statements, control statement
crrpavankumar
 
Flow of control C ++ By TANUJ
TANUJ ⠀
 
CONTROL STMTS.pptx
JavvajiVenkat
 
Control structures in java
VINOTH R
 
Chapter 5 java
Ahmad sohail Kakar
 
C Programming Control Structures(if,if-else)
poonambhagat36
 
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
LECO9
 
java notes.pdf
RajkumarHarishchandr1
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Btech i pic u-3 handling input output and control statements
Rai University
 
Mca i pic u-3 handling input output and control statements
Rai University
 
Java loops
ricardovigan
 
handling input output and control statements
Rai University
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
Loops and conditional statements
Saad Sheikh
 
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
Ad

Recently uploaded (20)

PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Landforms and landscapes data surprise preview
jpinnuck
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Ad

Control flow statements in java web applications

  • 2. Java Control Statements Java Control statements control the order of execution in a java program, based on data values and conditional logic. There are three main categories of control flow statements;  Selection statements: if, if-else and switch  Loop statements: while, do-while and for  Transfer statements: break, continue, return, try-catch-finally We use control statements when we want to change the default sequential order of execution
  • 3. Selection statements The If Statement  The if statement executes a block of code only if the specified expression is true.  If the value is false, then the if block is skipped and execution continues with the rest of the program.  You can either have a single statement or a block of code within an if statement.  Note that the conditional expression must be a Boolean expression The simple if statement syntax: if (<conditional expression>) <statement action>
  • 4. Example public class IfStatement { public static void main(String[] args) { int a = 10, b = 20; if (a > b) System.out.println("a is greater than b"); if (a < b) System.out.println("b is greater than a"); } }
  • 5. Selection statements… The If-else Statement  If the statements in the if statement fails, the statements in the else block are executed.  You can either have a single statement or a block of code within if-else blocks The if-else statement syntax: if (<conditional expression>) <statement action> else <statement action>
  • 6. Example public class IfElseStatement { public static void main(String[] args) { int a = 10, b = 20; if (a >b) { System.out.println("a is greater than b"); } else { System.out.println("b is greater than a"); } } }
  • 7. Selection statements… Switch Case Statement  It is a multi-way branch with several choices  It compares the value of the controlling expression to the values of each case label  It includes a default label to use in cases where there are no matches The Switch-Case statement syntax: switch (<expression>) { case label1: <statement1>; break; … case labeln: <statementn>; break; default: <statement>; }
  • 8. Example public class SwitchCaseStatement { public static void main(String[] args) { int status = -1; switch (status) { case 1: System.out.println(“You have entered number 1"); break; case 2: System.out.println("You have entered number 2"); break; case 3: System.out.println("You have entered number 3"); break; default: System.out.println("You have entered something else"); } } }
  • 9. Iteration statements For Loops  The for loop is a looping construct which can execute a set of instructions a specified number of times.  It’s a counter controlled loop. The syntax of the loop is as follows: for (<initialization>; <loop condition>; <increment expression>) <loop body>
  • 10. Example public class ForLoop { public static void main(String[] args) { System.out.println("Printing Numbers from 1 to 10"); for (int count = 1; count <= 10; count++) { System.out.println(count); } } }
  • 11. Iteration statements… While Statement  This is a looping construct control statement that executes a block of code while a condition is true.  You can either have a single statement or a block of code within the while loop.  The loop will never be executed if the testing expression evaluates to false.  The loop condition must be a boolean expression. The while loop syntax: while (<loop condition>) <statements>
  • 12. Example public class WhileLoop { public static void main(String[] args) { int count = 1; System.out.println("Printing Numbers from 1 to 10"); while (count <= 10) { System.out.println(count++); } } }
  • 13. Iteration statements… Do-while Loop Statement  The do-while loop is similar to the while loop, except that the test is performed at the end of the loop instead of at the beginning.  This ensures that the loop will be executed at least once. The do-while loop syntax: do <loop body> while (<loop condition>);
  • 14. Example public class DoWhileLoop { public static void main(String[] args) { int count = 1; System.out.println("Printing Numbers from 1 to 10"); do { System.out.println(count++); } while (count <= 10); } }
  • 15. Transfer statements Continue Statement  A continue statement stops the iteration of a loop (while, do or for) and causes execution to resume at the top of the nearest enclosing loop.  You use a continue statement when you do not want to execute the remaining statements in the loop.  You can use the label in your continue statement (optional).  It is usually only used when returning to the outermost loop in a series of nested loops. The continue statement syntax: continue; // the unlabeled form continue <label>; // the labeled form
  • 16. Example public class ContinueExample { public static void main(String[] args) { System.out.println("Odd Numbers"); for (int i = 1; i <= 10; ++i) { if (i % 2 == 0) continue; // Rest of loop body skipped when i is even System.out.println(i); } } }
  • 17. Transfer statements… Break Statement  The break statement transfers control out of the enclosing loop ( for, while, do or switch statement).  Use a break statement when you want to jump immediately to the statement following the enclosing control structure.  You can also provide a loop with a label, and then use the label in your break statement. The break statement syntax: break; // the unlabeled form break <label>; // the labeled form
  • 18. Example public class BreakExample { public static void main(String[] args) { System.out.println("Numbers 1 - 10"); for (int i = 1; ; ++i) { if (i == 11) break; // Rest of loop body skipped when i is eleven System.out.println(i); } } }