Help in Portugol (se e senao encadeado)

Asked

Viewed 1,171 times

0

It was proposed by the teacher to perform an activity, that from a day, month and year entered by the user, the algorithm should verify whether it is a valid date or not. Consider months with 30 and 31 days with the exception of February, which should be treated as if it always had 29 days. However, after entering the entries, the program returns the two answers.

programa
{

    funcao inicio()
    {
        inteiro dia, mes, ano

        escreva("\ndigite o dia: ")
        leia(dia)

        escreva("\ndigite o mes: ")
        leia(mes)

        escreva("\ndigite o ano: ")
        leia(ano)

        se(mes==1 e dia>0 e dia<=31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==2 e dia>0 e dia<=29 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==3 e dia>0 e dia<=31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==4 e dia>0 e dia<=30 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==5 e dia>0 e dia<=31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==6 e dia>0 e dia<=30 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==7 e dia>0 e dia<=31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==8 e dia>0 e dia<=31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==9 e dia>0 e dia<=30 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==10 e dia>0 e dia<31 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==11 e dia>0 e dia<30 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==12 e dia>0 e dia<31 e ano>0){
            escreva("\nData válida!")
        }

        senao{
            escreva("\ndata inválida!")
        }
    }
}
  • 2

    Your senaoapplies only to the latter se, I believe that his se should all be chained. Use ou for every month with 30 days, idem for every month with 31 days and for February check whether or not it is a leap year.

  • https://www.youtube.com/watch?v=FJ4ztAYUfzc

4 answers

1

Is there any structure in the port that can chain the conditions ? If the syntax is similar to other languages the if and otherwise only relate in the month of December.

would be something like

if codição
else if outra condiçao
else if outra condiçao
else data invalida

There put everything you know, for example if you type month 3, it is different from month 12, then enter into Else. In case, it should be working only in the month of December. Another thing, from month 10 onwards the condition is <= not only <.

1

As other responses have explained, the senao is only connected to the last se. This means that the program tests all se's, and if you fall into one of them, you will print that the date is valid, and when you arrive at the last se, will fall into the senao and print that the date is invalid.

You can simplify all these checks in less condition. Note that there are several months that have 30 or 31 days, so you can group these cases into a single condition. It would look like this:

programa
{

    funcao inicio()
    {
        inteiro dia, mes, ano

        escreva("\ndigite o dia: ")
        leia(dia)

        escreva("\ndigite o mes: ")
        leia(mes)

        escreva("\ndigite o ano: ")
        leia(ano)

        se (mes <= 0 ou mes > 12 ou ano <= 0) {
            escreva("\nData inválida!")
        } senao {
            inteiro ultimoDia
            se (mes == 2) {
                ultimoDia = 29
            } senao se (mes == 1 ou mes == 3 ou mes == 5 ou mes == 7 ou mes == 8 ou mes == 10 ou mes == 12) {
                ultimoDia = 31
            } senao {
                ultimoDia = 30
            }
            se (dia <= 0 ou dia > ultimoDia) {
                escreva("\nData inválida!")
            } senao {
                escreva("\nData válida!")
            }
        }
    }
}

First I check if the month and year are valid. If it is not, nor does it matter to check the day, then I can already show the message and stop.

If the month and year are valid, I check the last day of the month, and check if the day exceeds this value. The statement says to consider February always with 29 days, but if had to verify leap year, simply put another se within the se (mes == 2).

It’s much simpler that way than making one se for each month.

0

From what I remember of Portugol the syntax of the structure se is different:

se(condição)entao
    Bloco de Comando
fimse

Example here.

And for that function I believe that using the structure "choice of case" is a better option.

  • Ivan, this is Portugol Studio, it now has keys, library support etc... There’s an online version of https://portugol-webstudio.cubos.io/ide

0

In this case the "senao" command is being applied only to the last "se". See the same code organized differently, just to facilitate understanding

...

        se(mes==11 e dia>0 e dia<30 e ano>0){
            escreva("\nData válida!")
        }

        se(mes==12 e dia>0 e dia<31 e ano>0){
            escreva("\nData válida!")
        } senao {
            escreva("\nData inválida!")
        }
    }
}

A solution is to chain commands by placing a "senao" between each of the "if" blocks. For example:

    ...
    se(mes==1 e dia>0 e dia<=31 e ano>0){
        escreva("\nData válida!")
    } senao se(mes==2 e dia>0 e dia<=29 e ano>0){
        escreva("\nData válida!")
    } senao se(mes==3 e dia>0 e dia<=31 e ano>0){
        escreva("\nData válida!")
    } senao se(mes==4 e dia>0 e dia<=30 e ano>0){
        escreva("\nData válida!")
    }
    ...

Browser other questions tagged

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