Additional control structuresThe if-else statementFlowchart for the if-else statementThe while loopFlowchart for the while loopThe do-while loopFlowchart for the do-while loopThe increment operatorExamples of ++The decrement operatorExamples of --The for loopFlowchart for the for loopParts of the for loopExample for loopsExample: Multiplication tableResultsWhen do you use each loop?The break statementMultiway decisionsSyntax of the switch statementFlowchart for switch statementSlide 23Example switch statementSpaces and indentationThe EndJan 14, 2019Additional control structuresThe if-else statementThe if-else statement chooses which of two statements to executeThe if-else statement has the form: if (condition) statement-to-execute-if-true ;else statement-to-execute-if-false ;Either statement (or both) may be a compound statementNotice the semicolon after each statementThe else part is optionalFlowchart for the if-else statementcondition?truestatement-1 statement-2falseThe while loopThis is the form of the while loop:while (condition) statement ;If the condition is true, the statement is executed, then the whole thing is done againThe statement is executed repeatedly until the condition becomes falseIf the condition starts out false, the statement is never executed at allFlowchart for the while loopcondition?statementtruefalseThe do-while loopThe syntax for the do-while is: do { …any number of statements…} while (condition) ;The while loop performs the test first, before executing the statementThe do-while statement performs the test afterwardsAs long as the test is true, the statements in the loop are executed againFlowchart for the do-while loopcondition?statementtruefalseThe increment operator++ adds 1 to a variableIt can be used as a statement by itself, or within an expressionIt can be put before or after a variableIf before a variable (preincrement), it means to add one to the variable, then use the resultIf put after a variable (postincrement), it means to use the current value of the variable, then add one to the variableExamples of ++ int a = 5;a++;// a is now 6 int b = 5;++b;// b is now 6 int c = 5;int d = ++c;// c is 6, d is 6 int e = 5;int f = e++;// e is 6, f is 5 int x = 10;int y = 100;int z = ++x + y++;// x is 11, y is 101, z is 111Confusing code is bad code, so this is poor styleThe decrement operator-- subtracts 1 from a variableIt can be used as a statement by itself, or within an expressionIt can be put before or after a variableIf before a variable (predecrement), it means to subtract one from the variable, then use the resultIf put after a variable (postdecrement), it means to use the current value of the variable, then subtract one from the variableExamples of -- int a = 5;a--;// a is now 4 int b = 5;--b;// b is now 4 int c = 5;int d = --c;// c is 4, d is 4 int e = 5;int f = e--;// e is 4, f is 5 int x = 10;int y = 100;int z = --x + y--;// x is 9, y is 99, z is 109Confusing code is bad code, so this is poor styleThe for loopThe for loop is complicated, but very handySyntax: for (initialize ; test ; increment) statement ;Notice that there is no semicolon after the incrementExecution:The initialize part is done first and only onceThe test is performed; as long as it is true,The statement is executedThe increment is executedFlowchart for the for loopcondition?statementstruefalseincrementinitializeParts of the for loopInitialize: In this part you define the loop variable with an assignment statement, or with a declaration and initializationExamples: i = 0 int i = 0 i = 0, j = k + 1Test, or condition: A boolean conditionJust like in the other control statements we have usedIncrement: An assignment to the loop variable, or an application of ++ or -- to the loop variableExample for loopsPrint the numbers 1 through 10, and their squares: for (int i = 1; i < 11; i++) { System.out.println(i + " " + (i * i));}Print the squares of the first 100 integers, ten per line: for (int i = 1; i < 101; i++) { System.out.print(" " + (i * i)); if (i % 10 == 0) System.out.println();}Example: Multiplication table public static void main(String args[]) { for (int i = 1; i < 11; i++) { for (int j = 1; j < 11; j++) { int product = i * j; if (product < 10) System.out.print(" " + product); else System.out.print(" " + product); } System.out.println(); } }Results 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100When do you use each loop?Use the for loop if you know ahead of time how many times you want to go through the loopExample: Stepping through an arrayExample: Print a 12-month calendarUse the while loop in almost all other casesExample: Compute the next step in an approximation until you get close enoughUse the do-while loop if you must go through the loop at least once before it makes sense to do the testExample: Ask for the password until user gets it rightThe break statementInside any loop, the break statement will immediately get you out of the loopIf you are in nested loops, break gets you out of the innermost loopIt doesn’t make any sense to break out of a loop unconditionally—you should do it only as the result of an if testExample:for (int i = 1; i <= 12; i++) { if (badEgg(i)) break;} break is not the normal way to leave a loopUse it when necessary, but don’t overuse itMultiway decisionsThe if-else statement chooses one of two statements, based on the value of a boolean expressionThe switch statement chooses one of several statements, based on the value on an integer (int, byte, short, or long) or a char expressionSyntax of the switch statementThe syntax is: switch (expression) { case value1 : statements ; break ; case value2 : statements ; break ; ...(more cases)... default : statements ; break ;}The expression must yield an integer or a characterEach value must be a literal integer or characterNotice that colons ( : ) are used as well as semicolonsThe last
View Full Document