Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Break and labelled break statements

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In chapter 4 Java 11 book, below code snippet is given :
#Code
Public class sample
{
Public static void main(String args[])
{
int[][] list ={{1,13},{5,2},{2,2}};
int search=2;
Int positionX =-1;
Int position Y=-1;
Parent_loop:
for(int i=0; i<llist.length;i++)
{
for (int j=0, j<list.length;j++)
{
If(list[i][j]=search
{
position X=i;
position Y=j;
Break parent_lopp;
}}}
If(positionX==-1|| positionY==-1)
{
Sop ("value not found );
}
else
{
Sop("Value found at +position X, position Y);
}}}


The explanation states for the labelled break output would be 1,1
If it is normal break statement, the value is 2,0

I'm not able to understand how 2,0 is occurring?? Please explain
Also, let me know what will be the answer if the labelled break was replaced by Continue statement
 
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start by posting correctly indented code; you also have some compiler errors, e.g. spelling the keyword if with a capital I. It is very difficult to understand code without indentation. Please also give us fuller details about the book including authors and page number.
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have also not copied the code correctly; you have introduced a logic error somewhere. I shall leave you to run the code and work out what you have done wrong. Please copy the code correctly with indentation and proper spacing around operators and follow he flow of execution with a pencil if you want to understand what is happening. You can also work out similarly what would happen if you wrote continue; with or without a label instead.
The suggested answer is in fact correct:-

jshell> sample.main(null);
Value found at 1, 1
// code deleted from this quote.
jshell> sample.main(null);
Value found at 2, 0

 
Mohana Mahalingam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



1)When I execute the above program, answer is
Value2found at2,0
Can you explain how the value 2,0 is obtained when Break Parent_Loop is replaced with Break statement?

2) When I execute the same with Continue parent_Loop the same answer is obtained. Can we consider break and labelled Continue statements as the same?

3) When the program is executed with Continue statement, the answer is Value found at 2,1.

Please provide explanations how these statements are working
Thanks in advance
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohana Mahalingam wrote:. . . Can you explain how the value 2,0 is obtained . . .

What did you find when you went through the code with pencil and paper?

. . . break and labelled Continue statements as the same? . . .

No.

Why are you changing between Break and break, etc? If you get one or the other in your code in the exam, the correct answers will be completely different.
 
Mohana Mahalingam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm unable to understand the flow of the program..
Can you explain how the answers arrived for labelled break, break,labelled continue and continue statements
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print the program on a sheet of paper and follow its excution line by line with a pencil. You will find all the gory details of break; and continue; in the Java® Language Specification (=JLS).One terminates the loop and one causes the loop to start again from the next iteration.
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohana Mahalingam wrote:

1)When I execute the above program, answer is
Value2found at2,0
Can you explain how the value 2,0 is obtained when Break Parent_Loop is replaced with Break statement?

2) When I execute the same with Continue parent_Loop the same answer is obtained.
Can we consider break and labelled Continue statements as the same?

3) When the program is executed with Continue statement, the answer is Value found at 2,1.

Please provide explanations how these statements are working
Thanks in advance



hmmm... These are basics as everyone advised, you should try it to get yourself using pencil paper or learning about them, in your case, you should learn more about break and continue.

You seem to be not able to understand break and continue.

I will try to provide pointers here, you have to explore more by yourself.

break :
break is used to come out of the immediate loop on meeting some condition.
break with label will come out of the labelled loop on meeting some condition.

continue :
continue is used to skip the rest of the execution on meeting some condition, and makes the program to execute the next iteration in your loop.

All the best

reply
    Bookmark Topic Watch Topic
  • New Topic