Question for the java challenged

Post everything that hasn't to do with MOHAA or MOHPA here, including site feedback/suggestions.

Moderator: Moderators

Post Reply
nihilo
Sergeant Major
Posts: 107
Joined: Thu Mar 13, 2003 6:07 am

Question for the java challenged

Post by nihilo »

Coming from C++, I have a question about loops. What's the syntax for the while loop. Eg.

Code: Select all

var = 10;
while (var >= 0)
     // do what i want
     var --;
given that is in C++, but I've seen someone use:

Code: Select all

while (1)
so I'm guessing there is a different syntax

similar question about a for loop. If i have:

Code: Select all

for (var = 10; var >= 0; var --)
it will go through a loop 10 times (or is it 11). Anyway, I'm just wondering how to translate those C++ snippits into java. They're two similar languages, but there are just a few small differences I'm finding.

Just a few little questions...
Angex
Major
Posts: 293
Joined: Mon Dec 30, 2002 1:23 pm
Contact:

Re: Question for the java challenged

Post by Angex »

Code: Select all

var = 10;
while (var >= 0)
     // do what i want
     var --;
This needs to be:

Code: Select all

int var = 10;

while(var >= 0) {
  // What ever ...
  var--;
}

I don't think java will accept this;

Code: Select all

while (1)
but instead put:

Code: Select all

while(true){
}
This is nearly fine:

Code: Select all

for (var = 10; var >= 0; var --)
Should be:

Code: Select all

int var;

for(var = 10; var >= 0; var--) {
  //what ever ...
}

OR

for(int var = 10; var >= 0; var--) {
  //what ever ...
}

BUT THE FIRST IS BETTER!
Normally you wouldn't use var, its common practice to use single characters starting at i.
Post Reply