Computer Science Canada

large counted loop

Author:  cool dude [ Tue Jul 04, 2006 9:00 am ]
Post subject:  large counted loop

if you have to go to a very large number in a counted loop, it will say integer too large. so how would i do that? i tried using BigInteger, but it is not working and i'm not too sure if that is right?

Author:  r.3volved [ Tue Jul 04, 2006 12:10 pm ]
Post subject: 

counted loop as in a 'for' loop??

long myNum = 0;
while(myNum != 623456823746L)
{
++myNum;
}


if it's so long that it won't let you do a for loop, then a forloop probably isn't the best aproach

Author:  cool dude [ Tue Jul 04, 2006 1:26 pm ]
Post subject: 

r.3volved wrote:
counted loop as in a 'for' loop??

long myNum = 0;
while(myNum != 623456823746L)
{
++myNum;
}


if it's so long that it won't let you do a for loop, then a forloop probably isn't the best aproach


yes counted loop as a for loop. and i need the for loop.

p.s. why do you have a L at the end of the number?

Author:  TheFerret [ Tue Jul 04, 2006 1:29 pm ]
Post subject: 

You could have a different variable type in the counter if needed, like:

Java:
for(long i=0; i<584846484654; i++) {
}


And that should work...

Author:  r.3volved [ Tue Jul 04, 2006 2:23 pm ]
Post subject: 

L denotes a long integer
Just as if you were to do:

float myNum = 0.0f; //can use 'f' or 'F'


instantiation I guess would be:
long myNum = 0L; //can use 'l' or 'L'


I'd imagine the long would work with no problems in a for loop.
I just tend to avoid for loops that are so long like that and choose another procedure.

Author:  Krabjuice [ Tue Jul 04, 2006 5:05 pm ]
Post subject: 

Java:

for(int i=0; i<...; i++)
{
   if (i=...)
   {
       x+=1;
   }
   else if (x=...)
   {
       break;
   }
}

Replace "..." as necissary.

That should last a good while.

Author:  wtd [ Tue Jul 04, 2006 5:36 pm ]
Post subject: 

Using the assignment operator in a conditional could lead to trouble.


: