Logical Operators and Switch

Logical Operators and Switch

Lecture 5 Relational and Equivalence Operators Relational SYS-1S22 / MTH-1A66 Operator Meaning < Less than > Greater than Logical Operators <= Less than or equal to and switch >= Greater than or equal to Equivalence Stuart Gibson Operator Meaning [email protected] == equal to != not equal to S01.09A • A variable may be tested against a constant 1 or another variable. 2 Examples of Simple Logical Expressions Evaluation of logical expressions int a=10, b=20, c=5; final int b=21; evaluates to evaluates to final char no = ‘n’; if( a < b ) 10 < 20 true { int a = 20; evaluates to evaluates to if ( a > c ) 10 > 5 true int c = 20; { char choice = ‘y’; System.out.println(“Second if”); } Expression Value System.out.println(“End of first if”); } a < b true else a == c true { c > b false System.out.println(“In else”); } choice == no false System.out.println(“After”); c != a false (Incorrect in Lecture 4 L) Output • In the last lecture we saw how these logical expressions are Second if Flow of program evaluated at run time as either true or false. End of first if execution. 3 After 4 1 Evaluation of logical expressions Evaluation of logical expressions int a=10, b=20, c=25; int a=25, b=20, c=25; evaluates to evaluates to evaluates to evaluates to if( a < b ) 10 < 20 true if( a < b ) 25 < 20 false { { evaluates to evaluates to if ( a > c ) 10 > 25 false if ( a > c ) Does NOT get { { evaluated System.out.println(“Second if”); System.out.println(“Second if”); as the first if } } statement System.out.println(“End of first if”); System.out.println(“End of first if”); evaluates to } } false. else else { { System.out.println(“In else”); System.out.println(“In else”); } } System.out.println(“After”); System.out.println(“After”); Output Output Flow of program Flow of program End of first if In else execution. execution. After After 5 6 Logical Operators The AND Operator ( && ) • Example: • The logical operators AND, OR and NOT can be used in Java to form more complex logical expressions by if ( x >= 0 && x <= 10) combining simple logical expressions. System.out.print(“x in range 0 to 10”); • An AND expression evaluates as true only if BOTH && AND Two Arguments arguments are true. || OR Two Arguments • Truth Table: ! NOT One Argument Argument 1 Argument 2 Argument 1 && Argument 2 true true true true false false false true false false false false 7 8 2 The AND Operator ( && ) - Examples The OR Operator ( || ) int x = 10, y = 11, z = -3; • Example: if (x > z && x > y) if ( x < 0 || x > 100) è(10 > -3 && 10 > 11) System.out.print(“x is invalid”); è(true && false) è false • An OR expression evaluates as true if EITHER arguments are evaluated as true. if (z < x && y > x) è(-3 < 10 && 11 > 10) • Truth Table: è(true && true) Argument 1 Argument 2 è true Argument 1 Argument 2 || true true true if (z > x && x > y) true false true è(-3 > 10 && 10 > 11) false true true è(false && false) è false false false false 9 10 The OR Operator ( || ) - Examples Evaluation of && and || Operator int x = 10, y = 11, z = -3; • The computer is quite clever, so when evaluating && and || operators in some cases it only evaluates the first argument. if( x > z || x > y ) For example: è(10 > -3 || 10 > 11) è(true || false) int a = 0; è true if (a > 0 && b / a > 1) if( z < x || y > x ) • In an AND (&&) statement, if the first argument is false, then è(-3 < 10 || 11 > 10) whatever the second argument is, doesn’t matter! This is è(true || true) because according to the truth table, any combination starting è true with false evaluates to false. if( z > x || x > y ) • In an OR (||) statement, if the first argument evaluates to è(-3 > 10 || 10 > 11) true, then whatever the second argument is, doesn’t matter! è(false || false) This is because and combination starting with true, evaluates è false to true. 11 12 3 The NOT Operator ( ! ) The NOT Operator ( ! ) - Examples • Example: int x = 10, y = 11, z = -3; if ( ! ( x == y ) ) { if( !( x > z )) System.out.print(“x is not equal to y”); è!(10 > -3) System.out.print(“easier to use x!=y in”); è!(true) System.out.print(“ this case.”); è false } • A NOT expression is true when its argument is false. if( !( z > y )) è!(-3 > 11) • Truth Table: è!(false) Argument ! Argument è true true false false true 13 14 Compound Logical Expressions Compound Logical Expressions • We can build more complex expressions containing more int x = 1, y = 2, z =3; than one logical operator. ( x == 1 || y == 2 ) && z == 4 int x = 1, y = 2, z =3; x == 1 || y == 2 && z == 4 • Now the expression in the parentheses is evaluated first: • Unless parentheses (brackets) are used AND is always ( x == 1 || y == 2 ) && z == 4 evaluated before OR so: ( true || true ) && z == 4 true && z == 4 x == 1 || y == 2 && z == 4 true && false x == 1 || true && false false x == 1 || false true || false • However the expression to evaluate can be even more true complicated such as: if ( !( x > y && y < z) && z > 0 || (y>=12)) But we could rewrite it using parentheses as: 15 16 4 Compound Logical Expressions Precedence Table if ( !( x > y && y < z) && z > 0 || (y>=12)) Operator Description Highest • Q: How does the computer know what to evaluate first ? ! + - Logical not, unary plus and minus A: Precedence ! * / % Multiplication, division and mod • Brackets have the highest precedence. Always solve the Addition and subtraction deepest nested set first (L:N where N is the level of nesting): + - < <= >= > Relational operators if ((x >5 && (y < z || y > 3)) && (a > 3)) == != Equivalence operators L:3 L:3 && Logical AND L:2 L:2 L:2 L:2 L:1 L:1 || Logical OR Assignment • So, solve level 3 (deepest nested) first, then level2, note ties = Lowest on the same level are solved left to right and finally level 1. 17 18 More Logical Expressions Character Comparisons • You can compare characters as well: • Logical expression can involve arithmetic operators: Expression Value if ((x = y + 5) > 10 && (z = z + y) == 4)) System.out.print(“Some condition met”); ‘A’ < ‘B’ true ‘A’ < ‘a’ true • But it is much better to write: ‘?’ > ‘@’ false x = y + 5; • Q: Where does those values come from ? z = z + y; A: The computer converts the characters to numbers and then compares those numbers. if (x > 10 && z == 4) System.out.print(“Some condition met”) • The numbers come from the ASCII / UNICODE values: A = 65, B = 66, a = 97, ? = 63, @ = 64. 19 20 5 Control Structures Selection: switch • In the last lecture we saw that there were three main types of • switch statements can be used to chose between one of Control Structures: several alternatives. • Sequence • The alternatives are dependant on the value of a selector • Selection • Repetition • The selector must be an expression that evaluates to an • Selection integer (int), character (char), or boolean. So far we have seen the following Java control sequences: • Note that with boolean selector you can only have two choices, true or false, like a two alternative, if else One Alternative: if statement statement. Two Alternatives: if else statement More than two Alternatives: nested if else statements 21 22 switch statement switch statement switch( selector ) • switch statements provide an alternative syntax to multiple { if statements. Consider the following nested if: case label1: statements1; break; char staffType; … … … if( staffType == ‘A’) case label2: System.out.print(“Academic Staff”); statements2; break; else if( staffType == ‘U’ ) . System.out.print(“Undergraduate Student”); . else if( staffType == ‘P’ ) System.out.print(“Postgraduate Student”); else if( staffType == ‘T’ ) case labeln: System.out.print(“Technical Staff”); statementsn; break; else System.out.print(“Unknown Staff Type”); default: statementsd; } 23 24 6 switch statement Execution of a switch statement char staffType; • The selector expression is evaluated and compared to each … … … case label in turn, for example: switch (staffType) { case ‘A’: staffType = ‘P’; System.out.print(“Academic Staff”); break; case ‘A’: à staffType == ‘A’ à ‘P’ == ‘A’ à false case ‘U’: case ‘U’: à staffType == ‘U’ à ‘P’ == ‘U’ à false System.out.print(“Undergraduate Student”); case ‘P’: à staffType == ‘P’ à ‘P’ == ‘P’ à true break; case ‘P’: System.out.print(“Postgraduate Student”); • If the selector is equal to the condition of one of the case break; labels, then the statements associated to this case are case ‘T’: executed, so in our example the output would be: System.out.print(“Technical Staff”); break; default : Postgraduate Student System.out.print(“Unknown Staff Type”); } 25 26 switch – the use of break switch – with break • All statements after the matched label are executed until char staffType; Output case staffType = ‘P’; Postgraduate Student a break statement is reached. switch (staffType) After Switch { • The purpose of the break statement is to break out of the case ‘A’: false System.out.print(“Academic Staff”); current control structure, in this case the switch statement. break; case ‘U’: false • In our example where the program System.out.print(“Undergraduate Student”); staffType = ‘P’; break; executes the one line of code associated with the case ‘P’: case ‘P’: and then breaks out of the switch. Program execution true System.out.print(“Postgraduate Student”); continues at the next statement after the last curly brace of the break; case ‘T’: switch statement. At the break System.out.print(“Technical Staff”); execution break; jumps to after default : the switch System.out.print(“Unknown Staff Type”); } System.out.println(“After Switch”); 27 Flow of execution 28 7 switch – missing break switch – default

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us