Page 1 of 1

Question for the java challenged

Posted: Thu Mar 27, 2003 8:44 pm
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...

Re: Question for the java challenged

Posted: Thu Mar 27, 2003 9:41 pm
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.