Content Analysis
Object Oriented Design and Programming
Week 3
Kent Institute Australia Pty. Ltd.
ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051
Version 2 – 18th December 2015
1
SLIDE TITLE
Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning
2
2
Programming Logic and Design Ninth Edition
Chapter 3,4
Understanding Programming Structures
3
Programming Logic and Design, Ninth Edition
Objectives
In this chapter, you will learn about:
The disadvantages of unstructured spaghetti code
The three basic structures—sequence, selection, and loop
The need for structure
Recognizing structure
4
Programming Logic and Design, Ninth Edition
4
The Disadvantages of Unstructured Spaghetti Code
Spaghetti code
Logically snarled program statements
Often a complicated mess
Programs often work but are difficult to read and maintain
Confusing and prone to error
Unstructured programs
Do not follow the rules of structured logic
Structured programs
Follow the rules of structured logic
5
Programming Logic and Design, Ninth Edition
5
Unstructured Spaghetti Code
6
Programming Logic and Design, Ninth Edition
6
Understanding the Three Basic Structures
Structure
Basic unit of programming logic
Each structure is one of the following:
Sequence structure
Selection structure (decision structure)
Loop structure
any program can be constructed using one or more of these three structures
7
Programming Logic and Design, Ninth Edition
7
Understanding the Three Basic Structures (continued)
Sequence structure
Perform actions or tasks in order
No branching or skipping any task
Selection structure (decision structure)
Ask a question, take one of two actions based on testing a condition. Known as evaluating a Boolean expression, a statement that is either true or false
Often called if-then-else
Dual-alternative ifs or single-alternative ifs
Loop structure
Repeat actions while a condition remains true
8
Programming Logic and Design, Ninth Edition
8
The Sequence Structure
9
Programming Logic and Design, Ninth Edition
9
Sequence Structure
10
Programming Logic and Design, Ninth Edition
The Selection Structure
11
Programming Logic and Design, Ninth Edition
11
The Selection Structure (continued -1)
Single-alternative ifs
if employee belongs to dentalPlan then
deduct $40 from employeeGrossPay
An else clause is not required
null case
Situation where nothing is done
12
Programming Logic and Design, Ninth Edition
12
The Selection Structure (continued -2)
13
Programming Logic and Design, Ninth Edition
13
The Selection Structure (continued -3)
Dual-alternative ifs
Contains two alternatives
The if-then-else structure
if someCondition is true then
do oneProcess
else
do theOtherProcess
endif
14
Programming Logic and Design, Ninth Edition
14
The Selection Structure (continued -4)
int number=8;
if(number%2==0)
System.out.println(“ Number entered is even”);
else
System.out.println(“Number entered is odd”);
15
Programming Logic and Design, Ninth Edition
Check the usage of semicolons and case of if-else
15
The Selection Structure (continued -5)
16
Programming Logic and Design, Ninth Edition
Many alternatives ifs:
If you have more than one condition to check and do some tasks then:
If condition one is true
then do something
Else if condition two is true
then do something
Else if condition three is true
then do something
Else
do something
End IF
16
The Selection Structure (continued -6)
17
Programming Logic and Design, Ninth Edition
Relational Operators
-Sometimes, we check conditions using relational operators.
| Operators in java | Name | Usage and discussion |
| == | Equivalency operator | a==b, a is equal to b |
| > | Greater-than operator | a>b, a is greater than b |
| < | Less-than operator | a<b, a is less than b |
| >= | Greater-than-or-equal-to operator | a>=b, a is greater than or equal to b |
| <= | less-than-or-equal-to operator | a<=b, a is less than or equal to b |
| != | Not-equal-to operator | a>=b, a is not equal to b |
17
The Selection Structure using Relational Operators(continued -8)
18
Programming Logic and Design, Ninth Edition
18
The Selection Structure using Relational Operators(continued -8)
19
Programming Logic and Design, Ninth Edition
19
The Selection Structure (continued -9)
20
Programming Logic and Design, Ninth Edition
Logical Operators- common ones that can also be used in Java to check conditions.
https:// www.youtube.com/watch?v=aVVAy1KOskg
Watch above video of logical operators.
| Logical operators in Java | Name | Usage and Discussion |
| && | Logical AND | a>b&&a<c, a is greater than b and smaller than c |
| || | Logical OR | a==b||a<c, a is equal to b or smaller than c |
20
The Selection Structure using Logical Operators(continued -10)
21
Programming Logic and Design, Ninth Edition
21
Understanding Precedence When Combining AND and OR Operators
Combine multiple AND and OR operators in an expression
When multiple conditions must all be true, use multiple ANDs
if score1 >= MIN_SCORE AND score2 >= MIN_SCORE AND score 3 >= MIN_SCORE then
classGrade = "Pass"
else
classGrade = "Fail"
endif
22
Programming Logic and Design, Ninth Edition
22
Understanding Precedence When Combining AND and OR Operators (continued -1)
When only one of multiple conditions must be true, use multiple ORs
if score1 >= MIN_SCORE OR score2 >= MIN_SCORE OR score3 >= MIN_SCORE then
classGrade = "Pass"
else
classGrade = "Fail"
endif
23
Programming Logic and Design, Ninth Edition
23
When AND and OR operators are combined in the same statement, AND operators are evaluated first
if age <= 12 OR age >= 65 AND rating = "G"
Use parentheses to correct logic and force evaluations to occur in the order desired
if (age <= 12 OR age >= 65) AND rating = "G"
24
Programming Logic and Design, Ninth Edition
Understanding Precedence When Combining AND and OR Operators (continued -2)
24
Selection Structure:-Case Structure
Specialised Selection Structure.
Used when there are several distinct possible values for a single variable.
Each value requires subsequent action.
Usage
-Only use when a series of decision is made based on single expression.
25
Programming Logic and Design, Ninth Edition
Case Structure(continued)
26
Programming Logic and Design, Ninth Edition
Flowchart and pseudocode for case structure
Case Structure in Java
switch(classname)
{
case(“Freshman”):
System.out.println(“Tuition Fee is 75”);
break;
case(“Sophomore”):
System.out.println(“Tuition Fee is 50”);
break;
case(“Junior”):
System.out.println(“Tuition Fee is 30”);
break;
default:
System.out.println(“Tuition Fee is 10”);
}
27
Programming Logic and Design, Ninth Edition
In-class Activity
Write a rough program to ask user to enter a number and display the name of day according to the number
If user enter 1 print “ Day is Sunday”
If user enter 2 print “Day is Monday”
And so on….
Handle invalid input as well. (Hint- you can do it in default/else)
28
Programming Logic and Design, Ninth Edition
Summary
Spaghetti Code is best to avoid.
Use programming structures like sequencing, selection and looping.
Sequencing is step by step instructions in program.
Selection Structure is used when program has to take actions according to conditions.
If, if-else , case structure are selection structures
29
Programming Logic and Design, Ninth Edition
kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051
30
30