r/ProgrammerHumor • • Jan 16 '14

[deleted by user]

[removed]

1.3k Upvotes

446 comments sorted by

View all comments

Show parent comment

1

u/giggsy664 Jan 17 '14
for (int i = 1;i<=100;i++)
{
	if (i%3==0 && i%5==0)
	{
	System.out.println("FizzBuzz");
	}
	else if (i%3==0)
	System.out.println("Fizz");
	}	
	else if (i%5==0)
	System.out.println("Buzz");
	}	
	else
	{
		System.out.println(i);
	}
}    

is correct aswell right?

1

u/crazymuffin Jan 17 '14

Your parenthesis are kind of wrong, but otherwise yes, this would be correct (but your code would fail).

If you only execute one command after a (else/if) statement, you do not need parenthesis, just put it directly behind it (or below, depends on your habits). If you execute more than one, you need to use "{}".

1

u/giggsy664 Jan 17 '14

Oh jesus yeah I properly butchered that code

for (int i = 1; i<=100; i++)
{
	if (i%3==0 && i%5==0)
		System.out.println("FizzBuzz");
	else if (i%3==0)
		System.out.println("Fizz");
	else if (i%5==0)
		System.out.println("Buzz");
	else
		System.out.println(i);
}

Allll better