### nesting for loops

I am new to programming. Why will my y variable not increment inside the inner loop? i was expecting 10(y) to be divided 1 thru 10(x)then decrease y by 1 and x by 1 (x<=y), and start the whole process again until i reached 0. How do I do this? here is what i have.

#
 ``123456789101112131415161718192021222324252627282930313233`` ``````include using namespace std; int main() { double x; double y; double z; cout << " Enter Starting point: "; cin >> x; cout << " Enter Ending point: "; cin >> y; for(y;y>=0;y--) { for (x;x<=y;x++) { z=y/x; cout << " The Quotient is: " << z <<"\n"; cout << " Y is: " << y <<"\n"; cout << " X is: " << x <<"\n"; } } system("pause"); return 0; }``````
Last edited on
Im confused but you should read what you did.

for(y; y>=0; y--) <-- the y-- will reduce y by 1 until it reaches 0

No where in your loop do you increase y.

If you input 10 for y and 4 to x - what this is doing is
y = 10
x = 4
z = 10/4

y = 10
x = 5
z = 10/5

y = 10
x = 6
z = 10/6

...

then to:
y = 9
x = 10 (because you increment x, you might want a place holder for it?)
loop ends

edit: the x loop ends, which ends the output statements (because they are in the x loop brackets), the y loop will continue until y = 0
Last edited on
 `First of all, please edit your post so it uses code tags - select the code then press the <> button on the right under format.`

You should not use doubles in a for loop like that - it will almost certainly fail. Use integer types in loops then cast them to double inside the loop.

HTH
Thanks for your help and your are right. i did not know how to change y so the "forloop" with x will run until y is zero. I thought because it was inside another loop it would be forced to loop again. I need to intialize x to one for every outside loop. Thanks Again!!
Were you replying to me or deviants?
Topic archived. No new replies allowed.