for loops not working

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

for loops not working

Postby nickbrickmaster » Mon Jul 30, 2012 12:54 am

Any code I put inside a for loop never happens.
Here is my code, just to test for loops:
Code: Select all
public class Test {
   public static void main(String[] args) {
      
      for (int i=0;i==7;) {
         LCD.drawString("Hello!", 0, i);
      }
      Button.waitForAnyPress();
   }

}

I am using the latest version of LeJOS with Eclipse and the plugin.
Any help would be appreciated.
nickbrickmaster
New User
 
Posts: 2
Joined: Mon Jul 30, 2012 12:50 am

Re: for loops not working

Postby skoehler » Mon Jul 30, 2012 1:40 am

Yes, you don't seem to understand how for loops work. Try the following:

Code: Select all
public class Test {
   public static void main(String[] args) {
     
      for (int i=0;i<7;i++) {
         LCD.drawString("Hello!", 0, i);
      }
      Button.waitForAnyPress();
   }

}


"for (A; B; C){D}" translates into "A; while(B){D; C}". The code you posted translates into
Code: Select all
int i=0;
while (i==7) {
         LCD.drawString("Hello!", 0, i);
}


I hope you see now, why it isn't working.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm

Re: for loops not working

Postby nickbrickmaster » Mon Jul 30, 2012 3:39 pm

Oddly enough, for loops always worked that way for me. Thank you for pointing out I am an idiot.
nickbrickmaster
New User
 
Posts: 2
Joined: Mon Jul 30, 2012 12:50 am

Re: for loops not working

Postby skoehler » Mon Jul 30, 2012 3:43 pm

nickbrickmaster wrote:Thank you for pointing out I am an idiot.

I didn't say that.
skoehler
leJOS Team Member
 
Posts: 1128
Joined: Thu Oct 30, 2008 4:54 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff