Logical expression to (while or if) create repeat until the user hits the answer
I'm having problems with a question to set the result of a question in VisualG, so if the user answers the wrong way the question will repeat itself until the user gets it right.
The question is: "if you agree answer with (s) For Yes or (n) for no".
Here is the code I created:
var
RESPOSTA: CARACTERE
inicio
Enquanto RESPOSTA <> "n" faca
Escreva("Esta de acordo?:")
Leia(RESPOSTA)
Se RESPOSTA <> "n" entao
Escreval
Escreval("Resposta incorreta.")
Escreval("Vamos tentar novamente.")
Escreval
Escreval("Lembre-se!")
Escreval("Para Responder SIM, Digite a LETRA (s), ou Digite a letra (n) para Responder Não.")
Escreval("Boa Sorte!")
Escreval
Fimse
Se RESPOSTA = "n" entao
Escreval
Escreva("Parabéns, voçê acertou!")
Fimse
Fimenquanto
fimalgoritmo
The problem is that I was only able to put the letter n
to work as "no".
I would like to to know if there is any form of I can also put the letter s
for the answer "Yes".
All this so that the incorrect answer phrase repeats until the user hits the answer so that it stays N
for no and S
for Yes.
1 answers
Your optimized code will look like this:
var
RESPOSTA: CARACTERE
CONDICAO: LOGICO
inicio
CONDICAO <- VERDADEIRO
ENQUANTO CONDICAO = VERDADEIRO FACA
Escreval("==========================================")
Escreval(" Lembre-se! ")
Escreval(" Use LETRAS MAIUSCULAS ")
Escreval(" Para Responder SIM, Digite a LETRA (S) ")
Escreval("ou Digite a letra (N) para Responder Não. ")
Escreval(" Boa Sorte! ")
Escreval("==========================================")
Escreval(" ")
Escreval("Esta de acordo?: ")
Leia(RESPOSTA)
CONDICAO <- FALSO
ESCOLHA RESPOSTA
caso "s"
Escreval("!!!!!!!!!!!!!!!!!!!!!!!")
escreval("Parabéns, você acertou!")
Escreval("!!!!!!!!!!!!!!!!!!!!!!!")
caso "n"
Escreval("***************************")
Escreval(" Resposta incorreta. ")
Escreval("***************************")
Escreval(" ")
Escreval(" Vamos tentar novamente. ")
Escreval(" ")
Escreval(" ")
Escreval(" ")
CONDICAO<- VERDADEIRO
outrocaso
Escreval(" ")
Escreval(" ")
Escreval("############################################")
Escreval(" Você digitou um caracter inválido ")
Escreval("############################################")
Escreval(" ")
Escreval(" ")
CONDICAO<- VERDADEIRO
fimescolha
FIMENQUANTO
fimalgoritmo
I chose to use the command choose-case instead of the command if-then, since it will be more complex to implement your pseudocode using this command(if-then).
However, it is worth mentioning that the optimization of pseudocode is carried out in numerous ways, it may be that you find, on the internet or with your teacher, the solution of your problem with other commands, however fully functional.
O operation of the choose-if Command is described below:
The reserved words of this statement are
escolha
,caso
efimescolha
, being optional the use ofoutrocaso
, as is the case withse... entao
statement that can usesenao
(Composite selection) ornão
(simple selection).It is important to note that this selection statement already has its relational operation defined and can not be used other than equality, which is implicit in this instruction (the symbol never appears = in the statement ). By means of it it is only possible to check if there is equality between an integer value or desired character that you want evaluate, where their veracity will result only in the execution of the others instructions available in the case that corresponds to the value equal to valued.
If none of the values are specified as a case of this multiple choice instruction will execute the available instructions No
outrocaso
, if or even exist, or the algorithm will continue its execution afterfimescolha
.General syntax form
escolha (<identificador>) caso <valor_1> primeiro bloco de instrução caso <valor_2> segundo bloco de instrução caso <valor_3> terceiro bloco de instrução outrocaso bloco de instrução do outro caso (valor diferente de todos anteriores) fimescolha // continua as instruções do algoritmo em execução
In writing
<valor_1>
,<valor_2>
and<valor_3>
correspond to possible integer values or character that will be compared to the value which is stored in what will be described at the beginning of this instruction, right after the reserved word choice, the even in parentheses.Thus, the relational operation of equality will be performed without symbol of same is described, explicitly, in the statement
Flowchart :
Source: pseudocode control structures