4

I know I can do an IF ELSE, but I need to know if it is possible to set conditional loop, like so:

for ( i=0; i<la; dr?(i++):(i--) ) {}

or

for ( if (dr) { i=0; i<length; i++ } else { i=length-1; i--} ) {}
1
  • No. Just split them up. Commented Apr 8, 2015 at 5:27

4 Answers 4

3

Another, still compact (moreso, even) but more readable and efficient way to do it would be:

var str = 'hello';
for(var i=(dr?0:str.length-1), d=(dr?1:-1); str[i]; i+=d) do_something();

And if you put 1 or -1 in dr, which makes sense I think:

for(var i=+(dr===-1&&str.length-1); str[i]; i+=dr) do_something();
Sign up to request clarification or add additional context in comments.

2 Comments

dr is boolean and mostly true. SO I guess your first solution would do.
I tested your second solution and is great, thank you, you scored.
0

Yes it is fine to use a conditional loop but it is not recommended as it becomes really hard for someone new who tries to understand your code. The code is not readable if you use the first syntax. The second syntax is much readable so I think you should prefer to use it.

But if you are are fine to use the first version of the syntax then go with it. You should always use the code which is easiest to read and maintain.

3 Comments

I ask this question because I don't want same huge code inside for to be written twice, perhaps going with this idea would not produce any performance issue. I don't mind user readability, if it works with IE8+, here we go.
@thednp:- Thats why I added in my answer, if you dont mind readabiliyt then you can go with any of the option. It will have very minimal or rather no impact as far as the performance is concerned.
can you please correct my code, the first option, I don't know why I can't eliminate the error in my code. Thank you
0

First is ok. (but you probably have to use a?b:c on your end_condition too) Second is: syntax error ^^

Crazy way:

var str = 'hello';
for(i=(dr?0:str.length-1);(dr?i<str.length:i>=0);(dr?i++:i--)) do_something();

Right way:

   var str = 'hello';
   if(dr)
      for(i=0;i<str.length;i++) do_something();
   else
      for(i=str.length-1;i>=0;i--) do_something();

(code not tested)

6 Comments

Can you please edit your answer with the best possible fix for it? Thanks
I can't since i dont know what you exactly try to achieve. (what is la? etc...). However if it's for another goal as educative, the other users are right... You might better split it in a condition + 2 different loops...
la = abbrev, stands for length of active items
then you want to scan a string from beginning to end, or from end to beginning dependings on dr = true or false ?
ok i edited, and did it with a string, but it would work the same way for an array...
|
0
  1. The first is good and working. for this:

    for ( i=0; i<4; dr?(i++):(i--) )

    Check jsFiddle here

  2. The Second one is NOT ok as for loop expects an identifier and instead got 'if'.

2 Comments

if !dr it will loop indefinitely but the syntax is ok ^^
Yes, I happen to have experienced that as well, I think my Chrome tab is dead now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.