Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison."— Presentation transcript:

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 operators  switch Statements  Conditional Operators

2 PHY281Flow ControlSlide 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 overdrawnIf it's in the black

3 PHY281Flow ControlSlide 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 ( ) ; or if ( ) ; or if ( ) { ;... } if ( ) { ;... } Some people prefer: Note: No semicolon ;

4 PHY281Flow ControlSlide 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

5 PHY281Flow ControlSlide 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

6 PHY281Flow ControlSlide 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 ( ) { ; } else { ; } 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

7 PHY281Flow ControlSlide 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(); }

8 PHY281Flow ControlSlide 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:

9 PHY281Flow ControlSlide 9 switch Statements The switch statement is useful if there are several possibilities: switch ( ) { case : ; break; case : ; break; default: ; } 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 ;

10 PHY281Flow ControlSlide 10 Conditional Operator The ? operator behaves like an abbreviated if... else = ( ) ? : ; 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.


Download ppt "PHY281Flow ControlSlide 1 Decisions In this section we will learn how to make decisions in a Java program  if Statements  if... else Statements  Comparison."

Similar presentations


Ads by Google