Decision Structures

The if Statement

Decides whether or not a section of code executes.

if (boolean expression is true)
    execute next statement

Flowcharts

If statements can be modeled as a flow chart.

One-line Style

This—

if (average > 95)
    grade = 'A';

—is functionally equivalent to:

if (average > 95) grade = 'A';

Block if Statements

Curly braces are used to group conditionally executed statements.

e.g.,

if (expression)
{
    statement1;
    statement2;
}

Note: Some trick questions will leave out the curly braces but indent blocks to look like they are conditionally executed when only the first line is conditional.

Relational Operators and Boolean Expressions

Boolean Expression: Any variable or calculation that results in a true or false condition.

Typically, we’ll use relational operators in boolean expressions because they return true or false.

Relational OperatorMeaning
>is greater than
<is less than
>=is greater than or equal to
<=is less than or equal to
==is equal to
!=is not equal to

Reminder: = is the assignment operator, == is the comparison operator!

Flag

Flag: boolean variable that monitors some condition in a program

boolean isPrime = true;
if (5 % 4 == 0)
    isPrime = false;
if (5 % 3 == 0)
    isPrime = false;
if (5 % 2 == 0)
    isPrime = false;
if (isPrime)
    System.out.println("5 is prime");

Comparing Characters

Characters can be tested with relational operators.

char x = 'A';
if (x < 'B')
    System.out.println("A is less than B");

if-else Statements

if (expression)
    statementTrue;
else
    statementFalse;

Note: Curly braces not required if there is only one statement

Nested if statements

Nested if Statement: An if statement inside another if statement (single or block, doesn’t matter)

if (coldOutside)
{
    if (snowing)
    {
        wearParka();
    }
    else
    {
        wearJacket();
    }
}
else
{
    wearShorts();
}

if-else-if Statements

if (expression)
{
    // a
}
else if (expression)
{
    // b
}
else if (expression)
{
    // c
}
else{
    // catch whatever
}

Note how only one block can be executed

Logical Operators

Two logical (AND, OR) and one unary (NOT) operators are provided in Java.

OperatorMeaning
&&AND
||OR
!NOT

The System.out.printf Method

Print formatted string.

Examples:

double grossPay = 874.12;
System.out.printf("Your pay is %f \n", grossPay);

double grossPay = 874.12111;
System.out.printf("Your pay is %.2f \n", grossPay);

double grossPay = 5874.12111;
System.out.printf("Your pay is %,.2f \n", grossPay);

String name = "Ringo";
System.out.printf("Your name is %s", name);

int number = 9;
System.out.printf("The value is %6d", number);

String.format

Works exactly like System.out.printf, except it returns a reference to the formatted string instead of printing to the console.

Comparing String Objects

Common Mistake: Trying to compare two String objects with relational operators (==)

String name1 = "john", name2 = "John";
String name3 = new String("John");
String name4 = new String("John");

Common methods:

The Conditional Operator

BooleanExpression ? Value1 : Value2
z = x > y ? 10 : 5;
// functional equivalent:
if (x>y)
    z = 10;
else
    z = 5;

The switch Statement

Allows you to use value of variable/expression to determine how a program will branch.

switch (SwitchExpression)
{
    case CaseExpression;
        break;
    case CaseExpression;
        break;
    case CaseExpression;
        break;
    default:
        // blah
}

The case Statement