Condition to continue loop while value is invalid

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 is returning the line of escreval that the Número é inválido, being that it needs to be 1 or 2 (even I typing 1 or 2 the system returns as invalid number).

The moment I pull out the =, the code runs validating the 1 or 2 I entered. It would not be right to be less than or equal to 1 or greater than or equal to 2 than less than 1 or greater what 2 ?

Author: Maniero, 2020-05-29

2 answers

A <= 1

Means that if it is the value 1 (is equal) or (the smallest) 0, or -1, or -2, etc. it's worth it.

A >= 2

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

So which ones aren't worth it? None. What's between 1 and 2? Nothing.

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

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

A < 1

Means that if it is the values smaller than 1, that is, 0, or -1, or -2, etc. it's worth it.

A > 2

Means that if it is the values greater than than 2, i.e. 3, or 4, or 5, etc. it's worth it.

Now 1 and 2 have not entered the list, so they are different data. If the value dor 1 gives false in one of the conditions. If the value is 2 it gives false on the other. Any of them giving true the whole expression is true because it needs to be one or the other, it does not have to be two. It will only give false if both are false. So 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 it.

 2
Author: Maniero, 2020-06-01 13:16:44

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

Or zero? It is less than 1 (and therefore <= 1).
Or 1? Is equal to 1 (and therefore <= 1).
Or 2? Is equal to 2 (and therefore >= 2).
Or 3? Is greater than 2 (and therefore >= 2).

That is, when doing enquanto (A <= 1) ou (A >= 2), basically the loop will continue to run indefinitely, since any number entered satisfies the condition.

When taking 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, since you only want to continue the loop if the number is neither 1 nor 2, a clearer form - in my opinion - would be:

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

That is, as long as the value is different from 1 and different from 2, it continues to loop.

 2
Author: hkotsubo, 2020-06-02 15:56:16