Professional Documents
Culture Documents
So far
We have learned about variables, expressions, getting an input and display an output! Weve been computing a lot of things this past week right?
.
Mr. Calculator
Somethings missing
Right now our programs are robots and mindless slaves (they always will be anyway )
Statement 1 I wanna conquer the world!
Statement 2
Statement 3
Statement 4
Statement 1
Statement 2
Example : If Jim got the right answer he gets 1 point. If the file isnt there, display an error message.
When Java performs a CHECK, there are only two possible answers: pass or fail TRUE or FALSE.
X = 2 + 3
if (X == 2 + 3)
!= <
Less than
Greater than
>
>=
<=
Sample Input: 23 and 32, 100 and 50, 0 and 0, -23, -27
Pay attention to the syntax! Look at where your semi-colon should be placed!
Enter: if Statement
Move on?
Or The End?
Enter: ELSE
What if we remove the else then? How is it different?
Y has a value of 10 in first code while it has a value of 15 in the second code!
How is that different from having several ifs instead? Whats the
big diff huh?
Testing testing
Okay, lets try one solution first
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!);
This time theyre BOTH wrong! but still the user wins 1 mill!?
Color is correct but number is wrong --STILL WHAT THE @#$#@ ?!
We need the number AND color to be BOTH correct before we give the 1 mill prize...
INTRPRG AY 2011-2012 1st Term
Still problematic
Whats wrong this time? When number is wrong and color is correct, no message on the 1k Prize! Cheat!
AND
also known as | |
OR
INTRPRG AY 2011-2012 1st Term
Enter: AND / OR
Okay, try out this code.
if (numGuess == 23 || colGuess.equalsIgnoreCase(Orange)) if (numGuess == 23 && colGuess.equalsIgnoreCase(Orange)) System.out.println(1 million Pesos!); else System.out.println(1k Pesos!); else System.out.println(Try again!);
*Drum Roll*
The Loner:
The last tool in your condition box is the NOT operator!
NOT
also known as !
Use this when you dont know the values you want but know the values you DONT WANT.
if (x >= 9) if (x == false)
if (!x == true)
NOT SAME!!!!
The End!