MxLDevs
MxLDevs

Reputation: 19516

When are curly braces not required for multi-line loop bodies?

Why does this code behave correctly? I've been told that multi-line loop bodies should always have curly braces

public class Sample {

    public static void main(String[] args)
    {
        int[] nums = {1,2,3,4,5,6,7,8,9,10};

        // print out whether each number is
        // odd or even
        for (int num = 0; num < 10; num++)
            if (num % 2 == 0)
                System.out.println(num + " is even");
            else
                System.out.println(num + " is odd");
    }
}

Upvotes: 2

Views: 1851

Answers (4)

Xynariz
Xynariz

Reputation: 1243

The trick here is the difference between a statement and a line. Loop bodies will only execute the next statement, unless there are curly braces, in which case the loop will execute the whole block inside the curly braces. (As mentioned in other answers, it is always good practice to use curly braces for every loop and if statement. This makes the code easier to understand, and easier to correctly modify.)

To your specific example:

An if-else statement in java is considered to be a single statement.

In addition, the following would be a valid single-line statement:

if(someBoolean)
    someAction(1);
else if (someOtherBoolean)
    someOtherAction(2);
else
    yetAnotherAction();

You could add as many else-if's as you wanted, and the compiler would still view it as a single statement. However, if you don't use the else, it views it as a separate line. For example:

for(int a=0; a<list.size; a++)
    if(list.get(a) == 1)
        someAction();
    if(list.get(a) == 2)
        someOtherAction();

This code actually won't compile, because the second if statement is outside the scope of the for loop, hence the int a doesn't exist there.

Upvotes: 5

Malwaregeek
Malwaregeek

Reputation: 2274

If your loop has only one statement, then adding curly braces wont affect your code. If else together is considered as one statement with else ifs in between, as others have mentioned above. However, multiple statements will not be executed without the curly brace.

for (int i=0;i<5;i++)
   if (i<4)
   System.out.println("Hurray");
   System.out.println("Alas");

Output

Hurray
Hurray
Hurray
Hurray
Alas     //Exited the loop here

Upvotes: 1

Aniket
Aniket

Reputation: 379

The If-else statement is considered to a single statement and hence the code works. But If you add a single line after your If-else that line will not be considered as part of for loop.

e.g. -

for (int num = 0; num < 10; num++)
        if (num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
            System.out.println("Blah");

The Output would be -

0 is even
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
Blah

Upvotes: 1

AlexWien
AlexWien

Reputation: 28727

You need curly braces when using multiple statements (not multiple lines).
However it is good practise to always use the curly braces.
This avoids bugs when later adding a statement.

Upvotes: 6

Related Questions