Unformatted text preview:

Slide 1Outline4.2 Algorithms4.3 PseudocodeSlide 5Slide 64.4 Control StructuresSlide 8Slide 9Activity diagrams and structured controlsActivity diagrams and structured controlsSlide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 214.7 while Repetition StatementSlide 23Slide 244.8 Formulating Algorithms: Counter-Controlled RepetitionSlide 26Slide 274.9 Formulating Algorithms: Sentinel-Controlled RepetitionSlide 29Slide 30Slide 31Type Casting4.11 Compound Assignment Operators4.12 Increment and Decrement OperatorsSlide 35Slide 36Slide 37Slide 38Slide 39Control Structures- Selections- Repetitions/iterations1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. YangOutline•Algorithms and control structures–Selections–Iterations234.2 Algorithms•An algorithm is a procedure for solving a problem in terms of–the actions to execute and–the order in which these actions execute•An ordered sequence of operations for completing a task•Specifying the order in which statements (actions) execute in a program is called program control. •A program can be controlled by using the control structures provided by the programming languages.-Common control structures include selections and iterations.44.3 Pseudocode•Pseudocode is an informal language that helps you develop algorithms without having to worry about the strict details of Java language syntax. •Particularly useful for developing algorithms that will be converted to structured portions of Java programs. •Helps you “think out” a program before attempting to write it in a programming language, such as Java. •Carefully prepared pseudocode can easily be converted to a corresponding Java program. •Try to represent the ‘major’ operations–e.g., input, output or calculations.5•Example Pseudocode-Goal: Write down the pseudocode of an algorithm that takes a natural number, N, and prints the sum of 1+2+…+N.-Questions: Would both algorithm accomplish the given task? Is one better than the other?Algorithhm-1 Algorithhm-21. Get N2. Let Sum = 03. i = 14. While i <= Na) Sum = Sum + ib) i = i + 15. Print Sum1. Get N2. If N > 0 then Sum = NElse Sum = 03. Let i = N - 14. While i > 0a) Sum = Sum + ib) i = i - 15. Print Sum6•Exercises:1. Trace the algorithms by inputting 5 as the value of N. Verify that both algorithms calculate the correct Sum.2. Trace the algorithms by inputting -3 as the value of N. Verify that both algorithms calculate the correct Sum.3. Revise the algorithms so they will be able to properly handle negative integers.4. Draw flowcharts to represent the revised algorithms.5. Convert the algorithms into Java applications.74.4 Control Structures•Also referred to as “control statements” (in the terminology of the Java Language Specification )•Edsger Dijkstra (1968). "Go To Statement Considered Harmful". Communications of the ACM 11(3)•Bohm and Jacopini demonstrated that programs could be written without any goto statements. •Structured Programming: All programs can be written in terms of only three control structures — the sequence structure, the selection structure and the repetition structure. -Sequence: Statements in a program execute one after the other in the order in which they are written. -Selection: Statements in a program execute depending on whether a given condition is true or false. -Repetition (or iteration): Statements in a program continue to execute until a given condition is true or false.8•Sequence structure –The activity diagram in Fig. 4.1 illustrates a typical sequence structure in which two calculations are performed in order. –A black circle represents the start (initial state) of the workflow.–An encircled black circle represents the end (final state).9UML Activity Diagram •Models the workflow (also called the activity) of a portion of a software system. •Composed of symbols similar to those in flowchartingQ: What are the main differences?•Clearly show how control structures operate. Source: http://www.ibm.com/developerworks/rational/library/2802.html10Activity diagrams and structured controls11Activity diagrams and structured controls12•Three types of selection statements.•if statement: –Performs an action, if a condition is true; skips it, if false. –Single-selection statement—selects or ignores a single action (or group of actions). •if…else statement: –Performs an action if a condition is true and performs a different action if the condition is false. –Double-selection statement—selects between two different actions (or groups of actions). •switch statement–Performs one of several actions, based on the value of an expression.–Multiple-selection statement—selects among many different actions (or groups of actions).selection statements13•Three repetition statements (also called looping statements) –Perform statements repeatedly while a loop-continuation condition remains true. •while and for statements perform the action(s) in their bodies zero or more times–if the loop-continuation condition is initially false, the body will not execute. •The do…while statement performs the action(s) in its body one or more times. •if, else, switch, while, do and for are keywords. –Appendix C: Complete list of Java keywords.repetition (also called looping )14•Every program is formed by combining the sequence statement, selection statements (three types) and repetition statements (three types) as appropriate for the algorithm the program implements. •Can model each control statement as an activity diagram.1516•Ternary operator (takes three operands)•Operands and ?: form a conditional expression•Operand to the left of the ? (x) is a boolean expression — evaluates to a boolean value (true or false)•Second operand between the ? and : (y) is the value if the boolean expression is true•Third operand to the right of the : (z) is the value if the boolean expression evaluates to false. Example:System.out.println( studentGrade >= 60 ? "Passed" : "Failed" );Evaluates to the string "Passed" if the boolean expression studentGrade >= 60 is true and to the string "Failed" if it is false. Conditional operator (x ? y : z)— shorthand of if…else1718•Pseudocode:If student’s grade is greater than or equal to 90 Print “A”else If student’s grade is greater than or equal to 80 Print “B” else If student’s grade is greater than or equal to 70


View Full Document

UHCL CSCI 3134 - Control Structures

Download Control Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?