This site requires JavaScript, please enable it in your browser!
Greenfoot back
Astralman
Astralman wrote ...

2016/12/4

Why doesn't this work? Loop error.

Astralman Astralman

2016/12/4

#
 for (int i=1; i<=15; i=i*i)
        {
            number(i);
            
        }
I want it to multiply by itself. 1*1, 2*2, 3*3, 4*4, etc. How do I fix it?
Super_Hippo Super_Hippo

2016/12/4

#
Well, it starts with 1, then after 1*1 it is still 1 and it will never change.
Tritos Tritos

2016/12/4

#
for (int i=1; i<=15; i++)
       {
           number(i*i);
            
       }
I think this is what you are going for.
Nosson1459 Nosson1459

2016/12/4

#
It's an infinite loop. i=i*i is saying i=1*1 which is 1 so 'i' will never be >15.
You need to login to post a reply.