Course C Initial 17th - Loop FOR Part 2
Now, I have others notes
about loop for. This command can be used like while, but it structure
is different because the loop for use ‘;’. Below, see the basic
structure of For command.
Between ‘;’, you put a
condition and so, the command will work like a loop while. So, it
works with this: a variable; a condition; a increment or decrement.
The command FOR is a good
tool that will be used many times. But if you ask to me: What’s the
difference between this command and the command do while or while? My
answer is that the command for is universal, but do-while is a
special case that in others languages, there’s not, python is an
example. FOR works with conditions too, but it allow to us, work with
a limited possibility. Look at this image:
The image give us it
explanation. But I have a note: the last value of a is 11, but 11
isn’t large than 10, that’s the reason of the code stop. Below,
see it being ran.
The command FOR works with
know ends, never with unknown ends, when we have a code that or with
do-while, while or for, we have what we call ‘Infinite Loop’, it
happens when the stop condition is unknown. So, be careful.
About infinite loop, an
article will written. But now, we’ll see more examples of Loop for
use.
See below this example:
First, we have two variable
called Sum and Num, Sum=0 or the first Sum value is: 0. Then, we have
a loop for, first is created a variable called a, an integer, and it
first value is 0, the condition is when a be small than 10. So, it
ask to user to type a number and it is read and assigned to Num,
other note is that a is increment, because it means a order, but the
value of a in the loop isn’t changed. After the sum of Sum with Num
is assigned to itself, after a is incremented in the loop, so it’ll
be tested, when the condition be false, it stops. And so, will be
printed the end value of Sum variable. It’s a description about how
the loop for works. Below, see it working.
Test it yourself! In the next
lesson, I’ll post exercises to you practice your programmer
ability. Learn it, it’s neediness!
Well, that’s all folks!
See you late!
Comentários
Postar um comentário