1 / 49

A8 – Control Structures

A8 – Control Structures. if, if-else, switch. Control of flow in Java. Any sort of complex program must have some ability to control flow. Control of Flow. Without this control, programs become limited to one basic job each time the program is run. Getting started.

haruki
Download Presentation

A8 – Control Structures

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. A8 – Control Structures if, if-else, switch

  2. Control of flow in Java • Any sort of complex program must have some ability to control flow.

  3. Control of Flow • Without this control, programs become limited to one basic job each time the program is run.

  4. Getting started • The most basic of these control structures is the ifstatement,

  5. The other most basic • . The other most basic of these control structures is the if-else,

  6. The other other most basic • Huh? • and then the switch statement.

  7. Structured Programming……………..in the old days • In the early days of programming (1960's), the approach to writing software was relatively primitive and ineffective. • Much of the code was written with goto statements that transferred program control to another line in the code.

  8. Spaghetti code • Tracing this type of code was an exercise in jumping from one spot to another, leaving behind a trail of lines similar to spaghetti.

  9. Spaghetti code • The term "spaghetti code" comes from trying to trace code linked together with goto statements. • The complexity this added to code led to the development of • structured programming.

  10. 5 rules of Structured Programming • a. No goto statements are to be used in writing code. • None, nope, don’t do it….

  11. 5 rules of Structured Programming • All programs can be written in terms of three control structures: • sequence, • selection, • and iteration.

  12. 5 rules of Structured Programming.get in, get out • Each control structure has one entrance point and one exit point. • We will sometimes allow for multiple exit points from a control structure using the break statement.

  13. Break command • The break command allows us to leave a loop at any time

  14. 5 rules of Structured Programming • Control structures may be stacked (sequenced) one after the other.

  15. 5 rules of Structured Programming • Control structures may be nested inside other control structures.

  16. sequence, selection, and iteration. • There are only three necessary control structures • sequence, • selection, • and iteration

  17. sequence • Sequence refers to the line-by-line execution as used in your programs so far.

  18. sequence • The program enters the sequence, does each step, and exits the sequence. • This allows for sequences to do only a limited job during each execution.

  19. selection • Selection is the control structure that allows choice among different paths. Java provides different levels of selection: • • One-way selection with an if structure • • Two-way selection with an if-else structure • • Multiple selection with a switch structure

  20. different levels of selection: • • One-way selection with an if structure • • Two-way selection with an if-else structure • • Multiple selection with a switch structure

  21. Iteration refers to looping.. Java provides three loop structures. Iteration

  22. Looping • • while loops • • do-while loops • • for loops

  23. Algorithm Development • An algorithm is a solution to a problem. • . Computer scientists are in the problem-solving business.

  24. Algorithms will range from the easier: "finding the average of two numbers" To more difficult"visiting all the subdirectories on a hard disk, searching for a file." Algorithms

  25. Pseudocode refers to a rough-draft outline of an answer, written in English-like terms. Pseudocode

  26. generally use phrases and words that are close to programming languages, but avoid using any specific language Pseudocode

  27. Don’t skip this stage • Once the pseudocode has been developed, translation into code occurs more easily than if we had skipped this pseudocode stage.

  28. Stepwise refinement • Stepwise refinement is the process of gradually developing a more detailed description of an algorithm.

  29. "finding the average of two numbers" • Get two numbers • Add them together • Divide by 2 • Print out the answer

  30. Stepwise refinement • Problem solving in computer science involves overall development of the sections of a program,

  31. Stepwise refinement • expand each section with more detail,

  32. later work out the individual steps of an algorithm using pseudocode, and then finally writing a code solution. Stepwise refinement..final steps

  33. Relational Operators • A relational operator is a binary operator that compares two values. • 5 = = 3 + 2

  34. Relational Operators << less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to

  35. relational expression • A relational operator is used to compare two values, resulting in a • relational expression. • For example: number > 16 grade == 'F' passing >= 60

  36. boolean value • The result of a relational expression is a • boolean value of either true or false.

  37. Comparing character data • When character data is compared, use the ASCII code values are used to determine the answer.   'A' < 'B' evaluates as true, (65 < 66) 'd' < 'a' evaluates as false, (100 < 97) 't' < 'X' evaluates as false, (116 < 88)

  38. upper case letters come first in the ASCII collating sequence; the lower case letters follow after and consequently have larger ('A' = 65, 'a' = 97). Upper case, lowercase

  39. The three logical operators in the AP subset are AND, OR, and NOT. AND && OR||(two vertical bars-no space) NOT !

  40. logical operators allow us to combine conditions. For example, if a dog is gray and weighs less than 15 pounds it is the perfect lap dog Logical operators compare

  41. The && (and) operator requires both operands (values) to be true for the result to be true. (true && true) -> true (true && false) -> false (false && true) -> false (false && false) -> false && (and) operator

  42. The following are Java examples of using the && (and) operator. ((2 < 3) && (3.5 > 3.0)) -> true ((1 == 0) && (2 != 3)) -> false && (and) operator

  43. The && operator performs short-circuit evaluation in Java. If the first operand in && statement is false, the operator immediately returns false without evaluating the second half. Short Circuit evaluation

  44. The || (or) operator requires only one operand (value) to be true for the result to be true. (true || true) -> true (true || false) -> true (false || true) -> true (false || false) -> false The || (or) operator

  45. Precedence • The order of operations is called precedence in Java.

  46. Precedence and Associativity of Operators OperatorAssociativity ! unary - ++ -- right to left * / % left to right +- left to right < <= > >= left to right == != left to right && (and) left to right || (or) left to right = += -= *= /= right to left

  47. logical operators • logical operators have low precedence in Java, • parentheses are not needed to maintain the correct order of solving problems. • However, they can be used to make complex expressions more readable.

  48. parentheses are not needed ((2 + 3 < 10) && (75 % 12 != 12)) // easier to read

  49. parentheses are not needed (2 + 3 < 10 && 75 % 12 != 12) // harder to read Hhowever, parentheses can be used to make complex expressions more readable.

More Related