1 / 9

If Statements

If Statements . If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on the If or Else lines!. Comparing variables.

jaeger
Download Presentation

If Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. If Statements • If statements: allow the computer to make a decision • Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on the If or Else lines!

  2. Comparing variables • When comparing variables (except Strings), use the double-equals ( = = ), NOT the equals sign! example: if (GPA = = 3.2)… • “Does not equals:” ! = • Greater than > • Greater than or equal to >= • Less than < • Less than or equal to <=

  3. If-else if • If there are more than 2 possibilities, you must use the “else if” • Your final possibility should be the only “else” • Syntax: if (condition_1) { Statement_1; } else if (condition_2) { Statement_2; } else { Statement_3; }

  4. And vs. Or • “and”  && (Statements will execute only if both conditions are true) • “or”  | | (Statements will execute if one condition is true) if ( score >= 80 && score < 90 ) { grade = ‘B’; }

  5. A common mistake: If (score >= 80 && < 90) • You must put a variable before each <,>,==, <=, >=, etc….. • So the correct code would be: if (score >= 80 && score < 90)

  6. When an and statement is false… • Very important: when && is used, once the compiler finds a part of the condition that is false, it ignores the rest of the condition, as well as whatever is inside the attached if-statement. • Example: int x = 0; int y = 0; if (x = = 1 && y = = 0) { System.out.println(“Hi”); } • In this example, once the compiler sees that x does not equal 1, it ignores everything that follows the &&, including y = = 0. • If this had been an || instead of a &&, then the condition would be true, and the if-statement would execute (“Hi” would be displayed).

  7. When comparing Strings in an if statement, you DO NOT use = = • Instead, you must use .equals(“ “) or .equalsIgnoreCase(“ “) String dog = “Fido”; if (dog.equalsIgnoreCase(“fido”)) // true if (dog.equals(“fido”)) // false ***to use “does not equal” with a String: if (!(dog.equalsIgnoreCase(“penny”)))

  8. Demo: IfDemo

  9. Assignments • (Walter) Prompt the user to enter his or her first name, then (separately) their last name. • If the first OR last name is Walter, give all praise to the user. • If the first name is Walter and the last name is White, display “Heisenberg.” • **Only one of the above results should display, not both.** 2. (Numbahz) Prompt the user to enter a test score between 0 and 100. • Display what letter grade the score corresponds to. • Display whether the score is even or odd. • Display whether or not the score is divisible by 7. • If the score is 97, display “Yay” • If the score is not between 0 and 100, display “error”

More Related