How do you do if/else statements these I really confusing to me


#1

// I just don't understand how to write it //


#2

Well if statements have 3 different parts,

  1. if
  2. condition
  3. code

if ( condition ) {
    //Code here!
}

So back in Algebra class do you remember using less than " < " greater than " > " signs? Well that's basically what we're doing here

if ( 1 < 2 ) {
    //Code here!
}

But let's say that the condition isn't true, like 3 is greater than 4.

if ( 3 > 4 ) {
    //THIS CODE WON'T RUN!!
}

When the condition is wrong we add a "else" statement that "catches" all of the things that don't go into the if statement.

if ( 3 > 4 ) {
    //THIS CODE WON'T RUN!!
}
else {
    //This code will run!
}

Think of it in English terms,

If 3 is greater than 4, I want to do this!. Or else I want to do this!

Does that make sense?


#3

Thank you very much! I get what you are saying . Also my mistake was that I put ; after the if statement then started the else statement as if they were two different things