Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Statements

Similar presentations


Presentation on theme: "Conditional Statements"— Presentation transcript:

1 Conditional Statements
Module 3 Conditional Statements

2 Conditional Statements
allow you to do different things in different situations. Imagine driving a car. You see a stoplight ahead. What you do next is dependent on the state of the stoplight. IF the stoplight is green, THEN you continue. IF the stoplight is yellow, THEN you prepare to stop. IF the stoplight is red, THEN you stop.

3 Or what about age and voting?
What do we want to do? 1) Ask the user for their age. 2) IF they are at least 18 years old, THEN tell them that they can vote. 3) ELSE inform them that they are too young to vote We can use an if/else statement to do this in Java!

4 Conditional Statement
If the conditional statement is false, everything after the else is executed. If the conditional statement is true, everything within the first set of curly braces is executed. If/else statement Scanner kb = new Scanner(System.in); System.out.print(“How old are you? “); int age = kb.nextInt(); if(age >= 18) { System.out.println(“You can vote!”); } else System.out.println(“Sorry, can’t vote yet.”); Conditional Statement

5 Conditional Statements
are statements that evaluate to true or false. Conditional statements often use an equality operator: Operator Meaning == Equal (only for primitive data) != Not equal > Greater than >= Greater than or equal to < Less than <= Less than or equal to

6 Now your turn… Ask the user for two numbers.
If the numbers are the same, output “Same” Otherwise output “Different”

7 Now your turn…

8 Let’s do another one Ask the user for a String.
If the String starts with the letter ‘a’, output “A is for Apple!” Otherwise, output “Nevermind.”

9 Notice that this is giving us a char, not a String.
Let’s do another one Notice that this is giving us a char, not a String.

10 Classes are different if(x == 5) if(s.equals(“hello”))
If you want to check if two numbers are equal, or two chars are equal, use == If you want to check if two Strings (or any other type of classes) are equal, use .equals if(x == 5) if(s.equals(“hello”)) You MUST use .equals for ALL class types! This is extra tricky because if(s == “hello”) will compile just fine. It just won’t work properly!

11 What if our situation is a bit more complicated?
Ask the user for their age. Describe them as a child, teenager, or adult depending on their answer. Give it a try! Hint – you can nest if/else statements

12 What if our situation is a bit more complicated?
Notice we break the possible options into two. Then we do it again. If statements always break the world into two parts.

13 When you find yourself nesting if/else statements, there’s a better way
We can use an “else if” to restructure our code: Notice the final else has no parentheses after it. We can have as many options as we want using “else if”

14 What would be the output of the following code?
AB BC AC ABC Socrative

15 What would be the output of the following code?
If/else statements do not “fall through”. We stop at the first section whose condition is met. A B C AB BC AC ABC Socrative

16 Use .equalsIgnoreCase to ignore the case of the order
Your turn… Use .equalsIgnoreCase to ignore the case of the order You’ve been hired by Starbucks to put together a new automated computer system. Ask the user if they would like an ice tea, latte, or cappuccino If they chose ice tea, tell them they owe $3.50 If they chose latte, tell them they owe $5.99 If they chose cappuccino, tell them they owe $4.75 If they chose something else, tell them that the barista will be right there to help them

17 Your turn… If you have only a single line in your if/else statement, you can skip the curly braces!

18 What is the output of the following code?
B C AB BC AC ABC Socrative

19 What is the output of the following code?
Notice that “else” is optional. A B C AB BC AC ABC Without curly braces, only the first line is in the if-statement. The computer ignores indentation. Good indentation is just for people reading your code. Socrative

20 Both sides of the expression must be true for the AND to be true.
Boolean Expressions We can use boolean operators to make more complex boolean expressions. Operator Name Example of use && And x > 7 && x < 17.5 x 7 17.5

21 Either side of the expression can be true for the OR to be true.
Boolean Expressions We can use boolean operators to make more complex boolean expressions. Operator Name Example of use && And || Or x > 7 && x < 17.5 x < 5 || x > 15 x 5 15

22 NOT negates whatever the value is to make it the opposite.
Boolean Expressions We can use boolean operators to make more complex boolean expressions. Operator Name Example of use && And || Or ! Not x > 7 && x < 17.5 x < 5 || x > 15 !(x > 3) x 3

23 De Morgan’s Laws x (x > 5 && x < 10)
NOT does funny things with AND and OR. If we negate this… (x > 5 && x < 10) x 5 10

24 We suddenly take the opposite
De Morgan’s Laws NOT does funny things with AND and OR. We suddenly take the opposite Which is equal to… !(x > 5 && x < 10) x 5 10

25 Likewise, !(x < 3 || x > 8) = !(x < 3) && !(x > 8)
De Morgan’s Laws NOT does funny things with AND and OR. !(x > 5 && x < 10) !(x > 5) || !(x < 10) x 5 10 Likewise, !(x < 3 || x > 8) = !(x < 3) && !(x > 8)

26 Operator Precedence Operator Precedence Postfix expr++, expr-- Unary
!, +, - Multiplicative *, /, % Additive +, - Relational >, >=, <, <= Equality ==, != Logical AND && Logical OR ||

27 Let’s play with this a little
Ask the user for a number x If x > 7 AND x < 17.5 display “Nailed it!” Otherwise display “Too bad.”

28 Let’s play with this a little
Ask the user for TWO numbers x and y If y is even, and x is between 1 and 10 inclusive, display “In Range” If y is odd, and x is less than 1 or greater than 10, display “In Range” Otherwise display “Out of Range” In your code, try to only write the words “In Range” ONE TIME. Use boolean operators to make this possible.

29 Let’s play with this a little
Notice how we use parentheses to specify the precedence.

30 What does this evaluate to?
Suppose x = 9 and y = 4 True False Socrative

31 What does this evaluate to?
Suppose x = 9 and y = 4 True False Socrative

32 What does this evaluate to?
Suppose x = 3 and y = 7 True False Socrative

33 What does this evaluate to?
Suppose x = 3 and y = 7 True False Socrative

34 Another one Ask the user for three numbers: x, y, and z
If x <= y <= z, display “In order!” Otherwise, display “Out of order” Notice that both sides of the boolean operator MUST BE complete boolean expressions!

35 Project Working in teams of up to size three:
Program a Rock/Paper/Scissors game The computer should choose a random number between 0 and 2. Each number should correspond to rock, paper, or scissors. The user needs to choose rock, paper, or scissors. Compare the computer’s choice with the users choice and announce who won. Points will be given for style and readability

36 If expression matches value2, then control jumps here.
Switch Statements Sometimes when you have a number of different cases, a switch statement is easier than if/else switch(expression) { case value1: statement-list1 break; case value2: statement-list2 } If expression matches value2, then control jumps here.

37 Let’s do an example Ask the user for an integer to represent the month
Use a switch statement to tell them what month corresponds with the number they chose.

38 If you forget the break statement, control falls through to the next case.
If a number other than 1-12 is used, control gets sent to the default case.

39 What’s the output of the following code?
B C D Something else Socrative

40 What’s the output of the following code?
B C D Something else Since there are no break statements, control will fall through and this will output BCD Socrative


Download ppt "Conditional Statements"

Similar presentations


Ads by Google