170 likes | 190 Views
Learn about counter-controlled repetition, infinite loops prevention, do...while loops, switch statements for multiple selections, and logical operators in Java programming. Practice Lab activities for hands-on learning in advanced business applications programming.
E N D
Week 4 Control Structure, Part 2 MSIS 655Advanced Business Applications Programming 4.1
Counter-controlled repetition • Control variable (loop counter) • Initial value of the control variable • Increment/decrement of control variable through each loop • Loop-continuation condition that tests for the final value of the control variable
The for repetition structure int counter = 1 counter <=10 System.out.printf(“%d “, counter); true counter++ false
Infinite loops • Infinite loops occur when the loop-continuation condition in a repetition statement never becomes false. To prevent this situation in a counter-controlled loop, ensure that the control variable is incremented (or decremented) during each iteration of the loop. In a sentinel-controlled loop, ensure that the sentinel value is eventually input.
do…while Repetition Statement • do…whilestructure • Similar to while structure • Tests loop-continuation after performing body of loop • i.e., loop body always executes at least once
The do…whilerepetition structure int counter = 1 do System.out.printf(“%d “, counter); counter++ counter <=10 while true false
switch Multiple-Selection Statement • switch statement • Used for multiple selections • Provide a default case in switch statements. Including a default case focuses you on the need to process exceptional conditions.
break and continue Statements • break/continue • Alter flow of control • break statement • Causes immediate exit from control structure • Used in while, for, do…while or switch statements • continue statement • Skips remaining statements in loop body • Proceeds to next iteration • Used in while, for or do…while statements
The switch Multiple-selection structure true Case a case a action break false true Case b case b action break false true Case z case z action break false default
Logical Operators • Logical operators • Allows for forming more complex conditions • Combines simple conditions • Java logical operators • && (conditional AND) • || (conditional OR) • & (boolean logical AND) • | (boolean logical inclusive OR) • ^ (boolean logical exclusive OR) • ! (logical NOT)
Logical Operators (Cont.) • Short-Circuit Evaluation of Complex Conditions • Parts of an expression containing && or || operators are evaluated only until it is known whether the condition is true or false • E.g., ( gender == FEMALE ) && ( age >= 65 ) • Stops immediately if gender is not equal to FEMALE
Lab activities (Week 7) • Exercises (pp. 227-228)5.11, 5.12, 5.15, 5.20, 5.21