#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Question Beginner / very simple if - else example not working


    Hi, all.

    I am new to programming. I expected the browser to recognize that the balance is less than zero & therefore respond with "you owe us money." For some reason, though, the browser is returning only the else statement.

    Komodo Edit shows no errors.
    I compared this against an example in a Lynda.com video - seems like it ought to work.

    I would greatly appreciate it if someone could set me in the right path here ... thank you!


    javascript Code:
     
    /* this is not working - i expected it to recognize (balance < 0);
    instead, only else statement returns */
     
    var balance = -10;
     
    if (balance >= 0) {
        alert("you do not owe us money.");
        if (balance < 0) {
            alert("you owe us money.");
        }
    } else {
        alert("else statement here.");
    }
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The value of balance can't be both greater than or equal to 0 and less than 0 at the same time. It's impossible for your "you owe us money" block to execute since that block is inside an if statement that only runs when balance is greater than or equal to 0.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Thank you so much for the explanation of what was wrong, E-Oreo.

    I now understand I was using nested statements incorrectly. I tried it again using if ... else if ... else statements and got the behavior I wanted.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    note that an else if in this case is useless, because balance >= 0 is either true (which would be the if part) or false (which is the else part). There's no third option.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo