A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program...

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

Transcript of A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program...

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

A8 – Control Structures

if,

if-else,

switch

Page 2: 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 in Java

• Any sort of complex program must have

some ability to control flow.

Page 3: 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.

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

Getting started

• The most basic of these control structures is

the if statement,

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

The other most basic

• . The other most basic of these control

structures is the if-else,

•  

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

The other other most basic

• Huh?

• and then the switch statement.

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

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.

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

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.

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

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.

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

5 rules of Structured Programming

• a. No goto statements are to be used in writing code.

• None, nope, don’t do it….

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

5 rules of Structured Programming

• All programs can be written in terms of three control structures:

– sequence, •selection,

–and iteration.

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

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.

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

Break command

• The break command allows us to

leave a loop at any time

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

5 rules of Structured Programming

• Control structures may be stacked (sequenced) one after the other.

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

5 rules of Structured Programming

• Control structures may be nested inside other control structures.

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

sequence, selection, and iteration.

• There are only three necessary control structures

• sequence,

• selection,

• and iteration

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

sequence• Sequence refers to the line-by-line

execution as used in your programs so far.

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

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.

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

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

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

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

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

Iteration

•Iteration refers to looping..

• Java provides three loop structures.

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

Looping

• • while loops

• • do-while loops

• • for loops

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

Algorithm Development

• An algorithm is a solution to a problem.

• . Computer scientists are in the problem-solving business.

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

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

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

Pseudocode

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

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

Pseudocode

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

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

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.

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

Stepwise refinement

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

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

"finding the average of two numbers"

• Get two numbers

• Add them together

• Divide by 2

• Print out the answer

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

Stepwise refinement

• Problem solving in computer science involves overall development of the sections of a program,

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

Stepwise refinement

• expand each section with more detail,

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

Stepwise refinement..final steps

• later work out the individual steps of an algorithm using

pseudocode,

• and then finally writing a code solution.

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

Relational Operators

• A relational operator is a binary operator that compares two values.

• 5 = = 3 + 2

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

Relational Operators

< < less than

> greater than

<= less than or equal to

>= greater than or equal to

== equal to

!= not equal to

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

relational expression

• A relational operator is used to compare two values, resulting in a

• relational expression. • For example:

number > 16grade == 'F'passing >= 60

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

boolean value

• The result of a relational expression is a

• boolean value of either

true or false.

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

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)

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

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

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

• The three logical operators in the AP subset are

• AND,

• OR, and

• NOT.

• AND &&

• OR|| (two vertical bars-no space)

NOT !

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

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

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

&& (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

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

&& (and) operator

The following are Java examples of using the && (and) operator.

•  •  

• ((2 < 3) && (3.5 > 3.0)) -> true• ((1 == 0) && (2 != 3)) -> false

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

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.

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

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

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

Precedence

• The order of operations is called precedence in Java.

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

Precedence and Associativity of Operators

Operator Associativity

! 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

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

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.

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

parentheses are not needed

((2 + 3 < 10) && (75 % 12 != 12))

// easier to read

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

parentheses are not needed

(2 + 3 < 10 && 75 % 12 != 12)

// harder to read

Hhowever, parentheses can be used to make complex expressions more readable.