Is it possible to put an OR in the FOR stop condition?

Asked

Viewed 158 times

2

It is possible to put two conditions to stop one for? In my example I’m trying to do this:

function criacaoParidade(entrada) {
    entrada = Array.from(entrada);
    func = positionBit(entrada);

    tam = entrada.length;
    var dic = {};

    for (let index = 0; (index < tam || paridade > tam); index++) {
        var paridade = func[index];
        console.log(paridade)
    }
  • 2

    It seems to me that the correct situation should be to use a While. ie, while X condition OR Y condition is true executes the code. I think that’s more what you want.

  • 1

    Yes you can do that, but you have to define the parity variable outside the cycle for.

1 answer

2


Having two conditions cannot, only one is possible which is the middle, as you may already know. But you can use the || or other operators, several times. It will still have a condition, but the expression can be much more complex, what matters is that in the end it results in a Boolean. Then you can have several comparative expressions linked by relational operators.

Except in your case it doesn’t make sense, it’s impossible index be less than tam, after all it was declared valid 0 just before and tam is a size that cannot be less than 0. You could make it that simple:

for (let index = 0; index < entrada.length; index++)

I put in the Github for future reference.

It doesn’t make sense, and it’s usually worse, to get the size out of the loop. There are other very weird and probably wrong things in this code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.