What's the difference between the "while" structure and repeat in VisualG? Do you have an example?
What's the difference between the "while" structure and repeat in VisualG? Do you have an example?
The main thing is that the condition of enquanto
is evaluated immediately on its input, so it can not execute anything from the loop if the condition is already false, whereas (sorry for the :)), that the condition of repita
is only evaluated for the first time at the end of the first block execution.
It is also different because% contains% as long as (without pun) the condition is true, and in% with% it repeats until it reaches that condition, so it repeats while the condition is false.
var i: inteiro
inicio
i <- 0
enquanto i < 10 faca
escreva(i)
i <- i + 1
fimenquanto
var i: inteiro
inicio
i <- 0
repita
escreva(i)
i <- i + 1
ate i >= 10
See What is the usefulness and importance of "do ... while"? . enquanto
is a mix of the two. It always lets you run once, but the output condition is false, the same is in repita
. In practice few languages, no strongly mainstream uses a construction equivalent to do-while
.