Title: CS 303E Class 10 Decision Making: if and ifelse statements
1CS 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
2Linear 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.
3Branching 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
4Branching 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
5Empty false branch
- If the condition is true, execute statement
- otherwise, do nothing
- Condition is an expression that can be true or
false.
false
6Syntax -- if Statement
- if (condition) // one-way branch
- statement
- if (condition) // two-way branch
- statement
- else
- statement
7if 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
8Compound Statements
- Often, one statement isnt enough.
- So use a compound statement
-
- statement
- . . . zero or more statements
- statement between braces
-
9if with Compound Statements
- if (condition) if (condition)
- statement statement
- . . . . . .
- statement statement
-
- else
- statement
- . . .
-
10Example
- // 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)
11Conditions 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.
12Boolean 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
13Examples
- 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?
14Indentation 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.
15Multi-way Decisions
16Multi-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.
17Nested Decision Making
Figure 7 Income Tax Computation