How is this operator not working in this code?

Asked

Viewed 258 times

2

Code:

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

Question:

I have a variable bool receiving false, and in my while I am denying her.

If I’m denying a false variable, it flips true, correct?

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

or

While i is smaller than vector size E !finished for false continue?

when I put a variable bool within a while, it ignores the value that was given to it? And defines how 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++;
        }

Man while this way in the beginning

while(false && true)

when I accept the "accents" and finished receives true, then my denial operator denies him, so he gets like false, woe mine while gets like this

while( false && false)

as the two conditions are equal he leaves the loop? I may be traveling, but I’m trying to think how logic works.

  • Start with https://answall.com/questions/37971/operador-not . Not inverts the value, ! finished => while not finished => while finished is fake

  • 1

    Possible duplicate of Operator NOT (!)

  • my doubt was not about the operator not itself, I know he "reverses" the values,but I think the bigown answered my question.

4 answers

5


if I’m denying a false variable, it flips true correct?

No, you are reversing the value contained in the variable, you do not change the variable.

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

That, but I would read "as long as i is smaller than the size of vector E is not finished". Or better yet, "as long as i is smaller than the size of vector E is not finished", you have to decide whether it is English or Portuguese.

While i is smaller than vector size E !finished for false continue?

No, this reading is wrong. You can only execute if everything is true. So you cannot analyze how false.

when I put a variable bool within a while, it ignores the value that was given to it? and defines how true?.

This doesn’t make much sense. Nothing is ignored. "Put" is a very ambiguous term. The line finished = true; is assigning true for the variable, probably changing its value, at least as you can see in this code. And this will have consequence in the condition.

my while this so at the beginning while(false && true)

I don’t know, but I believe they’re both true

Oh my while gets like this while( false && false)

This is more difficult to analyze just with this excerpt. But it is to stay true and false, which will close the loop.

as the two conditions are equal he leaves the loop?

No, he leaves when one of his is false, so that the previous statement is wrong, if one of them is false right off the bat or enters the loop the first time.

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

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

All code can be better written. Even you don’t need this variable of flag finished.

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 put in the Github for future reference.

  • So the logic would be like this, at the beginning my while is like this, while( true && true), and then when finished gets true, while becomes like this while(true && false) ? as both are not true it comes out of the loop.

  • @Nicolasguilhermematheus as one of them is not true he comes out.

  • that, I used break; in place of finished, I am following the steps to use the head js, so from finished, it shows me different options, have anything in javascript that I can check the values of the condition while? as if it were a php var_dump only with while. @Edit Thanks for clearing my doubts, those words though obvious made me see the code differently.. You can only execute if everything is true.

  • the loose variable is control variable if (selSeat >= 0) {&#xA; selSeat = -1;&#xA; initSeats();&#xA; }

  • @ where was it declared? This may be a problem. You can always have the values printed at any time. You have options for debug in some Ides and browsers, but as I have not done anything in JS I do not know how the practicality of this. At the time I did something in JS was bad. My intention is always to respond in a way that the person sees things differently, because it is very common who is learning to have confused ideas yet.

  • it is as global variable outside the function/method, did not know about Debugger.. very useful, I found it in Chrome and I’ve checked the values, while(true & true) and dps when finished gets true, gets while(true && false), thanks again..

  • That was my fear.

  • @Nicolasguilhermematheus as you have never accepted an answer to your questions, take a look at the [tour] how the site works. Review all your questions. Accept the one that best answered you, if any. Don’t be in a hurry, just don’t forget to do it to help the community identify the one that was best for you. Even you can vote for everything you find useful throughout the site.

  • I checked the tour, very interesting, already evaluated this post. Regarding your fear of global variable, what was your experience with it? until then, I see no problem in using.

  • 1

    @Nicolasguilhermematheus isto: https://answall.com/q/32251/101

  • interesting, did not know about global without the use of var, in my code I think it’s okay, //variavel global var selSeat = -1;&#xA;function findSeat() {&#xA; if (selSeat >= 0) {&#xA; selSeat = -1;&#xA; initSeats();&#xA; } // + código abaixo., I got interested in this top-level thing, and then I’m going to do some research on.

Show 6 more comments

1

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

finished = true;
//!finished = false
finished = false;
//!finished = true;
  • Yes yes, the question was not that, when I’m having doubts I can not express them very well, sorry, the bigown I think answered my question with these words here >> Can only perform if everything is true

0

Two denials are transformed into one statement. ex: Not false -> true not 2 != 2-> not (false) -> true (two is equal to two, then returns a false value). Thus, there in while, since &&(and) is used, the finished value has to be false to fall into the internal code.

0

I have a bool variable receiving false, and in my while I am denying it. The question is this, if I am denying a false variable, it turns 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, as long as I am smaller than the size of vector E ! finished for true continue? or While i is smaller than the size of vector E ! finished for false continue?

You’re saying as long as I’m smaller than the size of the vetro and finished nay is true continues.

when I put a bool variable inside a while, it ignores the value that was given to it? and defines it as true?

Not.

Let me get this straight.. My while is so at the beginning while(false && true)

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

as the two conditions are equal he leaves 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 any one is true use or:
false or true equals true.

  • Think with me, finished is initialized as false correct? finished = false, when I put this condition on while( !finished ) , which value boleano(true or false) you fill there? while ( ? )

  • As they said ! false equals true. So stay while(true).

  • this, my doubt was why the condition comes out of the while, when finished gets true, and as I got confused I forgot that, regardless of the condition that was given, whether it is a boolean condition or not, the while with the logical operator && only continues if the two are true. (in that case).

Browser other questions tagged

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