# help with loops

• 01-14-2003
ica0330
help with loops
I"m very lost with how i should write the code for the loop. I've been stumped for days. Any help would be greatly appreciated.

//-----------------------For_Loop----------------
void Call_The_For_Loop(int Numa1, int Numb1, int &sum1)
{ //purpose: uses the for loop to find the sum of the numbers between( and including) Numa1 and Numb1
sum1 = 0;
Put in the code for the for loop here
}// end For_Loop function
• 01-14-2003
minesweeper
Code:

for(int i = Numa1; i<=Numb1; i++)
{
sum1 += i;
}

• 01-14-2003
quzah
pesudocode:
Code:

somevalue = num1
somevalue2 = num2
biggest = whateveris bigger, num1 or num2
smallest = whatever is smaller, num1 or num2

loop initialization:
sum = 0, count = smallest
count < biggest
count++

loop body:
sum += count

This is far to easy for me to actually provide the loop for you. Read the above, it details (exhaustivly) what you need to do.

**** it. Never mind. Everyone does everyone elses homework for them anyway. What's the point. No one wants to actually learn anything.
[/edit]

Quzah.
• 01-14-2003
minesweeper
Ahhhhh, yes but your way was far better.

I assumed that Numb1 was largest.
• 01-14-2003
ica0330
thank you for the psuedocode. it helped me understand a little.

could you help exlpain to me how I would do the same thing using a while loop and do while loop.
• 01-14-2003
quzah
Use the same as my pseudocode. Just change where you put the initializers, and where you put the check. A while loop has initializers before the loop itself, the check in the while( ) block, and the incrementors inside the loop. A do-while has the initializers before the loop, the incrementation inside the loop, and the check at the end of the cycle, in the while( ) block.

Quzah.
• 01-14-2003
minesweeper
Code:

initialise counter

initialise sum

set counter to low number

increment counter

while counter is not greater than high number

Code:

initialise counter

initialise sum

set counter to low number

while counter is not greater than high number