Johnny G Junior's NoteBook Treasures: IF true then do this or ELSE do this.

Friday, April 18, 2008

IF true then do this or ELSE do this.

If else statements look a bit crazy but its more of a sentence then anything... you just need to know how to format it. I will try to explain it the simpliest way i can. it just may confuse the hell out of you, but thats the price you have to pay being a nerd.

so i go into a gas station and i want to buy a soda. im infront of the soda stand and see 2 drinks that i like. one of the drinks cost $5 dollars (the purple drink) and one is $10 dollars (sunny d). now its time for me to reach into my pocket and see what i have. when i reach into my pocket i could have possiblities. I could have a $5 or a $10 dollar bill in my pocket, (i know its more then a dollar and less then a 20). I pull out the bill and :::


IF (the bill is the same as a 5)
{ then i will buy the purple drink; }
ELSE
{ i can afford to buy the sunny d drink and i will; }


Well... thats the format in english but now i will break it down for real.

If (condition) {
do whatever's in these {blocks}
} else {
if that condition is not true, do this block set
};


Condition - it is a senero like 5 > 1. this is a true condition so the next thing to be read is the command following the condition in the {} <<-- a start and close block. if the condition was like this. 5 < 1. Well thats a false statement and it will move to the command and block. since it is false and we have an else, it will do whatevers in that block.

You can not have an else without an if. but you can have an if without and else.


if (bill > 5){
purchase the sunny d;
}


bill = 1 dollar making condition FALSe

if i reached into my pocket and i had a dollar... then it would just move out of the block {} and do nothing.

if (TRUE) do { this } else { this set }

Now we could have if statements within an if statement. you just have to follow the format already provide. think about it as if one format ate another.

if (condition) { <--- start block a(if)
if (condition) { <-- start block b(if)
do this;
} else { <--- end block b(if) start block b (else)
do this;
} <---- end block b (else)
} else { <---- end block a(if) start block a(else)
do this.
} <---- end block a(else).




if (bill == 5) { ** == is same as
if (bill == ripped){
i cant buy the drink;
} else {
i can buy the drink;
} else { <-- if bill is not the same as a 5 FALSE do this one.
its a 10 dollar bill and i can buy sunny d;
}


WOAH. so the senerio is the same as above. if i pull out 5 dollars out of my pocket, i will do whats in the block... but if my 5 dollars is ripped, then i cant use it... so i will do whats in the next if statement.

the key is ask yourself the logical question. If this is a true statement.. then what will i do... or ELSE i can do this...

No comments: