• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Regarding contiue keyword

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



The label is used to determine which loop you are refering to -- in order to do the "continue" operation. A "continue" does not leave the loop. It does not restart the loop. It ends the current iteration, and starts the next one.

Henry
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



You did more than replace "continue" with "break" -- you added a new println statement. A println statement that uses "i" when it is out of scope -- which is why the compiler is complaining.

Henry
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just in addition to Henry's reply, if your label is refering to another loop outer than loop you are calling the continue statement, in this case, the inner loop local variable will be initialized.


Output is :
z :1 i:0
z :1 i:1
z :1 i:2
z :2 i:0
z :2 i:1
z :2 i:2
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Error is
XY.java:16: cannot resolve symbol
symbol : variable i
location: class XY
System.out.println(i);
^
1 error


if you want why the complie time error occurs ,I will talk wiith you the reason
your program is just like that (attentin:I don't talk you with the label continue or break,I say something about the scoping of the variable )


now you can see the local variable is in the inner bracket,so in the outer bracket except in the inner bracket this variable is NOT visible
 
Liar, liar, pants on fire! refreshing plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic