Condition to continue loop while invalid value

Asked

Viewed 194 times

3

I wanted to know about this code:

Algoritmo "Brincadeira2Ou1 "

  Var   A, C, P: inteiro

Inicio

      A <- 0
      C <- 0
      P <- 0
      escreval ("(=====================2===OU===1=====================)")
      escreval ("Insira o número do (A)ndré: ")
      leia (A)
           enquanto (A <= 1) ou (A >= 2) faca
                    escreval ("Número inválido! Número deve ser 1 ou 2")
                    escreval ("Por favor, digite novamente um número para André:")
                    leia (A)
           fimenquanto

There in the structure of enquanto (A <= 1) ou (A >= 2), when I do the A receive 1 or 2, it keeps returning the line of escreval that the Número é inválido, being that it needs to be 1 or 2 (even if I type 1 or 2 the system comes back as invalid number).

The moment I withdraw the =, the wheel code validating the 1 or 2 I entered. It wouldn’t be right to be Less than or equal to 1 or A greater than or equal to 2 than the Less than 1 or A greater than 2?

2 answers

2

Every integer is less than or equal to 1 or greater than or equal to 2.

Zero? It is less than 1 (and therefore <= 1).
The 1? It is equal to 1 (and therefore <= 1).
The 2? It is equal to 2 (and therefore >= 2).
The 3? It is greater than 2 (and therefore >= 2).

That is, in doing enquanto (A <= 1) ou (A >= 2), basically the loop will continue running indefinitely, since any number typed satisfies the condition.

By taking out the equal sign (enquanto (A < 1) ou (A > 2)), you consider that the condition for the loop to continue is that the number is less than 1 or greater than 2. But if the number is 1 or 2, then it no longer satisfies the condition of the loop and it closes.


But in this case, as you only want to continue the loop if the number is not 1 or 2, a way - in my opinion - clearer would be:

enquanto (A != 1) e (A != 2)

That is, as long as the value is different from 1 and other than 2, keeps running the loop.

  • Thank you very much for your reply! I realized that I was not making much sense, I need to improve yet and much, one day I get there. In this case I also saw that connectives make a huge difference. I ended up choosing A <> 1 with connective E. This Pseudo with Visualg that I’m doing is the "game" of 2 or 1, between 3 people, which gives equal numbers of the draw, a different win and so on. Thank you very much again.

2


A <= 1

means that if it is the value 1 (it is equal) or (the smaller ones) 0, or -1, or -2, etc. it is worth.

A >= 2

means that if it is the value 2 (is equal) or (the largest) 3, or 4, or 5, etc. is worth.

So which ones aren’t? None. What’s between 1 and 2? Nothing.

How the two conditions are connected with a OU any one being true is true.

When you replace the lesser or the equal with the lesser and the greater or equal with the greater, everything changes:

A < 1

means that if it is the values underage that 1, that is, 0, or -1, or -2, etc. is worth.

A > 2

means that if it is the values bigger that 2, ie, 3, or 4, or 5, etc. is worth.

Now 1 and 2 have not entered the list, so they are different data. If the value pain 1 gives false in one of the conditions. If the value is 2, it is false in the other. Any one of them giving true the whole expression is true because it needs to be one or the other, it doesn’t need to be two. It will only give false if both are false. Then you need the number to be neither 1 nor 2 at the same time to give false.

I prefer to write the code like this:

Algoritmo "Brincadeira2Ou1"
    Var A: inteiro
Inicio
    A <- 0
    escreval("(=====================2===OU===1=====================)")
    enquanto verdadeiro faca
        escreval("Insira o número do (A)ndré: ")
        leia(A)
        if A == 1 ou A == 2
            sair //não sei se é assim no seu pseudo código seria um break aqui
        escreval("Número inválido! Número deve ser 1 ou 2")
    fimenquanto

I find it more logical, intuitive and more DRY, but for some reason people don’t usually do.

  • Thank you so much for your reply, you helped me so much! I realized I wasn’t making much sense of the way I was doing, I need to get better and better, one day I get there. In this case I also saw that connectives make a tremendous difference. I ended up choosing A <> 1 with connective E. this Pseudo with Visualg 3 I’m doing the "game" of 2 or 1, between 3 people, which gives equal numbers of the draw, a different wins and so on. Thank you very much again.

Browser other questions tagged

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