Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Flow of Control Java Programming Mrs. C. Furman January 5, 2009."— Presentation transcript:

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

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

3 Conditional statement/ Selection Statement Allows us to choose which statement will be executed next. In Java  If statement / if-else statement and switch statement.

4 Decision Making Boolean Expressions / Conditional Decisions are made based upon boolean expressions. The expressions are evaluated as either true or false.

5 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

6 Logic Operators ! – NOTChanges the truth value && - AndTrue if both values are true || - ORTrue when one of the values are true Precedence – Evaluate in the order of !, && then ||

7 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 = 90 – honor roll = 85 – Merit roll <85 – no certificate

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

9 if structure if (grade >= 95) keyword boolean condition The boolean condition must be placed inside parenthesis.

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

11 Using if…else if (grade >= 95) { System.out.println (“High Honor Roll”); } else { System.out.println (“Needs Improvement”); } grade = 98 Output: High Honor Roll grade = 88 Output: Needs Improvement

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

13 if structure if (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.

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

15 Grade output cont if(grade >= 95) { System.out.println (“High Honor Roll”);} if (grade = 90) {System.out.println (“Honor Roll”); } if (grade = 85) {System.out.println (“Merit Roll”);}

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

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

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

19 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 = 4 Output: 4 is even Even is awesome Bye num = 5 Output: 5 is odd Odd is weird Bye

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

21 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”); What would the output be for an even number? odd number? When you omit the braces, the if and else only take the 1 st 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.

22 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

23 Common Errors!! If(total >= 25); { } if(total = 10) { }

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

25 Alphabetical Order with Strings To determine alphabetical order use the compareTo method. String name = “Furman”; int greaterOrLess; greaterOrLess = name.compareTo(“Gilbert”) greaterOrLess is negative greaterOrLess = name.compareTo (“Dog”); greaterOrLess is positive greaterOrLess = name.compareTo(“Furman”); greaterOrLess is 0.


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

Similar presentations


Ads by Google