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

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

Dec 28, 2015

Download

Documents

Lilian Porter
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Flow of Control Java Programming Mrs. C. Furman January 5, 2009.

Flow of Control

Java Programming

Mrs. C. Furman

January 5, 2009

Page 2: 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.

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

Conditional statement/ Selection Statement

Allows us to choose which statement will be executed next.

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

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

Decision Making

Boolean Expressions / Conditional Decisions are made based upon

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

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

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

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

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 ||

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

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

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

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.

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

if structure

if (grade >= 95)

keyword boolean condition

The boolean condition must be placed inside parenthesis.

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

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.

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

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

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

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.

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

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.

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

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.

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

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”); }

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

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?

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

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.

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

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”); }}

}

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

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

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

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”);

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

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.

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

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

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

Common Errors!!

If(total >= 25);

{

}

if(total = 10)

{

}

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

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”);

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

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.