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

Control flow statements in java web applications

  • 1.
  • 2.
    Java Control Statements JavaControl 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 IfStatement  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-elseStatement  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 CaseStatement  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 LoopStatement  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); } } }