Docsity

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

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.

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)

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 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”

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.

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