a8 – control structures if, if-else, switch control of flow in java any sort of complex program...
TRANSCRIPT
![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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/1.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/2.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/3.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/4.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/5.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/6.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/7.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/8.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/9.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/10.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/11.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/12.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/13.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/14.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/15.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/16.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/17.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/18.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/19.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/20.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/21.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/22.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/23.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/24.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/25.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/26.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/27.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/28.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/29.jpg)
"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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/30.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/31.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/32.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/33.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/34.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/35.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/36.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/37.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/38.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/39.jpg)
• 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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/40.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/41.jpg)
&& (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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/42.jpg)
&& (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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/43.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/44.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/45.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/46.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/47.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/48.jpg)
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](https://reader031.vdocument.in/reader031/viewer/2022013003/551ab43d550346856e8b504f/html5/thumbnails/49.jpg)
parentheses are not needed
(2 + 3 < 10 && 75 % 12 != 12)
// harder to read
Hhowever, parentheses can be used to make complex expressions more readable.