Mathematical and logical operators in Visualg

Asked

Viewed 86 times

0

Why the codes highlighted in asterisk do not work the way I chose?

I realized that using the smaller number first and the bigger number second, it works normally, but I’ve been itching to know why it’s wrong.

   algoritmo "semnome"
var
   nota1, nota2, media: real
inicio
      Escreval ("--------------------------")
      Escreval ("ESCOLA TIO PATINHO CRACUDO")
      Escreval ("--------------------------")
      Escreval ("Digite a primeira nota: ")
      Leia (nota1)
      Escreval ("Digite a segunda nota: ")
      Leia (nota2)
      media <- (nota1 + nota2) / 2
      Se **(media >=10) e (media <9)** entao
         Escreval ("Média: ",media)
         Escreval ("Aproveitamento: A")
      senao
           Se **(media >=9) e (media <8)** entao
              Escreval ("Média: ",media)
              Escreval ("Aproveitamento: B")
      senao
              Se **(media >=8) e (media <7)** entao
                 Escreval ("Média: ",media)
                 Escreval ("Aproveitamento C")
      senao
                 Se **(media >=7) e (media <6)** entao
                    Escreval ("Média: ",media)
                    Escreval ("Aproveitamento D")
      senao
                    Se **(media >=6) e (media <5)** entao
                       Escreval ("Média: ",media)
                       Escreval ("Aproveitamento E")
      senao
                          **Se (media <4) entao**
                          Escreval ("Média: ",media)
                          Escreval ("Aproveitamento F")
                                                 FimSe
                                        FimSe
                                FimSe
                          FimSe
                  FimSe
              FimSe
fimalgoritmo

1 answer

1


Let’s make it a little more Portuguese than it is:

If average is more or equal to And average is less than 8.

Then the two comparisons must be true so that the whole expression be true, this is the function of the relational operator e (which is usually and or && in real languages).

Let’s say the number is greater than or equal to nine, then it’s true. So that number can be 9, 10, 11, 12 or 13, or any other number in that sequence. But it can’t be 8, or 7, or less than that, right?

Then, knowing this, he goes in the second comparison and we can already conclude that it will be false, correct?

If one is true and the other is false, how the whole can be true after applying the operator e? There is no way in any situation.

In your question is spoken:

don’t work the way I chose

It even makes it hard to tell which one is right because the question says you chose something, but only you know what was chosen. There is no clear definition of what I wanted to do.

I have tried to infer what should be, but it does not seem to have logic within the knowledge I have of note classifications. The A should only be a note up, so I don’t know why it compares with the 8. B should start from a lower note and go to a slightly higher one, and so on. The code tries to consider B for a note above 8 and below 7, it doesn’t make sense.

Who knows wanted to do:

Se media > 7 e media <= 8 entao

Nor should I do so many indentations, one level is enough, imagine if there were more comparisons, where this code would end?

Browser other questions tagged

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