Chapter 5
Making Decisions

1. Planning decision making logic
2. Make decisions with the if and if...else structures
3. Use multiple statements in an if or if...else structure
4. Nest if and if…else statements
5. Use And and Or operators
6. Use the switch statement
7. Use the Conditional and Not operators
8. Operator precedence.

1. Planning decision making logic
Pseudocode requires that you write down the steps needed to accomplish a task.
a. A flow chart is similar to pseudocode but a graphic representation.
b. Every computer decision results in a Boolean value - t or f.  And or not
In sequence structure, one step follows another unconditionally.
d. In decision structure, alternative courses of action are chosen based on a Boolean value.

2. The if and if...else Structures
a. The condition must appear within parentheses.
b. One way selection 

     System.out.println("The score is perfect");      //(single alternative)

c. Two way selection 

     System.out.println("the score is perfect');      //(dual alternative)  
     System.out.println("the score is not perfect):

d. You can use the relational operators to compare the values of primitive data types, but cannot use <,>,<=, or >= to compare objects. The equal == or != (not equal) can be used with objects but it references only the object’s memory address not it values. == only yields true for two objects when they refer to the same object in memory, not when they are different objects with the same value.

3.  Using Multiple  Statements in an if or if else structure

{                                      //nothing will execute if hoursWorked is less than Full_Week
     regularPay=Full_Week * rate;
     overtimePay=(hoursWorked-FULL_Week) * OT_RATE * rate:

a. Correct blocking is crucial to achieving valid output in if statements.  It is perfectly legal to place curly braces around a single statement.
b. Remember any variable you declare in a block is local to that block.

4. Nesting if and if ...else statements  
When you put an if statement inside of another if, it is “nested.”

a. Nested if example:

    if(totalValue>= MIN_VALUE)
        bonus=SALES_BONUS;       //notice the semi-colon is at the end of the nested statement. This series does not deal with the else clause>

b. Nested if…else example:

if (itemsSold>= MIN_ITEMS)
          bonus=MEDIUM_BONUS:    //pay close attention to else statements so that they are nested correctly.                                         

5. Using Logical And and Or operators 
&& is an operator. When using && a complete Boolean expression is needed on each side.
a. Short-circuit evaluation determines whether both side are true.
b. Both must be true in order for the following statement to execute:

if((saleAmount>1000) && (saleAmount<5000)) 

c. || is the or operator.
d. If the first test is false then the second one will be evaluated.

if(selection =='A' || selection =='a') 

e. A range check is a series of statements that determines a set of ranges within which a value falls.

6. Using the switch statement is useful when you need to test a single variable against a series of exact integer (int,byte,short), character, or string values.
a. Switch starts the structure and is followed immediately by a test expression enclosed in parentheses.
b. Case is followed by one of the possible values for the test expression and a semicolon.
c. Break optionally terminates a switch structure at the end of each case.
d. Default optionally is used prior to any action that should occur if the test variable does not match a case.
e. Switch example:

int year
    case 1:
    case 2:                               //if you leave out the break statement and the programs finds a match,
                                          //all the statements in the switch statement

        System.out.println("Sophomore");  //will be executed from that point forward.
        break;  //The break bypasses the rest of the switch structure and execution continues
                //with any statement after the closing burly brace of the switch.
    case 3:     //Code does not have to be written for each case value.
    case 4:
        System.out.println("Invalid year");

f. It is most efficient to list the most common case first, instead of the case with the lowest value. Switch will execute only the true case or default if the break statement is included.
g. The ability to test strings is new in this version of JAVA.
h. The same results from switch can be achieved by using nested if statements.

7. Using the Conditional and Not operators.
The conditional operator is a ternary operator - one that needs three operands
a. Testexpression ? trueResult :  falseResult;     smallNum=(a<b) ? a:b;
b. An advantage to using the conditional operator is the conciseness of the statement.
c. Using the not operator.  Use ! to negate the results of any Boolean expression. Any expression that evaluates as true becomes false when preceded by the not operator, and any false statement becomes true if preceded by the ! operator.

8. Operator Precedence: in JAVA.








Multiplication, division, modulus



Addition, subtraction







== !=


Logical AND



Logical Or








d. Remember the () is used to override precedence. Additionally, parentheses can be helpful in making your program more understandable.

Description: flow chart decision
False= stay home                       true= goto 1st grade.

  false   ◊  true