Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Flow of Control As in most programming languages, flow of control in Java refers to its branching and looping mechanisms Flow of Control Branching Switch case if Looping If… else For while Do.. while Control Structures 3 احمد يوسف/د Branching Statements – Simple if statement – if-else statement – Nested if statements – switch statement 4 5 Java Comparison Operators Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-6 Simple if Statement Syntax: if (expression) statement ; if (hour < 12) System.out.println("Good morning."); 7 احمد يوسف/د 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 " ); } } احمد يوسف/د 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); } } 9 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 10 احمد يوسف/د 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 if ( grade > = 60 ) System.out.println (" passed " ); else System.out.println (" failed " ); 12 احمد يوسف/د Compound (Block of) Statements Syntax: { statement1 statement2 . . . statementn } 13 Compound Statements • • You have to use braces if the <then> or <else> 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"); Then Block } else { System.out.println("You did pass"); System.out.println("Keep up the good work"); } Else Block 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. 3-15 if - else- if if (score >= 90) { Test Score System.out.println("Grade is A"); 90 score } else if (score >= 80) { 80 score 90 System.out.println("Grade is B"); 70 score 80 } 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"); } 60 score 70 score 60 Grade A B C D F 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 Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-17 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;) Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-18 switch Structures احمد يوسف/د 19 switch (expression) { case value 1: statements 1; break; case value 2: statements 2; break; ... case value n: statements n ; break; default: statements } احمد يوسف/د 20 switch With No break Statements public class SwitchNoBreak { public static void main( String s[] ) { char ch = 'a'; int x = 0; ch == 'a' ? switch ( ch ) { false case 'a': x = 10; ch == case 'b': 'b' ? x = 20; case 'c': false x = 30; ch == } 'c' ? System.out.println( "x = " + x ); } true x = 10; true x = 20; true x = 30; false } x = 30 احمد يوسف/د 21 switch With break Statements 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 ch == 'a' ? false ch == 'b' ? true x = 10; break; true x = 20; false ch == 'c' ? false break; true x = 30; break; ); } } x = 10 احمد يوسف/د 22 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 ; } } } 23 احمد يوسف/د 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) Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-24 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) Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-25 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 Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-26 Truth Tables Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 3-27