I'm 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 access to the internet. I have the book but the doubt is that!
I'm 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 access to the internet. I have the book but the doubt is that!
The "for" loop is one of the forms of conditional deviation , that is, test if a particular condition is satisfied, and depending on the result, divert to a different statement or move on to the next statement. It is made up of four parts:
para ( iniciação ; condição ; passo )
corpo
fimpara
próxima instrução
The iniciação
is simply a code that executes before starting the loop; depending on the language, any variable defined there will exist only within the code of that loop. In others, it continues to exist outside of it.
condição
is a test to see if you should continue inside the loop or move outside the loop. Typically it is tested after initialization, so if it is not satisfied it exits the loop immediately - without executing the corpo
and the passo
never - and moves on to the next instruction.
If the condition is true, it executes corpo
, then passo
, and then tests the condition again to see if it remains true. If you continue, repeat this step again and again. Only when the condition becomes false is it out of the loop, going to the next instruction after it.
The corpo
is the main code snippet, which will be repeated zero or more times. It has access to the variables of iniciação
and condição
, and can manipulate them or not. You can also execute commands that "force" the loop break ( break
) or continue from passo
( continue
) before reaching the end of corpo
. If these commands are not used, the body follows through to the end before moving on to the next step.
The passo
is simply a small code that executes after corpo
, but before retesting condição
. In general, its function is to manipulate the variable (s) that makes up the condição
, so as to give it a chance to exit 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"
Output:
1
4
9
16
25
36
49
64
81
100
Fim
All parts except the condition are optional. If you omit iniciação
and passo
, the result is the same as a enquanto
( while ) :
para ( ; teste() ; )
faça algo()
fimpara
enquanto ( teste() )
faça algo()
fimenquanto
The most common syntax for the "to" 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, just a similar name - for..in
, used to scroll through the elements of a list (real or abstract). Often these ties are referred to as simply for
, but their construction is different.
In Portuguese, the "to" loop with a single variable looks more or less like this (I'm not sure of the exact syntax):
para ( i de 1 a 10 passo 1 ) faca
escreva(i*i)
fimpara
The para
.. faca
(do) is a portugol
replay structure with control variables, and serves for you to execute a given block of internal code 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 the Visualg
will be:
1 2 3 4 5 6 7 8 9 10
* End of execution. * Close this window to return to Visual.
In addition to using Visualg in your studies, I also advise you to watch this class , and see this manual .
Good studies.
In other words the function will do the same task a certain number of times
Example in a race:
It will start with lap 0 // int x = 0; (on the model of Lucas Henrique)
It will have 100 turns // x
for (int x = 0; x
for
is usually used to iterate (defined by the language or a language feature) 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 with the condition:
for(int x = 0; x <= 100; x++)