0

Say I've got this loop

while(condition1 && condition2 && condition3 && condition4){
     browseReddit();
}

if (!condition1){}
if (!condition2){}
if (!condition3){}
if (!condition4){}

It could be broken by any of the 4 conditions becoming false, and I would want to execute different code based on which one(s) it was. Ideally, I would not like to evaluate them all more than I absolutely have to. Assume browseReddit() forces us to evaluate them all again.

Putting an if chain after the loop is how I'm doing it currently. Is there a cleaner way? value brevity above all else in your answer.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Nathan
  • 6,095
  • 10
  • 45
  • 54
  • 2
    Why do you think that's not the most efficient way? What are you trying to achieve? Do you just want a shorter or oneliner method? You could use a ternary style op. – jcolebrand Nov 19 '10 at 03:19
  • 2
    What if the loop breaks out and both condition1 and condition2 are false? Do you want to deal with both? At the moment your code will only deal with the condition1 failing. You may not want those nested, and just use 4 if-ends instead. – david Nov 19 '10 at 03:22
  • @david I want to deal with each case, so that should have been 4 if-ends. Edited. – Nathan Nov 19 '10 at 03:24
  • @drachenstern, I want to know if there is a way to write this in less code, but not a one-liner. – Nathan Nov 19 '10 at 03:27
  • Did you ever get this resolved successfully? Do you still need help with this? – jcolebrand Dec 14 '10 at 04:02

3 Answers3

2
while (1) {
    stop = 0;
    if (!condition1) {
        // condition 1 code
        stop = 1;
    }
    if (!condition2) {
        // condition 2 code
        stop = 1;
    }
    if (!condition3) {
        // condition 3 code
        stop = 1;
    }
    if (!condition4) {
        // condition 4 code
        stop = 1;
    }
    if (stop) {
        break;
    }
    browseReddit();
}
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
1

Final edit:

while((condition1||function(){alert("condition1 failed!");}()) & 
      (condition2||function(){alert("condition2 failed!");}()) &
      (condition3||function(){alert("condition3 failed!");}()) &
      (condition4||function(){alert("condition4 failed!");}())){
   browseReddit();
}

jsfiddle: http://jsfiddle.net/ctrlfrk/HjftT/3/

david
  • 17,925
  • 4
  • 43
  • 57
  • I didn't think all javascript engines did shortcircuit expressions – jcolebrand Nov 19 '10 at 03:45
  • It should be written into the spec. Fixed it so it actually works now http://jsfiddle.net/ctrlfrk/HjftT/ – david Nov 19 '10 at 03:50
  • Okay I think I'm about done :p I think this evaluates the conditions the minimum number of times, and also deals with multiple failures. You just need to be careful to make sure your anonymous functions don't return true. – david Nov 19 '10 at 04:09
  • JSLint also gets angry about creating functions in a loop, but it's boound to be fine. – david Nov 19 '10 at 04:12
0

I would go with the way you have in your current code sample, so that you don't exempt yourself from testing all four of your exit cases.

jcolebrand
  • 15,889
  • 12
  • 75
  • 121