Why is this kind of comparison/choice wrong?

Asked

Viewed 134 times

1

escreval("Digite o consumo")
   leia(consumo)
   escolha consumo
   caso <= 100
      escreval("Parabéns, voce e economico")
   caso >=101 e <200
      escreval("Cuidado com o consumo")
   caso >200
      escreval("Consumo execessivo")
   outrocaso
      escreval("Valor invalido")
   fimescolha
  • 2

    You can’t. The structure of escolha/caso will check whether consumo is equal to the value defined in each case. Basically you would be comparing consumo = <= 100, which makes no sense.

  • So I can only have one set value for the "case"?

  • @Joãovictortrindade tries to see the answer down there to see if I can help you...

1 answer

0

For your program to work this way it has to be by conditional structure se:

escreval("Digite o consumo")
   leia(consumo)
   se consumo  <= 100 e consumo>=0 entao

      escreval("Parabéns, voce e economico")
   fimse
   se consumo>=101 e  consumo<200 entao
      escreval("Cuidado com o consumo")
   fimse
   se consumo >=200  entao
      escreval("Consumo execessivo")
   fimse
      se consumo <0 entao
      escreval("Valor invalido")
   fimse

If you choose to escolha/caso you have to remember that this kind of structure is like switch/case in other languages. You must have a number or character set in order to make the case. Example case 105 ... case 200... (pre-defines the numbers that may be the ones to access the case) in the example you gave is like a comparison, if consumption is <100 do ...(Remembering that <100 would have multiple "numbers" so can not be used escolha/caso)

Browser other questions tagged

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