Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 11)
11.
What will be the output of the program?
for (int i = 0; i < 4; i += 2) 
{ 
    System.out.print(i + " "); 
} 
System.out.println(i); /* Line 5 */
0 2 4
0 2 4 5
0 1 2 3 4
Compilation fails.
Answer: Option
Explanation:

Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.

Discussion:
4 comments Page 1 of 1.

Rohit Singh Rathour said:   4 years ago
The answer is D compilation fail because "i" is declared in the for a loop. So, it can't be accessed outside the loop.

Raman said:   10 years ago
If I has declared within the loop. Then what will be the output.

for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
System.out.println(i);
}

O/p = 0 0.

2 2.

Archit rai saxena said:   1 decade ago
if i has declared within the loop. then what will be the output.

for (int i = 0; i < 4; i += 2)
{
System.out.print(i + " ");
System.out.println(i);
}

Mohan said:   1 decade ago
Compilation fails on the line 5 - System.out.println(i); as the variable i has only been declared within the for loop. It is not a recognised variable outside the code block of loop.

Post your comments here:

Your comments will be displayed after verification.