Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question Regarding For Loops

Status
Not open for further replies.

Creeder

Programmer
Jul 5, 2000
110
MY
Hi all,

I am new to Java and basically have a question regarding the for loop.

The following code;

int poweroftwo;
int x;

for (int n = 1; Math.pow(2,n) < 100; n++)
{
poweroftwo = (int)Math.pow(2,n);
}
//Execute after for loop
System.out.println(poweroftwo);

However i get an error, Variable poweroftwo is not define, but when i use a while loop instead everything is fine.
Is there a difference?

Thanks in advance.

Yue Jeen
[sig][/sig]
 
try initialising powereoftwo to 0. i copied and pasted what you had and set poweroftwo to 0 initially and then it worked fine. [sig]<p>Chris Packham<br><a href=mailto:kriz@i4free.co.nz>kriz@i4free.co.nz</a><br><a href= > </a><br>A thousand mokeys at a thousand machines, it happened, it got called the internet.[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top