MSIS 655 Advanced Business Applications Programming

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
COMP Loop Statements Yi Hong May 21, 2015.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2005 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Computer Programming
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Control Statements: Part 2
Chapter 4 C Program Control Part II
Lecture 7: Repeating a Known Number of Times
Control Statements: Part 2
JavaScript: Control Statements I
Chapter 5: Control Structures II
Control Structures: Part 2
Control Structures II (Repetition)
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements.
JavaScript: Control Statements I
JavaScript: Control Statements II
Chapter 5- part 2 Control Statements: Loops 2
Looping and Repetition
Programming Fundamentals Lecture #6 Program Control
Control Statements: Part 2
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
Chapter 8 JavaScript: Control Statements, Part 2
Advanced Programming Chapters 5 & 6: Control Structures
Structured Program Development in C
Introduction to Object-Oriented Programming with Java--Wu
Chapter 4 - Program Control
4 C Program Control.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
3 Control Statements:.
Chapter 5 Control Statements: Loops 2
Chapter 6 Control Statements: Part 2
Control Statements: Part 2
Chapter 5: Control Structures II (Repetition)
Chapter 4 - Program Control
Chap 7. Advanced Control Statements in Java
Chapter 8 JavaScript: Control Statements, Part 2
Looping and Repetition
Control Statements:.
Presentation transcript:

MSIS 655 Advanced Business Applications Programming Week 4 Control Structure, Part 2 In this chapter you will learn: The essentials of counter-controlled repetition. To use the for and do while repetition statements to execute statements in a program repeatedly. To understand multiple selection using the switch selection statement. To use the break and continue program control statements to alter the flow of control. To use the logical operators to form complex conditional expressions in control statements. 11/16/2018 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 Control counting loops with integers. Because floating-point values may be approximate, controlling loops with floating-point variables may result in imprecise counter values and inaccurate termination tests. 11/16/2018

The for repetition structure int counter = 1 counter <=10 System.out.printf(“%d “, counter); true counter++ Using the final value in the condition of a while or for statement and using the <= relational operator helps avoid off-by-one errors. For a loop that prints the values 1 to 10, the loop-continuation condition should be counter <= 10 rather than counter < 10 (which causes an off-by-one error) or counter < 11 (which is correct). Many programmers prefer so-called zero-based counting, in which to count 10 times, counter would be initialized to zero and the loop-continuation test would be counter < 10. false 11/16/2018

In the most cases, preincrementing and postincrementing are both used to add 1 to a variable in a statement by itself. In these cases, the effect is exactly the same, except that preincrementing has a slight performance advantage. Given that the compiler typically optimizes your code to help you get the best performance, use the idiom with which you feel most comfortable in these situations. Placing a semicolon immediately to the right of the right parenthesis of a for header makes that for’s body an empty statement. This is normally a logic error. 11/16/2018

Fig. 5.4 | UML activity diagram for the for statement in Fig. 5.2. 11/16/2018 Fig. 5.4 | UML activity diagram for the for statement in Fig. 5.2.

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. 11/16/2018

do…while Repetition Statement do…while structure Similar to while structure Tests loop-continuation after performing body of loop i.e., loop body always executes at least once 11/16/2018

Fig. 5.8 | do...while repetition statement UML activity diagram. The do…while repetition structure int counter = 1 do System.out.printf(“%d “, counter); counter++ counter <=10 while true false 11/16/2018 Fig. 5.8 | do...while repetition statement UML activity diagram.

Always include braces in a do Always include braces in a do...while statement, even if they are not necessary. This helps eliminate ambiguity between the while statement and a do...while statement containing only one statement. 11/16/2018

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. Although each case and the default case in a switch can occur in any order, place the default case last. When the default case is listed last, the break for that case is not required. Some programmers include this break for clarity and symmetry with other cases. 11/16/2018

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 Forgetting a break statement when one is needed in a switch is a logic error. 11/16/2018

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 11/16/2018 Fig. 5.11 | switch multiple-selection statement UML activity diagram with break statements.

11/16/2018

11/16/2018

Logical Operators Logical operators Java 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/16/2018

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 In expressions using operator &&, a condition-we will call this the dependent condition-may require another condition to be true for the evaluation of the dependent condition to be meaningful. In this case, the dependent condition should be placed after the other condition, or an error might occur. For example, in the expression ( i != 0 ) && ( 10 / i == 2 ), the second condition must appear after the first condition, or a divide-by-zero error might occur. 11/16/2018

Lab activities (Week 7) Exercises (pp. 227-228) 5.11, 5.12, 5.15, 5.20, 5.21 11/16/2018