Iteration: WHILE and REPEAT · Iteração: WHILE e REPEAT
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · Esta página precisa de um navegador recente (com suporte a SharedArrayBuffer). Por favor, atualize Chrome, Edge, Firefox ou Safari para a versão mais recente.
English
WHILE tests first
- A pre-condition loop tests the condition, then maybe runs the body.
- If the condition is already
FALSE, the body never runs. - The block ends at
ENDWHILE.
Português
WHILE testa primeiro
- Um loop pré-condição testa a condição, depois talvez execute o corpo.
- Se a condição já for
FALSE, o corpo nunca executa. - O bloco termina em
ENDWHILE.
DECLARE N : INTEGER
N ← 3
WHILE N > 0
OUTPUT N
N ← N - 1
ENDWHILE
English
REPEAT runs, then tests
- A post-condition loop runs the body, then tests.
- The body runs at least once.
- The loop stops when the condition becomes
TRUE.
Português
REPEAT executa, depois testa
- Um loop pós-condição executa o corpo, depois testa.
- O corpo executa pelo menos uma vez.
- O loop para quando a condição se torna
TRUE.
DECLARE N : INTEGER
N ← 0
REPEAT
N ← N + 1
OUTPUT N
UNTIL N = 3
English
Watch out
WHILEstops onFALSE.REPEATstops onTRUE. They are opposite tests.- Something inside a
WHILEmust change the condition, or the loop never ends.
Português
Atenção
WHILEpara emFALSE.REPEATpara emTRUE. São testes opostos.- Algo dentro de um
WHILEdeve alterar a condição, senão o loop nunca termina.
English
Common mistakes
ENDWHILEcloses aWHILE.UNTILcloses aREPEAT.- A
REPEATthat should stop at 3 must updateNbefore the test.
Português
Erros comuns
ENDWHILEfecha umWHILE.UNTILfecha umREPEAT.- Um
REPEATque deve parar em 3 deve atualizarNantes do teste.
English
Now you try
- Write one
WHILEand oneREPEAT.
Português
Agora você tenta
- Escreva um
WHILEe umREPEAT.
Set · Definir N to · até 3. While N is greater than 0, output N and then subtract 1. · Defina N para 3. Enquanto N for maior que 0, saia N e depois subtraia 1.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Use REPEAT … UNTIL to output 1, 2 and · e 3. · Use REPEAT … UNTIL para imprimir 1, 2 e 3.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.