Upload
chelsea-celestino
View
217
Download
0
Embed Size (px)
Citation preview
8/13/2019 Major Topic 2 - Condition
1/29
My program is smarter than
a 5
th
grader!By: Chelsea Celestino
PROVE IT!
8/13/2019 Major Topic 2 - Condition
2/29
We have learned about variables, expressions, getting aninput and display an output! Weve been computing a lot of
things this past week right?
So far
I can do thattoo! Pssh,
youre not sospecial!
Mr. Calculator
.
INTRPRG AY 2011-2012 1st Term
8/13/2019 Major Topic 2 - Condition
3/29
8/13/2019 Major Topic 2 - Condition
4/29
We need to make our programs think and make a DECISION!
Bring out your wands and say ----- No, no, not that.
We need to make them a bit smarter!
Step
1
INPUT
Statement1
Statement2
Our program needs to be
able to do different thingsbased on an input!
Example :If Jim got the right answer he gets 1 point.
Ifthe file isnt there, display an error
message.
INTRPRG AY 2011-2012 1st Term
8/13/2019 Major Topic 2 - Condition
5/29
They are able to perform a CHECK (by doing a test). Theycheck if a certain condition is TRUE or NOT
Thankfully, computers do have some
sense in them!
When Java performs a CHECK,there are only two possible answers:pass or fail TRUEorFALSE.
INTRPRG AY 2011-2012 1st Term
8/13/2019 Major Topic 2 - Condition
6/29
To figure out if Jim got the correct answer, weshould know the correct answer . We should
also know what Jims answer was so we canprobably say something like:
1. Are two things equal?
2. Is something greater than another?
3. Is something less than another?
How does Java check?
Write your conditions in a way Java can understand!
If Jims answer
is equal to correct answer
INTRPRG AY 2011-2012 1st Term
8/13/2019 Major Topic 2 - Condition
7/29
Whats the difference between the two?
Two plus three is five. STATEMENT
Is two plus three five? QUESTION
Lesson 1 Formulating your conditionIf Jims answer is equalto correct answer
Whats up with that?
X = 2 + 3
if (X == 2 + 3)
=
Not Equal To
Greater than
Greater thanor equal to
Less than
Less than orequal to
INTRPRG AY 2011-2012 1st Term
8/13/2019 Major Topic 2 - Condition
8/29
Pay attention to the syntax!Look at where your semi-colonshould be placed!
Try the following code:
Flex your fingers
INTRPRG AY 2011-2012 1st Term
Sample Input:
23 and 32,
100 and 50,
0 and 0,
-23, -27
8/13/2019 Major Topic 2 - Condition
9/29
Lets look at the anatomy:Enter: if Statement
boolean expression
Sequence ofstatements to executeif the condition is true
INTRPRG AY 2011-2012 1st Term
Or The End?End the program
Move on?with the remainingstatements?
8/13/2019 Major Topic 2 - Condition
10/29
What if we remove the elsethen? How is it different?
Open GuessTheNumber.java
Remember our guessing game?
INTRPRG AY 2011-2012 1st Term
Do this if conditionis true!
But if NOT, do thisinstead! (ONLY do this if
condition is FALSE)
Do this REGARDLESSif the condition is true
or not!!
Enter: ELSE
8/13/2019 Major Topic 2 - Condition
11/29
if (x < y)
int t = x;
x = y;
y = t;
System.out.println (Done!);
You mean like this?
INTRPRG AY 2011-2012 1st Term
Can I have more than one statement
under an ifor an else?
But, HOW do you tell Java which lines to
include under an if statement?
8/13/2019 Major Topic 2 - Condition
12/29
Lets say the user inputs 15 for both x and y.Any difference?
Is the curly braces always required?
Try the two codes below:
INTRPRG AY 2011-2012 1st Term
Defining a block of code
if (x < y)
x = x + 5;
y = y 5;
System.out.println(x + + y);
if (x < y){
x = x + 5;
y = y 5;
}
System.out.println(x + + y);
Yhasavalueof10infirstcodewhileithasavalueof15inthesecondcode!
8/13/2019 Major Topic 2 - Condition
13/29
What if all else fails?!
No problem, Java can handle that!
INTRPRG AY 2011-2012 1st Term
What if I want to execute my elsebased
only on a certain condition also?
if (grade >= 15)
System.out.println(Awesome!);
else if (grade >= 10)
System.out.println(Pretty good!);
else if (grade >= 5)
System.out.println(What happened!);
else
System.out.println(Dont worry
about it!);
Simply add an else
at the end!
8/13/2019 Major Topic 2 - Condition
14/29
She means this one:
INTRPRG AY 2011-2012 1st Term
How is that different from having several
ifsinstead? Whats thebig diff
huh?
if (grade >= 15)
System.out.println(Awesome!);
if (grade >= 10)
System.out.println(Pretty good!);
if (grade >= 5)
System.out.println(What
happened!);
8/13/2019 Major Topic 2 - Condition
15/29
Input Output using Else If Output using Many Ifs
8 What happened? What happened?
14 Pretty Good Pretty Good
What happened?17 Awesome Awesome
Pretty GoodWhat happened?
Think it doesnt matter?
Think again.
INTRPRG AY 2011-2012 1st Term
The mystery of Else Ifs and Many Ifs
Now, think some more.What really happened just now?
Dont want!My brainstired!
8/13/2019 Major Topic 2 - Condition
16/29
What really is happening
INTRPRG AY 2011-2012 1st Term
How Java interprets the two codes
if (grade >= 15)
if (grade >= 10)
if (grade >= 5)
if (grade >= 15)
else If (grade >=
10)
if (grade >= 5)
This is read only if the first if is
FALSE
This is read only if the
SECOND if is FALSE
Next!
Next!
Multiple ifs are checked one by one!
Else ifs are checked depending on the result
of the previous condition
8/13/2019 Major Topic 2 - Condition
17/29
Okay, imagine theres a promo in a store where the customer
has to choose a color (ROYGBIV) and a number (099). Theprize is as follows:
If both color and number is correct, customer gets 1 Million Pesos!
If only color or number is correct, customer gets 1K Pesos!
Lets create a program to simulate this!
1. We have to know what is the correct color and number. (Orangeand
23)
2. Next, we have to get the customers guess!3. Then we perform a check and display the result!
INTRPRG AY 2011-2012 1st Term
Lets check what we know so far
8/13/2019 Major Topic 2 - Condition
18/29
int numGuess = in.nextInt();String colGuess = in.next();
if (numGuess == 23)
System.out.println (Numbers correct!);if (colGuess.equalsIgnoreCase(orange))
System.out.println (Colors correct!);
System.out.println
(You win 1 million pesos!);
Okay, lets try one solution first
INTRPRG AY 2011-2012 1st Term
Testing testing
8/13/2019 Major Topic 2 - Condition
19/29
Number is correct and color is wrong
but the user still won 1 million pesos!
Lets examine the output:
INTRPRG AY 2011-2012 1st Term
Mhm.. Not quite.
This time theyre BOTH wrong!
but still the user wins 1 mill!?
Color is correct but number is wrong ---
STILL WHAT THE @#$#@ ?!
Yourescheming tomake mebankrupt?
8/13/2019 Major Topic 2 - Condition
20/29
Okay, lets try using an else then!
INTRPRG AY 2011-2012 1st Term
Dont forget your curly braces!
if (numGuess == 23)System.out.println (Numbers correct!);
elseif (colGuess.equalsIgnoreCase(orange))
{
System.out.println (Colors correct!);System.out.println
(You win 1 million pesos!);
}
And else here anda couple of thosecurly braces should
do the trick!
8/13/2019 Major Topic 2 - Condition
21/29
Lets check the output of that code
INTRPRG AY 2011-2012 1st Term
Some more Tests
Oh theres an improvement! but it
didnt display the 1k Prize hmm.
Would be nice if it told us to try
again next time huh?
Oh no, have to hide this output from
boss yikes!
We need the numberAND color to be
BOTH correctbefore we give the 1
mill prize...
8/13/2019 Major Topic 2 - Condition
22/29
Lets see how
INTRPRG AY 2011-2012 1st Term
Java can combine conditions!
if (numGuess == 23)
if (colGuess.equalsIgnoreCase(Orange))System.out.println(Congratulations!);
else
System.out.println(You win 1k Pesos!);
Hey! Why no statement after if ?A: An if is still a statement so this is valid!
8/13/2019 Major Topic 2 - Condition
23/29
Whats wrong this time?
When number is wrongand color is correct, no message on
the 1k Prize! Cheat!
INTRPRG AY 2011-2012 1st Term
Still problematic
Lets look a little closer shall we?
if (numGuess == 23)
if (colGuess.equalsIgnoreCase(Orange))
Check for thesecond condition if
andONLY IF thefirst condition isTRUE
Arrgh!
8/13/2019 Major Topic 2 - Condition
24/29
Meet your logical operators!
INTRPRG AY 2011-2012 1st Term
AND
OR
also known as &&
also known as | |
IF you wantALLof your conditions to be true
IF you wantANYof your conditions to be true
8/13/2019 Major Topic 2 - Condition
25/29
Okay, try out this code.
INTRPRG AY 2011-2012 1st Term
Enter: AND / OR
if (numGuess == 23 ||
colGuess.equalsIgnoreCase(Orange))
if (numGuess == 23 &&
colGuess.equalsIgnoreCase(Orange))System.out.println(1 million Pesos!);
else
System.out.println(1k Pesos!);
elseSystem.out.println(Try again!);
8/13/2019 Major Topic 2 - Condition
26/29
*Drum Roll*
INTRPRG AY 2011-2012 1st Term
OH MY!
FINALLY!ITS PERFECTLY
WORKING!
... We forgot one more operator though...
8/13/2019 Major Topic 2 - Condition
27/29
The last tool in your condition box is the NOT operator!
INTRPRG AY 2011-2012 1st Term
The Loner:
NOTalso known as !
I like to negateothers! Thats just
how I am!
Use this whenyou dont know the values you want but
know the values you DONT WANT.
Th NOT t h f
8/13/2019 Major Topic 2 - Condition
28/29
if (!(x < 9))
INTRPRG AY 2011-2012 1st Term
The NOT operator can rephrase some of
your conditions
if (x >= 9)same as
if (!x == true) if (x == false)same as
if (!(x < 9)&& y
8/13/2019 Major Topic 2 - Condition
29/29
The End!