Loops - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Loops

Posts: 3Member
Hi everybody i just need help in loops i know its simple but with simple things you get stusk on the most so if you can help me please do.
My question is that i have to add all numbers between 1 to 100 and out put the result by using for, while and repeat loops for each.
thanks a loot.

• Posts: 6,349Member
: Hi everybody i just need help in loops i know its simple but with simple things you get stusk on the most so if you can help me please do.
: My question is that i have to add all numbers between 1 to 100 and out put the result by using for, while and repeat loops for each.
: thanks a loot.
:
This looks much like simple homework, so I will not hand you the code necessary to create the sums. I will however help you on your way.

When adding numbers together, firstly you have to remember to set the sum variable to 0, otherwise your results might come out very wrong. The simplest code to use in this case is the for-do loop. Just take a look at the help files and some of the examples. The for-do loop allows you to easily specify a range of numbers. Since you want to add all numbers in a specified range, this shouldn't be too difficult (providing you know how to add).
The second best method in this case is the repeat-until. Here you are responsible for maintaining the counter. Here is a code, to illustrate that principle:
[code]
i := 1; { set counter to start value }
repeat
i := i + 1; { increase counter for each loop}
until i = 100; { continue increasing until a maximum }
[/code]
If you read the code like the compiler does (line for line) you will see that the program will first perform the action and then check if it need to continue. The while-do loop is similar to the repeat-until, but exchanges the order of checking and performing:
[code]
while { some check } do begin
{ some action }
end;
[/code]
This difference is very important when creating the sums.

I would suggest that you try to write the code and play around with the order in which you increase the counter (in case of repeat-until & while-do) and the actual adding to the sum. You also need to figure out the check of the while-do loop. If you have more question, please include the code you have written.