Control Structures in Java - allow us to change the ordering of how the statements in our programs are executed ● Decision

Control Structures - which allow selection of specific sections of code to be executed - if - if-else - if - else if - switch ● Repetition Control Structures – which allow executing specific sections of code a number of times - while - do-while - for ● Branching Statements – which allow redirection of program flow - break - continue - return Syntax and Examples • if-statement has the form: if ( boolean_expression ) statement; or if ( boolean_expression ) {statement 1;statement 2;} -where: boolean _expression is either a boolean expression or boolean variable Ex: int grade = 68;if ( grade > 60 ) System.out.println ("Congratulations! ") ; • if-else statement has the form: if ( boolean_expression ) {statement 1;statement 2;. . .} else {statement 3;statement 4;. . .} Ex: int grade = 68; if ( grade > 60 ) System.out.println ("Congratulations! "); Else System.out.println ("Sorry you failed") ; • if-else-if statement has the form: if ( boolean_expression1 )statement 1; else if ( boolean_expression2 )statement 2; else statement 3; Ex: int grade = 68;if ( grade > 90 ) {System. out. println ("Very good! ") ;} else if ( grade > 60 ) {System. out. println ("Very good! ") ;} else {System. out. println ("Sorry you failed") ;}

• for loop has the form: for(InitializationExpression.LoopCondition. case 80: System. out.statement 2.} . do {System.are unique integer or character constants.println("Good job!" ).//block n} -where: switch _expression . : default: statement1.} • do-while loop has the form: do {statement 1. out. Ex: public class Grade { publicstatic void main( String[] args ) { int grade = 92.} while ( boolean_expression ) .. i < 10. you failed. default: System.out. . } }} • while loop has the form: while ( boolean_expression ) {statement 1. break. i++ ) {System.5 switch(grade) { case 100: System.• switch statement has the form: switch( switch_expression ) {case case_selector1: statement1..println("Study harder!"). .println( "Excellent!" ).out.//block2 break. case case_selector2: statement1.//block 1 break. . Ex: int x = 0.x++.while (x<10) {System. case_selector2 and so on .statement2.// statement2. .compares the loop variable to some limit value StepExpression . println (x) .out.is an integer or character expression case_selector1. case 90: System.statement 2. .StepExpression) {statement1.} -where: InitializationExpression -initializes the loop variable LoopCondition . break. println (x) .break.out. Ex: int i.} Ex: Int x = 0.//statement2.} while (x<10) . println (i) .x++. for ( i = 0.").. . out.println("Sorry.updates the loop variable.// statement2.

Sign up to vote on this title
UsefulNot useful