PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java...

10
PHY281 Flow Control Slide 1 Decisions In this section we will learn how to make decisions in a Java program if Statements if ... else Statements Comparison operators switch Statements Conditional Operators

Transcript of PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java...

Page 1: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 1

Decisions

In this section we will learn how to make decisions in a Java program if Statements

if ... else Statements

Comparison operators

switch Statements

Conditional Operators

Page 2: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 2

Decisions

Usually the computer executes the statements you provide one after the other from the top to the bottom of the program.

However, at some points you might want it to take different routes depending on what information it is given or as a result of what it calculates.

Finish

Say it's OKGive a warning

Check my Bank Balance

Start

If it's overdrawn If it's in the black

Page 3: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 3

The if statement is used to test if a condition is true or false and only to take action if it is true:

if Statements

if (total >= 70) result = "Grade A!";if (total < 40) {

result = "Failure";attempts++;

}

if ( <test> ) <expression > ; or if ( <test> ) <expression > ; or if ( <test> ) { <expression 1> ; <expression 2> ; . . . }

if ( <test> ) { <expression 1> ; <expression 2> ; . . .}

Some people prefer:

Note: No semicolon ;

Page 4: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 4

Testing Equality

if (total == 0 && reason != "Absent") {result = "Failure";

}

Can combine several tests using && (AND) and || (OR) operators.

Note: to check if two things are equal use == not =

int x = 5;int y = 10;if (x = y) { g.drawString("x and y are equal", 50, 50);}

In Java this does not compile but in C++ would probably give the wrong result: Tries to set x equal to y rather

than test whether they were equal

Fails to compile because the if wants to tests boolean (logical) expression and x is an int

Page 5: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 5

Comparison Operators

Comparison Operators - used to compare variables or objects

< less than<= less than or equal to> greater than>= greater or equal to== equal to!= not equal to

Page 6: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 6

if . . . else Statements

The if ... else statement is used to test if a condition is true or false and to take action in either case:

if (total >= 40) { result = "Success";}else { result = "Failure";}

if ( <test> ) { <expression1 > ;}else { <expression2 > ;}

Can also combine several else and if statements:

if (total >= 70) { result = "Grade A";}else if (total >=60 && total < 70) { result = "Grade B";}else { result = "Lower than A B";}

This bit is redundant because we already know it's true

Page 7: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 7

if - else Example

public class Marks extends Applet implements AdjustmentListener{ Scrollbar slider; int mark = 0;

public void init() { slider = new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 101); add(slider); slider.addAdjustmentListener(this); }

public void paint(Graphics g) { String result; if (mark >= 40) { result = "Pass"; } else { result = "Fail"; } g.drawString("Mark = " + mark + " Result = " + result, 50, 50); }

public void adjustmentValueChanged(AdjustmentEvent e) { mark = slider.getValue(); repaint(); }}

Page 8: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 8

if - else if Example

public void paint(Graphics g) { String result; if (mark >= 70) { result = "Grade A"; } else if (mark >= 60) { result = "Grade B"; } else if (mark >= 50) { result = "Grade C"; } else if (mark >= 45) { result = "Grade D"; } else if (mark >= 40) { result = "Grade E"; } else { result = "Fail"; } g.drawString("Mark = " + mark + " Result = " + result, 50, 50);}

Now change the paint( ) method to this:

Page 9: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 9

switch Statements

The switch statement is useful if there are several possibilities:

switch ( <variable> ) { case <value1>: <expression1 > ; break; case <value2>: <expression2>; break; default: <expression3> ;}

switch (grade) { case 'A': result = "First rate"; break; case 'B': result = "Pretty Good"; break; default: result = "Ah well";}

Suggest you use switch sparingly. It is easy to screw up and everything can be done using if else.

Does this if all others fail

break tells the computer to leave the switch block - if you leave it out it carries on through the next case as well

Note : not ;

Page 10: PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison.

PHY281 Flow Control Slide 10

Conditional Operator

The ? operator behaves like an abbreviated if ... else

<variable> = (<test>) ? <value if true> : <value if false>;

if (total >= 40) { result = "Success";}else { result = "Failure";}

result = (total >= 40) ? "Success" : "Failure";

can be written:

Suggest you don't use this as it's not easy to understand the code afterwards.