Mathematical and logical operators in Visualg
Why don't the highlighted codes in asterisk work the way I chose?
I realized that using the smaller number first and the larger number second, it works normally, but I was hunched over 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 answers
Let's leave a little more Portuguese than it is:
If average is more than or equal to and average is less than 8.
So the two comparisons must be true for the whole expression to 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 9, so it's true. So that number can be 9, 10, 11, 12 or 13, or any other number in that number. sequence. But he can't be 8, or 7, or less than that, right?
Then, knowing this, he goes on the second comparison and we can already conclude that it will be false, correct?
If one is true and the other is false, How can the whole be true after applying the operator e
? There's no way in any situation.
In your question is spoken:
Don't work the way I chose
It gets complicated until we talk about what is right because the question says you chose something, but only you know what was chosen. There is no clear definition of what he wished to do.
I've tried to infer what it should be, but it doesn't seem to have logic within the knowledge I have of grade classifications. The A should be only one note up, so I don't know why it compares to the 8. The 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 grade above 8 and below 7, it doesn't make sense.
Who knows wanted to do:
Se media > 7 e media <= 8 entao
Shouldn't do so many indentations either, one level is enough, imagine if I had more comparisons, where would that code end up?