Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved."— Presentation transcript:

1 Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

2 As in most programming languages, flow of control in Java refers to its branching and looping mechanisms Flow of Control Branching Switch caseifIf… else Looping ForwhileDo.. while

3 د / احمد يوسف 3 Control Structures

4 4 Branching Statements – Simple if statement – if-else statement – Nested if statements – switch statement

5 5

6 Java Comparison Operators 3-6 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

7 د / احمد يوسف 7 Simple if Statement Syntax: if (expression) statement ; if (hour < 12) System.out.println("Good morning.");

8 د / احمد يوسف Example: Simple if Statement import java.util.Scanner; public class Result{ public static void main(String[] args){ Scanner keyboard=new Scanner(System.in); int grade= keyboard.nextInt(); if ( grade > = 60 ) System.out.println ( " passed " ); }

9 9 Example: Simple if Statement Write a Java program prints the absolute value of a number. import java.util.Scanner; class absolute { public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(" Enter a number: "); int x = kb.nextInt(); int y = x; if( y < 0) y = -y; System.out.print(" The absolute value of " + x + " is " + y); }

10 د / احمد يوسف 10 Branching with an if-else Statement Two-Way Selection Syntax: if (expression) statement1; else statement2; The Expression must be enclosed in parentheses If the Expression is true, then the Statement1 is executed If the Expression is false, then the Statement2 is executed

11 Example: Even/Odd number import java.util.Scanner; class EvenOdd{ public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(" Enter a number: "); int num= kb.nextInt(); if(num %2 = = 0) System.out.println ( " The Number is Even" ); else System.out.println ( "The Number is Odd" ); } 3-11

12 د / احمد يوسف 12 if ( grade > = 60 ) System.out.println ( " passed " ); else System.out.println ( " failed " );

13 13 Compound (Block of) Statements Syntax: { statement1 statement2. statementn }

14 Compound Statements You have to use braces if the or block has multiple statements. if only one statement is there braces are optional but it is advisable to always use them to enhance readability if (testScore < 60) { System.out.println("You did not pass"); System.out.println("Try harder next time"); } else { System.out.println("You did pass"); System.out.println("Keep up the good work"); } Then Block Else Block

15 Multiway if-else Statement if (Boolean_Expression) Statement_1 else if (Boolean_Expression) Statement_2 else if (Boolean_Expression_n) Statement_n else Statement_For_All_Other_Possibilities Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

16 if - else- if if (score >= 90) { System.out.println("Grade is A"); } else if (score >= 80) { System.out.println("Grade is B"); } else if (score >= 70) { System.out.println("Grade is C"); } else if (score >= 60) { System.out.println("Grade is D"); } else { System.out.println("Grade is F"); } Test ScoreGrade 90  score A 80  score  90 B 70  score  80 C 60  score  70 D score  60 F

17 The switch Statement The switch statement is multiway branching – When a switch statement is evaluated, one of a number of different branches is executed – The choice of which branch to execute is determined by a controlling expression enclosed in parentheses after the keyword switch The controlling expression must evaluate to a char, int, short, or byte 3-17 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

18 The switch Statement Each branch statement in a switch statement starts with the reserved word case, followed by a constant called a case label, followed by a colon, and then a sequence of statements – Each case label must be of the same type as the controlling expression – Each sequence of statements may be followed by a break statement ( break; ) 3-18 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

19 د / احمد يوسف 19 switch Structures

20 د / احمد يوسف 20 switch (expression) { case value 1: statements 1; break; case value 2: statements 2; break;... case value n: statements n ; break; default: statements }

21 د / احمد يوسف 21 switch With No break Statements x = 10; false true ch == 'a' ? x = 20; x = 30; ch == 'b' ? ch == 'c' ? false true public class SwitchNoBreak { public static void main( String s[] ) { char ch = 'a'; int x = 0; switch ( ch ) { case 'a': x = 10; case 'b': x = 20; case 'c': x = 30; } System.out.println( "x = " + x ); } x = 30

22 د / احمد يوسف 22 switch With break Statements x = 10; false true ch == 'a' ? x = 20; x = 30; ch == 'b' ? ch == 'c' ? false true break; public class SwitchWithBreak { public static void main(String s[]) { char ch = 'a'; int x = 0; switch ( ch ) { case 'a': x = 10; break; case 'b': x = 20; break; case 'c': x = 30; break; } System.out.println( "x = " + x ); } x = 10

23 د / احمد يوسف 23 import java.util.Scanner; class Arithmatic{ public static void main(String [ ] args){ Scanner keyboard=new Scanner(System.in); System.out.print("Enter First number"); int x= keyboard.nextInt(); System.out.print("Enter Second number“); int y= keyboard.nextInt(); System.out.print("Enter 1:Add\n 2:Sub\n 3:Mul.\n 4: Div.“); int menu = keyboard.nextInt(); switch (menu) { case 1 : System.out.println(“x+y = " + (x+y) ); break ; case 2 : System.out.println(“ x – y =" + (x - y)) ; break ; case 3 : System.out.println("x * y ="+ (x * y)) ; break ; case 4 : System.out.println("x / y ="+ (x / y)) ; break ; }

24 The Conditional Operator The conditional operator is a notational variant on certain forms of the if-else statement – Also called the ternary operator or arithmetic if – The following examples are equivalent: if (n1 > n2) max = n1; else max = n2; vs. max = (n1 > n2) ? n1 : n2; – If the Boolean expression is true, then the expression evaluates to the value of the first expression ( n1 ), otherwise it evaluates to the value of the second expression ( n2 ) 3-24 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

25 Pitfall: Using == with Strings The equality comparison operator ( == ) can correctly test two values of a primitive type In order to test two strings to see if they have equal values, use the method equals, or equalsIgnoreCase string1.equals(string2) string1.equalsIgnoreCase(string2) 3-25 Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Confusion between =, ==, and equals method is one of the most frequent programming errors

26 Building Boolean Expressions When two Boolean expressions are combined using the "and" ( && ) operator, the entire expression is true provided both expressions are true – Otherwise the expression is false When two Boolean expressions are combined using the "or" ( || ) operator, the entire expression is true as long as one of the expressions is true – The expression is false only if both expressions are false Any Boolean expression can be negated using the ! operator – Place the expression in parentheses and place the ! operator in front of it Unlike mathematical notation, strings of inequalities must be joined by && – Use (min < result) && (result < max) rather than min < result < max 3-26 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

27 Truth Tables 3-27 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.


Download ppt "Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved."

Similar presentations


Ads by Google