25
Flow of Control Java Programming Mrs. C. Furman January 5, 2009

Flow of Control Java Programming Mrs. C. Furman January 5, 2009

Embed Size (px)

Citation preview

Flow of Control

Java Programming

Mrs. C. Furman

January 5, 2009

Flow of Control

The order in which statements are executed

Order is linear in fashion unless otherwise specified.

Linear – program is executed one statement at a time, one after the other.

Conditional statement/ Selection Statement

Allows us to choose which statement will be executed next.

In Java If statement / if-else statement and switch statement.

Decision Making

Boolean Expressions / Conditional Decisions are made based upon

boolean expressions. The expressions are evaluated as either true or false.

Relationship Operators

== Equal to

!= Not equal to

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

Example:

5==5

X<=3

x+3 != y + 5

Logic Operators

! – NOT Changes the truth value

&& - And True if both values are true

|| - OR True when one of the values are true

Precedence – Evaluate in the order of !, && then ||

If Statements We may want to perform different

operations based on different conditions.

Ex. We want to print a certificate based on a students grade

>= 95 – high honor roll

<95 and >= 90 – honor roll

<90 and >= 85 – Merit roll

<85 – no certificate

Grade example continued.if(grade >= 95)

{ System.out.println (“High Honor Roll”); }

If the boolean expression is true High Honor Roll is printed.

If the boolean expression is false Skip the output line and go to the next line in the program.

if structure

if (grade >= 95)

keyword boolean condition

The boolean condition must be placed inside parenthesis.

if structure continue

if(grade >= 95)

{

System.out.println (“High Honor Roll”);

}

{ } – we use braces to surround the body of the if statement.

The body is a block of code.

Using if…else

if (grade >= 95)

{

System.out.println (“High Honor Roll”);

}

else

{System.out.println (“Needs Improvement”);

}

grade = 98Output: High Honor Roll

grade = 88Output: Needs Improvement

if structure

if (grade >= 95)

{ System.out.println (“High Honor Roll”);}

else

{ The else comes after the body of the if

is closed, then the body of the else is also enclosed in braces.

if structureif (grade >= 95){ System.out.println (“High Honor Roll”);}else{ when the boolean condition is true, the

body of the if is executed and the else body is skipped.

when the boolean condition is false, the body of the if is skipped and the else body is executed.

Two-way Decisions

This windshield wiper decision is a two-way decision (sometimes called a binary decision.) It seems small, but in programming complicated decisions are made of many small decisions.

Grade output contif(grade >= 95)

{ System.out.println (“High Honor Roll”);}

if (grade < 95 && grade >= 90)

{ System.out.println (“Honor Roll”); }

if (grade <90 && grade >= 85)

{ System.out.println (“Merit Roll”); }

Grade Output Cont.

In the previous slide, we see 3 separate if statements.

Each if statement will be executed! And executed independent of each – other.

What would happen if grade was 98? 93? 80?

Grade Output Cont.

if… else would be a much more efficient way of handling the multiple conditions presented in the previous scenario.

The next slide shows nested if statements, which will be much more efficient.

Using if…else

if (grade >= 95){ System.out.println (“High Honor Roll”); }else{ if (grade >=90)

{ System.out.println (“Honor Roll”); }else { if (grade >=85)

{ System.out.println (“Merit Roll”); }}

}

Program Example

public class EvenOrOdd{

public static void main (String [] args){

int num;Scanner input = new Scanner (System.in);System.out.println (“Enter an integer: “);num = input.nextInt();

if (num % 2 == 0){ System.out.println (num + “ is even.”);

System.out.println (“Even is awesome”);}else { System.out.println (num + “ is odd.”);

System.out.println (“Odd is weird”);}System.out.println (“Bye Bye”); // this statement is outside the if

}}

num = 4Output:

4 is evenEven is awesome

Bye Bye

num = 5Output:5 is odd

Odd is weirdBye Bye

if.. else

if (num % 2 == 0)

System.out.println (num + “ is even.”);

else

System.out.println (num + “ is odd.”);

System.out.println (“Odd is weird”);

System.out.println (“Bye Bye”);

if.. elseif (num % 2 == 0)

System.out.println (num + “ is even.”);else

System.out.println (num + “ is odd.”);System.out.println (“Odd is weird”);System.out.println (“Bye Bye”);

What would the output be for an even number? odd number? When you omit the braces, the if and else only take the 1st

statement. If you want to execute more than one statement, you have to

put the body of the code into curly braces. The indentation helps our human eyes determine what is in

the if and what isn’t. It has no impact on how the computer interprets the code.

Recall: Short Circuit If the left hand operand is sufficient to

determine whether or not we are true / false, the right hand operator is not evaluated.

Ex. int x = 5;

if ((x == 5) || (x != 3)) - true

first part true with or

if ((x != 5) && (x !=3)) – false

first part false with and

Common Errors!!

If(total >= 25);

{

}

if(total = 10)

{

}

Comparing Strings

String class contains an equals method which returns a boolean, true, if the two strings contain exactly the same characters, and false otherwise.

Ex. String name = “Furman”;boolean same;same = name.equals (“Furman”);same = (name == “Furman”);

Alphabetical Order with Strings To determine alphabetical order use the compareTo method.

String name = “Furman”;int greaterOrLess;greaterOrLess = name.compareTo(“Gilbert”)

greaterOrLess is negativegreaterOrLess = name.compareTo (“Dog”);

greaterOrLess is positivegreaterOrLess = name.compareTo(“Furman”);

greaterOrLess is 0.