by skyvia
8 replies
I was wondering if I run a if statement inside of a loop if I can then use a else statement for it on the outside of that loop. The basic thing I am trying to do is compare all the values in an array to the id variable before proceeding to the else statement.
#loops
  • Profile picture of the author Musique
    You can't do that else statement must directly follow if statement. So nesting of a loop and if statement isn't allowed in all programming languages I ever knew. however, you can have a full if-else statement inside the loop, so if the condition you specified returned false, the code in the else statement will be executed, as follow:-

    while(while_condition){
    if(compare id variable to current array element){
    // Do something.
    }else{
    // Do something else.
    }

    }// End while loop

    Hope that helps.
    {{ DiscussionBoard.errors[4291331].message }}
  • Profile picture of the author TrueStory
    In PHP you can create as many nested IFs as you want


    however, misique has good point, use while() with if else inside

    You can also create recursive function call where you call the parent function recursively from within the loop. Probably the most underused and underutilized concept.


    If you're comparing two arrays, you can do

    foreach(array as item)
    {
    foreach(array2 as item2)
    {
    if item = item2
    do some crap
    }else / elseif(item2 vs. item)
    {
    do some other crap
    }
    } //leave inside foreach
    } //leave first foreach




    }
    Signature

    Your business matters only to people that matter to your business[/U][/B] - Reach them?

    {{ DiscussionBoard.errors[4297434].message }}
    • Profile picture of the author wizarddrummer
      If I understand your question correctly you would have a mess on your hands.

      Without being language specific lets look at some pseudo code.


      Code:
        While (some condition exists) 
           if (blah blah blah) then
              do stuff
              do more stuff
        end loop
           else
                do other stuff
           end if
      That is not a pretty site is it.

      That will not win any awards for coding and I'm not sure (even with correct syntax in a language) if it would compile correctly. It might work lexically because the while loop and an end loop and the if has its end if... I really don't know. But it is certainly not a good programming practice.

      Don't make programming more complex than it needs to be. Boolean logic can help in many ways.
      I can't post the link because my post amount is less than 15, but check this out on wikipedia. Edsger W. Dijkstra

      I call him the "Father" of modern programming. I listened to a lecture of his many decades ago and gained lots of insight about how to construct code.

      Depending on the circumstances if I have lots of ifs and elses I use the switch or case statement and if I have a really complex set of if's then I use bitwise manipulation.

      cheers
      {{ DiscussionBoard.errors[4298190].message }}
      • Profile picture of the author TrueStory
        Originally Posted by wizarddrummer View Post

        If I understand your question correctly you would have a mess on your hands.

        Without being language specific lets look at some pseudo code.


        Code:
          While (some condition exists) 
             if (blah blah blah) then
                do stuff
                do more stuff
          end loop
             else
                  do other stuff
             end if
        That is not a pretty site is it.

        ...

        THIS WILL NEVER "compile"

        the "else" will be stand-alone clause... and will throw you an exception when running the code.

        Do you want to exit WHILE loop and execute else statement? The problem is,

        IF statement in the loop will not exit the WHILE loop unless you put BREAK statement. So even if IT DID compile the "else" statement will receive last item from WHILE loop


        Can you write exactly what you want to do? I don't think we understand what you're trying to accomplish.
        Signature

        Your business matters only to people that matter to your business[/U][/B] - Reach them?

        {{ DiscussionBoard.errors[4300905].message }}
  • Profile picture of the author Harrison_Uhl
    Putting the IF/ELSE inside the loop will cause it to be executed with each iteration of the loop. If a variable was set to FALSE before the loop, and its value was only changed when a match was found (within the loop) then if the variable was still FALSE after the loop, you would execute the ELSE clause you describe.

    Harrison
    {{ DiscussionBoard.errors[4308273].message }}
  • Profile picture of the author ussher
    What you can do is to set a flag within the loop and check for that flag after the loop is finished:

    While (some condition exists)
    if (blah blah blah) then
    do stuff
    do more stuff
    else
    set the flag to TRUE
    end loop
    if(flag)
    do other stuff
    end if
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[4312447].message }}
  • Profile picture of the author rufaswan
    The basic thing I am trying to do is compare all the values in an array to the id variable
    If that's the case, in PHP, you can use:

    Code:
    if ( in_array(id, array) ) {
      // found a match
    } else {
      // no match
    }
    If your id variable is an array, you can just wrap a foreach loop on top of it:

    Code:
    foreach ( array2 as id ) {
      if ( in_array(id, array) ) {
        // found a match
      } else {
        // no match
      }
    }
    - Rufas
    {{ DiscussionBoard.errors[4326064].message }}

Trending Topics