Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)

Similar presentations


Presentation on theme: "Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)"— Presentation transcript:

1 Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements) Guided Practice (1.3 Assignments) Closure Activity Students will be able to: know how to write an if statement use the "and" & "or" operators within an if statement See how today's lesson fits into the unit and the course as a whole

2 Warmup 67 % 10 = 67 / 10 = 3 / 5 = 3 / 5.0 = 5) int x = 7; int y = 2;
double z = (double)(x/y); // what does z equal now? 6) What is byte code? 7) What file extension does a source code file have? 8) Scanner sc = new Scanner(System.in); // what is sc called?

3 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!

4 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 <=

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

6 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’; }

7 You must put a variable before each <,>,==, <=, >=, etc…..
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)

8 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).

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

10 Demo: DemoIf

11 Java uses order of operations (PEMDAS), and modulus division is at the same level as multiplication and division. So, Java really uses PEMMDAS. Example: int x = / 5 * 3 – 10 % 6 * 2; what would this equal? = * 3 – 4 * 2 = – 8 = 0

12 Assignments (IfAndOr) (Numbers)
Prompt the user to enter his or her first name, then (separately) their last name. If the first OR last name is Joe, display “hi”. If the first name is Joe and the last name is Smith, display “bye.” **Test your program: only one of the above results should display, never both.** If the names are the same, display “same,” otherwise, “different.” If both names are “Sirhan,” display “Oh no.” (Numbers) Prompt the user to enter a test score between 0 and 100. Display what letter grade the score corresponds to. ( = A, 80-89 = B, etc) Display whether the score is even or odd. (Use a nested if) If it is even, display whether or not it is prime. Display whether or not the score is divisible by 7. If so, display whether or not it is a multiple of 14. If the score is 97, display “Yay” If the score is not between 0 and 100, display “invalid score”.


Download ppt "Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)"

Similar presentations


Ads by Google