1 / 17

MSIS 6 55 Advanced Business Applications Programming

Week 4 Control Structure, Part 2. MSIS 6 55 Advanced 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

dmatta
Télécharger la présentation

MSIS 6 55 Advanced Business Applications Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 4 Control Structure, Part 2 MSIS 655Advanced Business Applications Programming 4.1

  2. 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

  3. The for repetition structure int counter = 1 counter <=10 System.out.printf(“%d “, counter); true counter++ false

  4. 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.

  5. 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

  6. The do…whilerepetition structure int counter = 1 do System.out.printf(“%d “, counter); counter++ counter <=10 while true false

  7. 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.

  8. 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

  9. 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

  10. 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)

  11. 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

  12. Lab activities (Week 7) • Exercises (pp. 227-228)5.11, 5.12, 5.15, 5.20, 5.21

More Related