numeric rela5onal operators the if statement
play

Numeric Rela5onal Operators The if Statement The if - PDF document

if Statement Ensures that a statement is executed only when some Flow of Control: Branching condi5on is true (Savitch, Chapter 3) Condi5ons typically involve comparison


  1. if ¡Statement ¡ • Ensures ¡that ¡a ¡statement ¡is ¡executed ¡only ¡when ¡some ¡ Flow of Control: Branching condi5on ¡is ¡ true (Savitch, Chapter 3) • Condi5ons ¡typically ¡involve ¡comparison ¡of ¡variables ¡or ¡ quan55es ¡for ¡equality ¡or ¡inequality ¡ TOPICS • Example: ¡ • Conditional Execution Expression in parenthesis ¡ ¡ ¡ must evaluate to either • if and else Statement true or false if (age >= 18) • Boolean Data • switch Statement System.out.println( “ You are eligible to vote. ” ); ¡ ¡ ¡ CS 160, Fall Semester 2012 1 CS 160, Fall Semester 2012 2 Numeric ¡Rela5onal ¡Operators ¡ The ¡ if ¡Statement ¡ • The ¡ if ¡statement ¡has ¡the ¡following ¡syntax ¡ Math Java description The condition must be a boolean expression. It must if is a Java < < Less than evaluate to either true or false. reserved word > > Greater than if ( condition ) ≤ <= Less than or equal to statement ; ≥ >= Greater than or equal to = == Equal to If the condition is true, the statement is executed. If it is false, the statement is skipped. ≠ != Not equal to 3 CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 4 1

  2. if ¡Statement ¡with ¡ else ¡ Defining ¡Blocks ¡ • To ¡execute ¡more ¡than ¡one ¡statement ¡condi5onally, ¡use ¡ { ¡} ¡to ¡ • An ¡ if ¡statement ¡may ¡have ¡an ¡op5onal ¡ else ¡ define ¡a ¡block ¡(aka ¡ “ compound ¡statement ” ) ¡for ¡the ¡sequence ¡ clause ¡that ¡will ¡only ¡be ¡executed ¡when ¡the ¡ of ¡statements ¡ • Example: ¡ condi5on ¡is ¡false ¡ Give an example ¡ if (firstNumber <= secondNumber) of when BOTH • Example: ¡ { statements will quotient = secondNumber / firstNumber; execute? ¡ ¡ if ( wages <= 57600 ) remainder = secondNumber % firstNumber; NONE! NONE! tax = 0.124 * wages; } One or the other One or the other else else must execute must execute { tax = 0.124 * 57600; quotient = firstNumber / secondNumber; Give an example remainder = firstNumber % secondNumber; of when NEITHER } statement will execute? CS 160, Fall Semester 2012 5 CS 160, Fall Semester 2012 6 Cascading ¡if-­‑else ¡Statements ¡ Dangling ¡else ¡ • Example: ¡ • Code ¡wriKen: ¡ • Which ¡ if ¡does ¡the ¡ else ¡finish? ¡ ¡ ¡ ¡ ¡ ¡ ¡ if ( condition1 ) ¡ ¡ ¡ ¡ if ( condition1 ) statement1; if ( condition2 ) statement1; else else will else if ( condition2 ) match to the statement2; ¡ nearest statement2; Be sure to use unmatched if indentation within the same else properly block statement3; ¡ Otherwise too difficult to read! CS 160, Fall Semester 2012 7 CS 160, Fall Semester 2012 8 2

  3. boolean ¡Data ¡Type ¡ Fix ¡dangling ¡else-­‑ ¡Use ¡Blocks ¡to ¡Specify ¡ • Code ¡wriKen: ¡ • boolean ¡ if ( condition1 ) • A ¡primi5ve ¡data ¡type ¡that ¡can ¡be ¡set ¡to: ¡ { – true Notice there are no if ( condition2 ) – false quotation marks around true and statement1; • Example: false ! } boolean correct = true; else statement2; CS 160, Fall Semester 2012 9 CS 160, Fall Semester 2012 10 Boolean ¡Expressions ¡ boolean ¡Operators ¡ ¡ • Condi5ons ¡are ¡expressions ¡that ¡have ¡a ¡truth ¡ • Logical ¡ “ and ” ¡(conjunc5on) ¡ ¡ value. ¡ – Java ¡symbol ¡ && ¡ – Math ¡symbol ¡ ∧ ¡ • Arithme5c ¡rela5onal ¡operators ¡produce ¡a ¡ – true ¡only ¡when ¡both ¡expressions ¡are ¡true ¡ truth ¡value, ¡e.g., ¡ (MINIMUM_WAGE <= wages) && (wages <= MAXIMUM_WAGE) – 10 < 3 • Logical ¡inclusive ¡ “ or ” ¡(disjunc5on) ¡ – x > y – Java ¡symbol ¡ || ¡ – Math ¡symbol ¡ ∨ ¡ – a >= (b + 12) – true ¡when ¡either ¡or ¡both ¡expressions ¡are ¡true ¡ (wages < MINIMUM_WAGE ) || (wages > MAXIMUM_WAGE ) CS 160, Fall Semester 2012 11 CS 160, Fall Semester 2012 12 3

  4. Java ¡Logical ¡and ¡Arithme5c ¡ ¡ boolean ¡Operators ¡(cont.) ¡ ¡ Operator ¡Precedence ¡Rules ¡ • Logical ¡ “ exclusive ¡or ” ¡ 1. ¡ ¡ ! ¡ -­‑ ¡(unary) ¡ – Java ¡symbol ¡ ¡ ¡ ^ ¡ 2. ¡ ¡ * ¡ / ¡ % ¡ – Math ¡symbol ¡ ¡ ¡ ⊕ ¡ 3. ¡ ¡ + ¡ -­‑ ¡ – true ¡when ¡exactly ¡one ¡of ¡the ¡expressions ¡is ¡true ¡ 4. ¡ ¡ < ¡ <= ¡ > ¡ >= ¡ (MINIMUM_WAGE < wages) ^ (MINIMUM_WAGE == wages) 5. ¡ ¡ == ¡ != ¡ • Logical ¡ “ not ” ¡(nega5on) ¡ 6. ¡ ¡ ^ ¡ ¡ & ¡ | ¡ – Java ¡symbol ¡ ¡ ! 7. ¡ ¡ && ¡ – Math ¡symbol ¡ ¡ ¬ ¡ 8. ¡ ¡ || ¡ !(MINIMUM_WAGE == wages) CS 160, Fall Semester 2012 13 CS 160, Fall Semester 2012 14 Combining ¡Rela5onal ¡Operators ¡ Complicated ¡Boolean ¡Expressions ¡ • Unlike ¡some ¡other ¡operators, ¡rela5onals ¡cannot ¡ boolean isLeapYear = ((year % 4) == 0) be ¡combined ¡in ¡Java. ¡ && ((year % 100) != 0) • Example: ¡ || ((year % 400) == 0); (a <= b <= c) Interpreta5on: ¡ – Does ¡not ¡mean ¡a ¡<= ¡b ¡and ¡b ¡<= ¡c. ¡ • Leap ¡years ¡are ¡every ¡four ¡years ¡(divisible ¡by ¡4) ¡ – It ¡produces ¡a ¡compile-­‑5me ¡error ¡-­‑-­‑ ¡cannot ¡compare ¡a ¡ except ¡for ¡centuries ¡that ¡are ¡not ¡divisible ¡by ¡400. boolean ¡(return ¡value ¡of ¡<= ¡operator) ¡with ¡a ¡number. ¡ ¡ – How ¡should ¡this ¡be ¡done? ¡ ( a <= b && b <= c) CS 160, Fall Semester 2012 15 CS 160, Fall Semester 2012 16 4

  5. switch ¡Statement ¡ Why ¡is ¡break ¡used ¡in ¡switch ¡statements? ¡ Consider ¡the ¡code ¡fragment ¡below ¡ • Used ¡to ¡accomplish ¡mul5-­‑way ¡branching ¡based ¡on ¡the ¡value ¡ • of ¡an ¡integer ¡selector ¡variable ¡ ¡ ¡ ¡ ¡ ¡ ¡ int i = 1; • Example: ¡ switch (i) Expression in ( ) { switch (numberOfPassengers) must evaluate to an case 0: System.out.println( “ 0 ” ); int or char ONLY! { case 1: System.out.println( “ 1 ” ); case 0: System.out.println( “ The Harley ” ); case 2: System.out.println( “ 2 ” ); case 3: System.out.println( “ 3 ” ); break; } case 1: System.out.println( “ The Dune Buggy ” ); break moves System.out.println( ); break; 1 flow of control to 2 default: end of switch • Without ¡breaks ¡what ¡is ¡the ¡output? ¡ 3 statement System.out.println( “ The Humvee ” ); ¡ } (note: ¡it ¡is ¡legal ¡to ¡leave ¡out ¡the ¡ default case is executed if breaks ¡and ¡some6mes ¡desired) ¡ Don ’ t need a break after no other case values match default – already at end CS 160, Fall Semester 2012 expression 17 CS 160, Fall Semester 2012 18 Why ¡execute ¡mul5ple ¡cases? ¡ Symbolic ¡Constants ¡in ¡switch ¡Statements ¡ • Consider ¡if ¡you ¡want ¡a ¡base ¡level ¡with ¡add-­‑ons ¡for ¡increasing ¡ final int numbers ¡as ¡in… ¡ SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4, THURSDAY = 5, FRIDAY = 6, SATURDAY = 7; switch (zoomember_level) int d; { ... case 500: System.out.print( “ Meet a tiger ” ); switch (d) { case 100: System.out.print( “ Free t-shirt ” ); case SUNDAY: System.out.print( “ Sunday ” ); break; case 50: System.out.print( “ Free admission! ” ); case MONDAY: System.out.print( “ Monday ” ); break; default: System.out.println(); } case TUESDAY: System.out.print( “ Tuesday ” ); break; case WEDNESDAY: System.out.print( “ Wednesday ” ); break; • Example ¡of ¡when ¡we ¡want ¡to ¡leave ¡off ¡the ¡break ¡statements ¡ case THURSDAY: System.out.print( “ Thursday ” ); break; to ¡allow ¡execu5on ¡to ¡follow ¡through ¡ case FRIDAY: System.out.print( “ Friday ” ); break; case SATURDAY: System.out.print( “ Ski day ” ); break; } CS 160, Fall Semester 2012 19 CS 160, Fall Semester 2012 20 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend