You are on page 1of 29

PROVE IT!

My program is smarter than a 5th grader!


By: Chelsea Celestino

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?
.

I can do that too! Pssh, youre not so special!

Mr. Calculator

INTRPRG AY 2011-2012 1st Term

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

INTRPRG AY 2011-2012 1st Term

We need to make them a bit smarter!


Bring out your wands and say ----- No, no, not that.
Step 1

We need to make our programs think and make a DECISION!


INPUT

Statement 1

Statement 2

Our program needs to be able to do different things based on an input!

Example : If Jim got the right answer he gets 1 point. If the file isnt there, display an error message.

INTRPRG AY 2011-2012 1st Term

Thankfully, computers do have some sense in them!


They are able to perform a CHECK (by doing a test). They check if a certain condition is TRUE or NOT

When Java performs a CHECK, there are only two possible answers: pass or fail TRUE or FALSE.

INTRPRG AY 2011-2012 1st Term

How does Java check?


1. Are two things equal? 2. Is something greater than another? 3. Is something less than another?

Write your conditions in a way Java can understand!


To figure out if Jim got the correct answer, we should know the correct answer . We should also know what Jims answer was so we can probably say something like:

If Jims answer is equal to correct answer

INTRPRG AY 2011-2012 1st Term

Lesson 1 Formulating your condition


If Jims answer is equal to correct answer
Whats the difference between the two? Two plus three is five. STATEMENT Is two plus three five? QUESTION
Not Equal To
Whats up with that?

X = 2 + 3

if (X == 2 + 3)
!= <
Less than

Greater than

>

Greater than or equal to

>=

<=

Less than or equal to

INTRPRG AY 2011-2012 1st Term

Flex your fingers


Try the following code:

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!

INTRPRG AY 2011-2012 1st Term

Lets look at the anatomy:


boolean expression

Enter: if Statement

Sequence of statements to execute if the condition is true

Move on?

with the remaining statements?

Or The End?

End the program

INTRPRG AY 2011-2012 1st Term

Remember our guessing game?


Open GuessTheNumber.java
Do this if condition is true!

Enter: ELSE
What if we remove the else then? How is it different?

But if NOT, do this instead! (ONLY do this if condition is FALSE)

Do this REGARDLESS if the condition is true or not!!

INTRPRG AY 2011-2012 1st Term

Can I have more than one statement under an if or an else?


You mean like this?

if (x < y) int t = x; x = y; y = t; System.out.println (Done!);


But, HOW do you tell Java which lines to include under an if statement?

INTRPRG AY 2011-2012 1st Term

Defining a block of code


Try the two codes below:
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);

Lets say the user inputs 15 for both x and y.

Any difference? Is the curly braces always required?

INTRPRG AY 2011-2012 1st Term

Y has a value of 10 in first code while it has a value of 15 in the second code!

What if I want to execute my else based only on a certain condition also?


No problem, Java can handle that! 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!); What if all else fails?! else System.out.println(Dont worry about it!);
Simply add an else at the end!
INTRPRG AY 2011-2012 1st Term

How is that different from having several ifs instead? Whats the
big diff huh?

She means this one:


if (grade >= 15) System.out.println(Awesome!); if (grade >= 10) System.out.println(Pretty good!); if (grade >= 5) System.out.println(What happened!);

INTRPRG AY 2011-2012 1st Term

The mystery of Else Ifs and Many Ifs


Think it doesnt matter? Think again.
Input 8
14 17

Output using Else If What happened?


Pretty Good Awesome

Output using Many Ifs What happened?


Pretty Good What happened? Awesome Pretty Good What happened?

Dont want! My brains tired!

Now, think some more. What really happened just now?

INTRPRG AY 2011-2012 1st Term

How Java interprets the two codes


What really is happening
Else ifs are checked depending on the result of the previous condition
if (grade >= 15)
Next! if (grade >= 10) Next! if (grade >= 5) if (grade >= 15)

This is read only if the first if is FALSE


else If (grade >= 10)

Multiple ifs are checked one by one!

This is read only if the SECOND if is FALSE


if (grade >= 5)

INTRPRG AY 2011-2012 1st Term

Lets check what we know so far


Okay, imagine theres a promo in a store where the customer has to choose a color (ROYGBIV) and a number (0 99). The prize 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. (Orange and 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

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!);

INTRPRG AY 2011-2012 1st Term

Mhm.. Not quite.


Lets examine the output:
Number is correct and color is wrong but the user still won 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 @#$#@ ?!

Youre scheming to make me bankrupt?


INTRPRG AY 2011-2012 1st Term

Dont forget your curly braces!


Okay, lets try using an else then!
if (numGuess == 23) System.out.println (Numbers correct!); else if (colGuess.equalsIgnoreCase(orange)) { System.out.println (Colors correct!); System.out.println (You win 1 million pesos!); }
And else here and a couple of those curly braces should do the trick!

INTRPRG AY 2011-2012 1st Term

Some more Tests


Lets check the output of that code
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 number AND color to be BOTH correct before we give the 1 mill prize...
INTRPRG AY 2011-2012 1st Term

Java can combine conditions!


Lets see how
Hey! Why no statement after if ? A: An if is still a statement so this is valid!

if (numGuess == 23) if (colGuess.equalsIgnoreCase(Orange)) System.out.println(Congratulations!); else System.out.println(You win 1k Pesos!);

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!

Lets look a little closer shall we?


Check for the second condition if and ONLY IF the first condition is TRUE

if (numGuess == 23) if (colGuess.equalsIgnoreCase(Orange))


Arrgh!

INTRPRG AY 2011-2012 1st Term

Meet your logical operators!

AND

also known as &&

IF you want ALL of your conditions to be true

also known as | |

IF you want ANY of your conditions to be true

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!);

INTRPRG AY 2011-2012 1st Term

*Drum Roll*

OH MY! FINALLY! ITS PERFECTLY WORKING!

... We forgot one more operator though...


INTRPRG AY 2011-2012 1st Term

The Loner:
The last tool in your condition box is the NOT operator!

I like to negate others! Thats just how I am!

NOT
also known as !

Use this when you dont know the values you want but know the values you DONT WANT.

INTRPRG AY 2011-2012 1st Term

The NOT operator can rephrase some of your conditions


same as same as

if (!(x < 9))

if (x >= 9) if (x == false)

if (!x == true)

if (!(x < 9)&& y <= 9 )

NOT SAME!!!!

if (!(x < 9&& y <= 9 ))

INTRPRG AY 2011-2012 1st Term

The End!

INTRPRG AY 2011-2012 1st Term

You might also like