Posted: Fri May 05, 2006 6:56 pm Post subject: For loops
can someone please tell me what the difference is between doing it these 2 ways? because the first way doesn't work and i don't see much of a difference
code:
public class listnums1
{
public static void listnums1(int max){
int i = 0;
for (i; i<=max; i++){
System.out.println("Number: " + i);
}
}
}
Or the correct way
code:
{
public static void listnums1(int max){
for (int i = 0; i<=max; i++){
System.out.println("Number: " + i);
}
}
}
Sponsor Sponsor
Tony
Posted: Fri May 05, 2006 8:14 pm Post subject: (No subject)
well because it's like
for ( declare local? variable; condition; some operation)
so placing i where variable declaration is expected instead is wrong
Posted: Fri May 05, 2006 8:45 pm Post subject: Re: For loops
cool dude wrote:
code:
public class listnums1
{
public static void listnums1(int max){
int i = 0;
for (i; i<=max; i++){
System.out.println("Number: " + i);
}
}
}
The first part of the for-loop has to be either an initialization or an assignment. Your code clearly breaks that rule.
Maybe you meant this:
code:
public class listnums1
{
public static void listnums1(int max){
int i;
for (i = 0; i<=max; i++){
System.out.println("Number: " + i);
}
}
}
cool dude
Posted: Fri May 05, 2006 8:57 pm Post subject: (No subject)
so this is one of those pointless rules. i don't really see much of a difference in declaring a variables and then making the counted/for loop with it. thanks for explaining
P.S. how would you draw shapes in java?
[Gandalf]
Posted: Sat May 06, 2006 12:38 am Post subject: (No subject)
cool dude wrote:
P.S. how would you draw shapes in java?
You create either an applet or a Frame, JFrame, or some other kind of GUI container, and use the paint() method using the Graphics class.
rizzix
Posted: Sat May 06, 2006 3:11 pm Post subject: (No subject)
cool dude wrote:
so this is one of those pointless rules. i don't really see much of a difference in declaring a variables and then making the counted/for loop with it.
It's not really a pointless rule. It is a rule.
If you wish you may also write that snippet as follows:
code:
public class listnums1
{
public static void listnums1(int max){
int i = 0;
for (; i<=max; i++){
System.out.println("Number: " + i);
}
}