Working with Complex Conditions

We start our code with something simple like this :

if(balance > 0){
    alert("the balance is positive");
}

If our balance is more than 0, program will print "the balance is positive" to monitor. But what if we want to print something if it's not the case? We need 'else' statement.

if(balance > 0){
    alert("the balance is positive");
} else {
    alert("the balance is negative");
}

So now if the balance is less than zero, our code will printing "the balance is negative" instead just ignoring it.

You can also make conditional statement inside another conditional statement.

if(balance > 0){
    alert("the balance is positive");
    if(balance > 1000){
        alert("you rich!!");
    }
} else {
    alert("the balance is negative");
}

First the code will check if balance is larger than 0. If true, they will printing "the balance is positive" to monitor and then check if balance is larger than 1000. If true, our code will also printing "you rich!!".

You can even check more than two condition with format like this :

if(balance > 0){
    alert("the balance is positive");
} else if(balance == 0){
    alert("the balance is zero");
} else {
    alert("the balance is negative");
}

As you can see, we have to use double equal sign (==) to check if two variable is same. Single equal sign is used for assigning value to variable, so don't forget that. If you want to check if variable is not the same, use exclamation equal sign (!=).

results matching ""

    No results matching ""