Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Control Structures - Business Application Programming - Lecture Slides, Slides of Applications of Computer Sciences

Some concept of Business Application Programming are Additional Capabilities, Additional Components, Additional Class Features, Control Structures, Input and Formatting, Strings and Characters. Main points of this lecture are: Control Structures, Class Activities, Data Manipulation, Validation and Formatting, Sequence, Selection, Repetition, Rewritten, Structured-Programming Summary, Repetition

Typology: Slides

2012/2013

Uploaded on 04/27/2013

mehr5
mehr5 🇮🇳

4.4

(8)

36 documents

1 / 85

Toggle sidebar

Related documents


Partial preview of the text

Download Control Structures - Business Application Programming - Lecture Slides and more Slides Applications of Computer Sciences in PDF only on Docsity!

Control Structures

Chapters 4, 5, & 6 Overview

Today’s Lesson Plan

  • Discuss labs, quizzes, and TDA topics (10 min)
    • Discussion posts should cite sources
  • Class Activities – covers Chapter 4, 5, & 6 (120 min)
    • Discuss the 7 Java control Structures. Start with the sequence

structure. (30 min)

  • Discuss the three selection structures – single selection = if; double

selection = if/else; multiple selection = switch. (30 min)

  • Discuss the three repetition structures – while ; do/while; for. (

min)

  • Discuss data manipulation, validation and formatting. (30 min)
  • Coding Exercises & break (90 min)
  • Code Ex 1 – p 249 – # 5.2.8 (a & b)
  • Code Ex 2 – p 323 – # 6.4.
  • Code Ex 3 – p 225 – # 4.5.

Structured-Programming Summary

• All programs broken down into

– Sequence

– Selection

  • if , if/else , or switch
    • Any selection can be rewritten as an if statement

– Repetition

  • while , do/while , or for
    • Any repetition structure can be rewritten as a while statement

Flow of Control

• the order in which program

statements are executed

WHAT ARE THE POSSIBILITIES...

Flow of Control

  • is Sequential unless a “control structure” is used to change

that

  • there are 2 general types of control structures that change

a sequential flow:

Selection (also called branching)

Repetition (also called looping)

Java control structures

• Selection

if

if... else

switch

• Repetition

for loop

while loop

do... while loop

Selection Statements

• if-then-else statement: implements a

decision structure for two alternatives

Syntax:

if (condition) statement executed if condition is true; else statement executed if condition is false;

The if-then-else Statement

if-then-else performs instructions based on

the result of a comparison  Place statements on separate lines for readability

Syntax:

if-then-else provides two-way

selection

between executing one of 2 clauses (the if

clause or the else clause)

TRUE (^) FALSE

if clause else clause

condition

If-Then-Else Syntax

if ( Condition )

StatementA

else

StatementB

NOTE: StatementA and StatementB each can be a

single statement, a null statement, or a block.

Use of blocks is recommended

if ( Condition ) {

} else {

}

“if clause”

“else clause”

The if-then-else Flowchart

The if-

then-else

flowchart.

Example (Multiple Lines)

int carDoors, driverAge ; float premium, monthlyPayment ;

... if ( (carDoors == 4 ) && (driverAge > 24) ) { premium = 650.00 ; System.out.println(“ LOW RISK “ ); } else { premium = 1200.00 ; System.out.println(“ HIGH RISK ”) ; }

monthlyPayment = premium / 12.0 + 5.00 ;

What happens if you omit braces?

if ( (carDoors == 4 ) && (driverAge > 24) ) premium = 650.00 ; System.out.println(“ LOW RISK “) ; else premium = 1200.00 ; System.out.println(“ HIGH RISK ”) ;

monthlyPayment = premium / 12.0 + 5.00 ;

COMPILE ERROR OCCURS. The “if clause” is the single statement following the if.

Braces can only be omitted when

each clause is a single statement

if ( lastInitial <= ‘K’ )

volume = 1;

else

volume = 2;

System.out.println(“Look it up in volume # “

  • volume + “ of NYC phone book”);

The Selection Condition

• The condition is evaluated to its boolean

value:

– Can ONLY use boolean values

• The else portion is optional

– It is executed only if the condition is false

• The condition may be any valid Java

expression that has a boolean value

Relational Expressions

• Relational expression: compares two

operands or expressions using relational

operators

– Evaluates to true or false

Logical Operators

 Logical operators

 AND ( && ): condition is true only if both expressions are true  OR ( || ): condition is true if either one or both of the expressions is true  NOT (! ): changes an expression to its opposite state; true becomes false, false becomes true

Logical Expressions

LOGICAL

EXPRESSION MEANING DESCRIPTION

! p NOT p! p is false if p is true ! p is true if p is false

p && q p AND q p && q is true if both p and q are true. It is false otherwise.

p || q p OR q p || q is true if either p or q or both are true. It is false otherwise.

Caveats! (obscure)

 Comparing single and double precision values for

equality ( == ) can lead to errors because values are

stored in binary

 Reason is that numbers like 0.1 cannot be

represented exactly in binary

 Thus, testing for exact equality of such numbers

can fail

 Instead, test that the absolute value of the

difference is within an acceptable range, epsilon (ε)

 Example:

abs(operandOne – operandTwo)<0.

What is the value?

int age ;

boolean isSenior, hasFever ;

float temperature ;

age = 20;

temperature = 102.0 ;

isSenior = (age >= 55) ; // isSenior is false

hasFever = (temperature > 98.6) ; // hasFever is true

EXPRESSION VALUE

isSenior && hasFever?

isSenior || hasFever?

! isSenior?

! hasFever?

The value is…?

int age, height;

age = 25;

height = 70;

EXPRESSION VALUE

! (age < 10)?

! (height > 60)?

Write an expression for each

taxRate is over 25% and income is less than $20000

temperature is less than or equal to 75 or humidity is less than

70%

age is over 21 and age is less than 60

age is 21 or 22

Some Answers

(taxRate > .25) && (income < 20000)

(temperature <= 75) || (humidity < .70)

(age > 21) && (age < 60)

(age == 21) || (age == 22)

WARNING about Expressions in Java

  • “Boolean expression” means an expression whose value is

true or false

  • an expression is any valid combination of operators and

operands

  • each expression has a value
  • this can lead to UNEXPECTED RESULTS
  • construct your expressions CAREFULLY
  • use of parentheses is encouraged
  • otherwise, use precedence chart to determine order