CS 303E Class 10 Decision Making: if and ifelse statements - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS 303E Class 10 Decision Making: if and ifelse statements

Description:

As soon as questions of will or decision. or reason or choice of action arise, human science is at a loss. Noam Chomsky. CS303E. Decision Making: if and if/else. 2 ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 18
Provided by: MikeS2
Category:

less

Transcript and Presenter's Notes

Title: CS 303E Class 10 Decision Making: if and ifelse statements


1
CS 303E Class 10Decision Makingif and if-else
statements
As soon as questions of will or decision or
reason or choice of action arise, human science
is at a loss.Noam Chomsky
2
Linear flow of control
  • All we have seen so far is one statement after
    another
  • The program must execute each statement in the
    order written. The methods we have created in
    our classes cant make decisions.
  • A programming language that cannot branch is
    pretty limited in its usefulness.

3
Branching making decisions
  • If the condition is true, execute statement_1
  • otherwise, execute statement_2
  • The condition is an expression that can be true
    or false, such as x gt 0

4
Branching example
  • An example for a checking account
  • The account gets interest or a penalty depending
    on whether the balance is over 0.

myBalance myBalance interest
true
myBalance gt 0
myBalance myBalance PENALTY
false
5
Empty false branch
  • If the condition is true, execute statement
  • otherwise, do nothing
  • Condition is an expression that can be true or
    false.

false
6
Syntax -- if Statement
  • if (condition) // one-way branch
  • statement
  • if (condition) // two-way branch
  • statement
  • else
  • statement

7
if Statement Examples
  • if (sales gt SALES_FOR_BONUS)
  • commission commission BONUS_RATE
  • if (balance gt 0)
  • balance balance (balance INTEREST_RATE)
  • else
  • balance balance - PENALTY

8
Compound Statements
  • Often, one statement isnt enough.
  • So use a compound statement
  • statement
  • . . . zero or more statements
  • statement between braces

9
if with Compound Statements
  • if (condition) if (condition)
  • statement statement
  • . . . . . .
  • statement statement
  • else
  • statement
  • . . .

10
Example
  • // Pay at payRate with time and one half for
  • // overtime
  • pay hoursWorked payRate
  • if (hoursWorked gt FULL_TIME_HOURS)
  • overtime hoursWorked - FULL_TIME_HOURS
  • pay pay overtime (payRate / 2)

11
Conditions for if statements
  • Conditions are evaluated for the if statement
  • The condition will either evaluate to true or
    false
  • One form of condition is
  • expression relop expression
  • where relop is a relational operator.

12
Boolean Relational Operators
  • Math Name Java Java Example
  • equal balance 0
  • ? not equal ! answer ! "Yes"
  • gt greater than gt expenses gt income
  • ? greater than or equal gt points gt 60
  • lt less than lt pressure lt max
  • ? less than or equal lt expenses lt income

13
Examples
  • count 1 gt a b
  • far - near gt 10
  • Relational operators have lower precedence
  • than arithmetic operators. The expressions are
    evaluated before the comparison.
  • Invalid
  • a lt b lt c // syntax error
  • a b c // syntax error
  • //but we can do these use logical operator
  • What happens when you compare objects?

14
Indentation Helps Programmers but Compilers
Ignore it.
  • if (hours gt FULL_TIME_HOURS)
  • over hours - FULL_TIME_HOURS
  • pay pay over (rate / 2)

All simple statements within another statement
should be indented further to the right than the
outer statement. The two assignment statements
are within the if. All statements in a sequence
of statements in a compound statement should be
indented the same amount, as the two assignment
statements are.
15
Multi-way Decisions
  • Several possible paths

16
Multi-way Decisions
  • if (balance gt 0)
  • System.out.println (Positive)
  • else if (balance lt 0)
  • System.out.println (Negative)
  • else
  • System.out.println (Zero)
  • Compound statements can be used here too.

17
Nested Decision Making
Figure 7 Income Tax Computation
Write a Comment
User Comments (0)
About PowerShow.com