Please help to understand the logic behind this program
class XY
{
static int x;
public static void main(
String args[])
{
outer:
for (int i=0;i<5;i++)
{
if ( x==0 )
{
x++;
continue outer;
}
System.out.print(i);
}
}
}
The output of the above program is :1234
My query is that the int variable i in for loop is a local variable and should be reset to 0 when control goes out of the loop. Once the control is at the label outer, the value of the int i should be lost and i should be set to 0. But the value is retained by i.
Similarly with a few modifications where continue is replaced with break the below code generating the error while accessing the value of i outside the for loop
class XY
{
static int x;
public static void main(String args[])
{
loop:
for (int i=0;i<5;i++)
{
if ( x==0)
{
x++;
break loop;
}
System.out.println(i);
}
System.out.println(i);
}
}
Error is
XY.java:16: cannot resolve symbol
symbol : variable i
location: class XY
System.out.println(i);
^
1 error