How this operator is not working in this code?

2

Code:

var i = 0, finished = false;
    while( (i < acentos.length) && !finished){

Question:

I have a bool variable getting false, and in my while I'm denying it.

If I am denying a false variable, it will true , correct?

There I am saying, while i is less than the size of the vector E !finished for true , continue?

or

While i is less than the size of the vector E !finished is false continue?

When I put a bool variable inside a while , does it ignore the value that was given to it? E defines as true ?.

Complete code below:

  var i = 0, finished = false; // <-- *IMPORTANTE

        while( (i < acentos.length) && !finished){ // <-- *IMPORTANTE
            if (acentos[i] && acentos[i+1] && acentos[i+2]) {
                selSeat = i;

//Codigo..

                var accept = confirm("Reserva da cadeira "+ (i + 1) +" ~ "+(i + 3)+" está disponível, aceitar?.");

                if (accept) {

                    finished = true; // <-- *IMPORTANTE
                }else{
//Codigo..
                }
            }
            i++;
        }

My while is so in the beginning

while(false && true)

When I accept the "accents" and finished gets true , then my deny operator denies it, so it stays as false , so my while stays like this

while( false && false)

Since the two conditions are the same, does it leave loop ? I may be traveling, but I'm trying to think how logic works.

    
asked by anonymous 12.07.2017 / 03:36

4 answers

4
  

If I am denying a false variable, does it become true correct?

No, you are inverting the value of the variable, you do not change the variable.

  

There I am saying, while i is smaller than the size of the vector E! finished for true continue?

That, but I would read "as long as i is less than the size of the vector AND is not finished". Or even better, "as long as i is less than the size of the vector AND is not finalized", you have to decide whether to go English or Portuguese.

  

As long as i is smaller than the vector size E! finished for false continue?

No, this reading is wrong. It can only run if everything is true. Then you can not parse as false .

  

When I put a bool variable inside a while, does it ignore the value that was given to it? and set to true?.

That does not make much sense. Nothing is ignored. "Placing" is a very ambiguous term. The finished = true; line is assigning true to the variable, probably changing its value, at least to what you can see in this code. And this will have consequence in the condition.

  

My while is so in the beginning while (false & true)

I do not know, but I believe both are true

  

ai my while so while (false & false)

This is harder to analyze with just this snippet. But it is to be true and false , which will close the loop.

  

Since the two conditions are the same, does it leave the loop?

No, it comes out when one of it is false, so the above statement is wrong, if one of them is false right away or enter the loop the first time.

&& requires both the right and left operands to be true to give true, according to truth table .

true && true = true
true && false = false
false && false = false
false && true = false

All code can be written better. You do not even need this flag finished flag.

for (var i = 0; i < acentos.length; i++) {
    if (acentos[i] && acentos[i + 1] && acentos[i + 2]) {
        selSeat = i; //tenho medo desta variável solta
        //Codigo..
        if (confirm("Reserva da cadeira " + (i + 1) + " ~ " + (i + 3) + " está disponível, aceitar?.")) {
            break;
        } else {
            //Codigo..
        }
    }
}

I've placed it on GitHub for future reference .

    
12.07.2017 / 03:41
1

The ! will always deny the expression that comes forward, therefore:

finished = true;
//!finished = false
finished = false;
//!finished = true;
    
12.07.2017 / 03:39
0
  

I have a bool variable that gets false, and in my while I'm denying it. The question is, if I am denying a false variable, does it become true correct?

When you use! you reverse the value of it, if it was true it turns false and if it was false it turns true.

  

There I am saying, while i is smaller than the vector size E! finished for true continue?   or   As long as i is smaller than the vector size E! Finished for false continue?

You're saying as long as i is smaller than the vetro size and finalized no is true continues.

  

When I put a bool variable inside a while, does it ignore the value that was given to it? and set to true?

No.

  

Let me see if I understood .., my while it is so at the beginning while (false & true)

     

When I accept the "accents" and finished it gets true, so my negation operator denies it, so it gets false, so my while stays true while (false & false)

     

Since the two conditions are equal, does it exit the loop? I may be traveling, but ... I'm trying to think how logic works.

false and true equals false.
If you want it to run if either one is true use or:
false or true is true.

    
12.07.2017 / 03:53
0

Two negations are transformed into a statement. ex: Not false - > true not 2! = 2- > not (false) - > true (two equals two, then returns a false value). So, in the while, since & & (and) is used, the value of finished must be false to fall into the internal code.

    
12.07.2017 / 03:47