Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Like Tree2Likes

Thread: if if

  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    if if

    in an if else branch. if the first condition is true, the else statement will not be read, but what if there is an if if if branch, will each if statement be read and executed?(provided the condition evaluates to true)

  2.  

  3. #2
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Are you talking about else if? If so, only the true condition will run.
    busso likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  4. #3
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    is th following not possible?

    function name(){

    if (condition){
    code
    }

    if (another condition){
    more code
    }

    if (one last condition){
    last bit of code
    }
    }

  5. #4
    Senior Member Ronald Roe's Avatar
    Join Date
    Mar 2011
    Location
    Oklahoma City
    Posts
    3,141
    Member #
    27197
    Liked
    959 times
    Yes, you can do that as well. In that case, all of them will run.
    busso likes this.
    Ron Roe
    Web Developer
    "If every app were designed using the same design template, oh wait...Bootstrap."

  6. #5
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    I don't understand the context of the question. If the two statements aren't related (i.e. one condition or the other), then there would be no need to run if / if statements. You can do what Ron suggested and I have done it myself, but only in cases where the if conditions weren't related in some way.
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    You also have this one to use ...
    PHP: CASE - Manual


  8. #7
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    doesn't the interpreter stop reading once a correct case is found re the switch function?

  9. #8
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    Quote Originally Posted by TheGAME1264 View Post
    I don't understand the context of the question. If the two statements aren't related (i.e. one condition or the other), then there would be no need to run if / if statements. You can do what Ron suggested and I have done it myself, but only in cases where the if conditions weren't related in some way.

    its probably down to me being a newbie at programming lollolllol

  10. #9
    Unpaid WDF Intern TheGAME1264's Avatar
    Join Date
    Dec 2002
    Location
    Not from USA
    Posts
    14,483
    Member #
    425
    Liked
    2783 times
    Quote Originally Posted by busso View Post
    doesn't the interpreter stop reading once a correct case is found re the switch function?
    Only for if/else( )if/else scenarios ... the ( ) indicates that some languages use "elseif". If you did this:
    Code:
    if ("dog" == "cat") {console.log("The animal kingdom is screwed."); }
    if ("dog" == "dog") {console.log("The animal kingdom is okay."); }
    if ("dog" == "monkey") {console.log("The animal kingdom is still screwed."); }
    Your code will process all three of those conditions, because the "if" is the beginning of a potential series of possibilities. In each case above, there is only one possibility. Now...if I change this:
    Code:
    if ("dog" == "cat") {console.log("The animal kingdom is screwed."); }
    else if ("dog" == "dog") {console.log("The animal kingdom is okay."); }
    else if ("dog" == "monkey") {console.log("The animal kingdom is still screwed."); }
    Then your code will process only the first two conditions. The "else if" indicates that "if the first condition isn't true, go onto the second one, then the third, then so on and so on until you either run out of conditions or you hit upon the right one. If you hit the right one (in this case, #2), don't bother with the other conditions."
    If I've helped you out in any way, please pay it forward. My wife and I are walking for Autism Speaks. Please donate, and thanks.

    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link.

    My stuff (well, some of it): My bowling alley site | Canadian Postal Code Info (beta)

  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    You have to decide what to do depending on what needs to be done.

    Each application might be different. In fact, what you decide to do might be different between integers and strings. Or arrays, or SQL queries, etc. You can even utilize bitwise operators that can affect an IF statement, or even eliminate the need for an IF statement in some cases.

    So there is no way we can answer your question.

    Not enough information provided.

    Give us a real script, in a real working environment, that we can actually test.
    Last edited by mlseim; Jan 28th, 2017 at 09:47 AM.



Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:18 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com