Presentation is loading. Please wait.

Presentation is loading. Please wait.

A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.

Similar presentations


Presentation on theme: "A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow."— Presentation transcript:

1

2 A8 – Control Structures if, if-else, switch

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

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

5 Getting started The most basic of these control structures is the if statement,

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

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

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

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

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

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

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

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

14 Break command The break command allows us to leave a loop at any time

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

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

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

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

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

20 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

21 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

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

23 Looping while loops do - while loops for loops

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

25 Algorithms 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."

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

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

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

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

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

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

32 Stepwise refinement expand each section with more detail,

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

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

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

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

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

38 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)

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

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

41 Logical operators compare 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

42 && (and) operator 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

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

44 Short Circuit evaluation 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.

45 The || (or) operator 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

46 Precedence The order of operations is called precedence in Java.

47 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

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

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

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


Download ppt "A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow."

Similar presentations


Ads by Google