How to use the repeat command in this algorithm?
I'm with the following question but I couldn't figure out how to do it using the REPITA
Command:
Write an algorithm that asks for the age of multiple people (use repeat). Report the total of people under 25 and the total of people over 50. The program ends when age is negative (should not be used in counting).
This was one I did look good, but using the command PARA...FACA
:
var
menor,idade,maior,contador:inteiro
idade_media:real
inicio
menor <- 999
para contador de 0 ate 9 faca
escreva ("Idade: ")
leia(idade)
idade_media <- idade_media + idade
se (idade <= menor) entao
menor <- idade
fimse
se (idade>=maior) entao
maior <- idade
fimse
fimpara
idade_media <-idade_media/10
escreval
3 answers
I can sin on the exact syntax of Portugol / Visualg, but the idea is valid. I ask to fix eventual slide my
The repita
, as well as the para
is a repeat statement.
Usage:
repita
# códigos e mais códigos vem aqui
até <<condição de parada>>
Is similar in concept to enquanto
, in that it does not provide structure for evolution of repetition, this evolution needs to be controlled internally, in the code block.
In the example, the Stop condition is negative age. So, treating only the condition of the loop, it would look something like this:
leia(idade)
repita
# faz os julgamentos de idade neste trecho
leia(idade)
até idade < 0 # condição de parada: idade lida ser negativa
# imprime o relatórios dos julgamentos de idade
Note that I removed all the rest of the logic from your question and focused only on loop control, to make it easier to visualize using repita
.
Can be so...
var
menor,idade,maior,contador:inteiro
idade_media:real
inicio
menor <- 999
contador<-1
repita
escreva ("Idade: ")
leia(idade)
idade_media <- idade_media + idade
se (idade <= menor) entao
menor <- idade
fimse
se (idade>=maior) entao
maior <- idade
fimse
contador=contador+1
ate(contador=10)
idade_media <-idade_media/10
escreval(idade_media)
fimalgoritmo
Var
sairLoop: caracter
valor01, valor02: real
Inicio
repita
escreva("Digite o primeiro valor: ")
leia(valor01)
escreva("Digite o segundo valor: ")
leia(valor02)
escreval("Resultado: ", valor01 + valor02)
escreval("Deseja sair? S / N")
leia(sairLoop)
ate sairLoop <> "N"