6
I am learning algorithm, but I was in doubt about the use of Loop para
(for
). I know it’s a silly question, but unfortunately I have little internet access. I have the book but the doubt is this!
6
I am learning algorithm, but I was in doubt about the use of Loop para
(for
). I know it’s a silly question, but unfortunately I have little internet access. I have the book but the doubt is this!
8
The "to" loop is one of the forms of conditional deviation, that is, test whether a particular condition is met, and depending on the outcome, divert to a different instruction or proceed to the next one. It is composed of four parts:
para ( iniciação ; condição ; passo )
corpo
fimpara
próxima instrução
To iniciação
is simply a code that runs before starting the loop; depending on the language, any variable defined there will only exist within the code of that loop. In others, it continues to exist outside of him.
To condição
It’s a test to see if you should continue inside the loop or get out of it. Typically it is tested after initiation, so that if not satisfied it exits the loop immediately - without executing the corpo
and the passo
not once - and go to the next instruction.
If the condition is true, he executes the corpo
, then the passo
, and then test the condition again, to see if it remains true. If it continues, repeat this step again and again. Only when the condition becomes false does it come out of the loop, going to the next instruction after it.
The corpo
is the main chunk of code, which will be repeated zero or more times. It has access to the variables of the iniciação
and condição
, and can manipulate them or not. It can also execute commands that "force" the loop break (break
) or continue from the passo
(continue
) before reaching the end of the corpo
. If these commands are not used, the body goes to the end, before moving to the next step.
The passo
is simply a small code that runs after the corpo
, but before testing again condição
. In general, its function is to manipulate the variable(s) that composes(m) a condição
, so as to give you the chance to get out of the loop.
A complete example would be:
para ( seja i igual a 1 ; enquanto i for menor ou igual a 10 ; incremente i em 1 )
imprima i vezes i, enter
fimpara
imprima "Fim"
Exit:
1
4
9
16
25
36
49
64
81
100
Fim
All parts, except the condition, are optional. If you omit the iniciação
and the passo
, the result is the same as a loop enquanto
(while):
para ( ; teste() ; )
faça algo()
fimpara
enquanto ( teste() )
faça algo()
fimenquanto
The most common syntax for the "for" loop is that of the C family (includes Java, Javascript and others):
for ( int i = 1 ; i <= 10 ; i++ ) {
printf("%d\n", i*i);
}
Some languages, such as Python and Ruby, do not have this loop, only one with a similar name - the for..in
, used to traverse the elements of a list (real or abstract). Often these links are referred to as simply for
, but its construction is different.
In English, the "to" loop with a single variable looks something like this (I’m not sure of the exact syntax):
para ( i de 1 a 10 passo 1 ) faca
escreva(i*i)
fimpara
4
The para
.. faca
(do) is a repeating structure of the portugol
with control variables, and serves for you to run a certain internal code block by n
times.
A simple example:
algoritmo "contador"
var
C: inteiro
inicio
Para C <- 1 ate 10 faca
Escreva (C)
Fimpara
fimalgoritmo
The output of this algorithm in Visualg
will be:
1 2 3 4 5 6 7 8 9 10
* End of execution. * Close this window to return to Visualg.
In addition to using Visualg in your studies, I also advise you to watch this class, and consult this manual.
Good studies.
3
In other words the function will do the same task a certain number of times
Example in a race:
It will start with around 0 /int x = 0; (in Lucas Henrique’s model)
It will have 100 laps // x <= 100; (in Lucas Henrique’s model)
For each completed lap add +1 // x++; (in Lucas Henrique’s model)
Below this statement you inform what the program will do, in case the race would run the run function until complete the 100 laps.
I hope I’ve helped
thanks. helped a lot
So if you can, vote on the question, on the left side of my answer, you know how to do that?
AP still doesn’t have enough reputation to vote (needs 10). By the way, for ( int i = 0 ; i <= 100 ; i++ )
will perform 101
times, no 100
.
Truth mgibsonbr 101 times, ;)
2
for(int x = 0; x <= 10; x++) { //code to be incremented }
whereas int x = 0 => is creating an integer variable x that gets 0, which is the starting point of your repeat loop. That is, it will start at 0.
x<= 10 => it will perform until the condition is less than or equal to 10.
x++ => is the incrementing of your loop. In this case, it represents that it will add the initial value to each iteration.
"will perform until the condition is "think you meant "as long as the condition is", right? (or until the condition is false...)
1
The for
is usually used to make iterations (defined by the language or by a language resource) or also as a conditional loop defined as follows:
Regarding the two possibilities, I take Python and C. Iterators:
for x in range(0,100):
# faça o que quiser aqui com "x"
And on condition:
for(int x = 0; x <= 100; x++)
opa valeu ai!!!
@Leonardosales Welcome to Stackoverflow, you must choose an answer, take a tour to understand how : http://answall.com/tour
Browser other questions tagged loop portugol
You are not signed in. Login or sign up in order to post.
What is the language? The loop
for
It is usually very similar from language to language, but still it would be good to specify. And I didn’t quite understand your question: you said you were in doubt, but in relation to what? Your syntax, semantics, something else... If you can [Dit] adding more details would be very welcome!– mgibsonbr
in portugol itself.
– Leonardo Sales
Related: "I can’t learn syntax for"
– mgibsonbr