Week 2 Meeting 1

If statements

Up to this point, in any given method, all statements have executed sequentially, e.g.,

      System.out.println("Hello");
      System.out.println(
"Hello " + "there");

 

first “Hello” prints, then, on the next line, “Hello there”

 

Decision statements allow the programmer to execute a different “branch” of code depending on whether some expression is true.

Single Branch if

Here is the flow chart for the single branch if:

 

W06Notes01

 

If syntax

if (<boolean expression>)

{

     <statements for the ‘then’ branch>

}

<statement following the if-statement>

 

A boolean expression is an expression that is either true or false, e.g., d < 5 is true if the value stored in the memory location named d is less than 5.

 

if <boolean expression> evaluates to true, execute the statements in the then branch then execute <statement following the if-statement>.

If - else

An if – else statement extends the single branch if to execute one set of statements if the condition is true and another if it is false. 

control flow diagram

cannonC2ifControlflow

If syntax

if (<boolean expression>)

{

     <statements for the ‘then’ branch>

}

else

{

     <statements for the ‘else’ branch>

}

<statement following the if-statement>

 

if <boolean expression> evaluates to true, execute the statements in the then branch then execute <statement following the if-statement>.

 

if <boolean expression> evaluates to false, execute the statements in the else branch then execute <statement following the if-statement>.

 

Caution: Caution: Caution:

Proper indentation allows both you and the people who have to maintain the code you write to understand it much more quickly.  Improper indentation is not tolerated in the 'real world'. 

 

1.    Indentation rules

a.    Opening brace, {,  indent to the same level as the keyword they follow

b.    Indent one level after each { and outdent one level just before the closing brace, }, so the { and the } are at the same level.

c.    For a single-statement then or else branch or loop body, indent the statement one level, then outdent one level after the statement.

d.    else is at the same level as the if it matches.

 

2.    If you leave out the braces following the if or the else , the next statement and only the next statement is considered to be the entire then or else branch, which is why placing a semicolon at the end of the conditional expression of an if statement, e.g., if (x < 5);  is so damaging.  Doing so creates a then branch containing a single, empty, statement. 

a.    Non-empty single statement if branches are actually quite common, and, when you write one, standard programming practice is to omit the braces enclosing this statement.

Caution: Caution: Caution:

 

(See demos/W02N01.java)

 

Practice Exercise

Write a program named PE that will:

Read a double from the user.

If the double is not positive:

print the following

You have entered an illegal value.

The first number must be positive.

 

If the double is positive,

ask the user for a second number (the second number can be negative)

multiply the first number and the second number

print:

Your product is <result of the multiplication>

 

In either case, at the end, print:

All done!