d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Don' Tunderstand How This Works
12Next
Add Reply New Topic New Poll
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 05:54pm
I don't understand how this code works.... I know exactly what it does and the output it gives, but i still don't get how it does it
















here's the actual code if you need it:


Code
// Using break with a label.
class Break4 {
public static void main(String args[]) {
int i;
for(i=1; i<4; i++) {
one: {
two: {
three: {
System.out.println("\ni is " + i);
if(i==1) break one;
if(i==2) break two;
if(i==3) break three;
// this is never reached
System.out.println("won't print");
}
System.out.println("After block three.");
}
System.out.println("After block two.");
}
System.out.println("After block one.");
}
System.out.println("After for.");
}
}









and output is:

i is 1
After block one.
i is 2
After block two.
After block one.
i is 3
After block three.
After block two.
After block one.
After for.

This post was edited by ferf on Mar 14 2016 05:55pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2016 06:16pm
Quote
I know exactly what it does [...], but i still don't get how it does it


can you clarify? i'm not sure what the difference is in your mind.

personally i would not describe it as "break to a label" since flow is not going to a label. flow terminates the label and continues after the label.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 06:20pm
well I get how it gives "I is x"
^pretty sure anyway



the part I don't understand is how it goes to the println statements "After block X"

This post was edited by ferf on Mar 14 2016 06:20pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2016 06:42pm
it's called a labelled break. essentially, instead of breaking from the loop and continuing after the loop, it breaks out of the entire labeled block.

there are a few youtube videos

https://www.youtube.com/results?search_query=java%20break%20label

documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

Quote
The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.


This post was edited by carteblanche on Mar 14 2016 06:42pm
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 06:55pm
Quote (carteblanche @ Mar 14 2016 08:42pm)
it's called a labelled break. essentially, instead of breaking from the loop and continuing after the loop, it breaks out of the entire labeled block.

there are a few youtube videos

https://www.youtube.com/results?search_query=java%20break%20label

documentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html


I'm still a bit confused, but ty for the videos and explanation... I'ma wait till my friend comes online and i'll ask him for more indepth explanation :P but ty :)
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 06:56pm
Oh also had a personal questions for carteblanche, was just wondering if it's normal to get stuck sometimes like this? I mean when you started learning your first language did that happen a lot to you?
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Mar 14 2016 07:00pm
Quote (ferf @ Mar 14 2016 05:56pm)
Oh also had a personal questions for carteblanche, was just wondering if it's normal to get stuck sometimes like this? I mean when you started learning your first language did that happen a lot to you?
I know you directed this question to carteblanche, but I can tell you it happened to me a lot. When it happens you can read documentation to try to figure it out. Also you can mess with the code: perhaps make a snippet based on what you don't understand and make changes to it and execute it to try to get a feel for wtf is going on. Don't be afraid to get your 'hands dirty'.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 07:05pm
Quote (Azrad @ Mar 14 2016 09:00pm)
I know you directed this question to carteblanche, but I can tell you it happened to me a lot. When it happens you can read documentation to try to figure it out. Also you can mess with the code: perhaps make a snippet based on what you don't understand and make changes to it and execute it to try to get a feel for wtf is going on. Don't be afraid to get your 'hands dirty'.


yeah i've done that, mess with the code till you figure it out, that definitely helps

I should probably read documentation as well, thanks


The reason i asked carteblanche personally though, is cuz i already know he's an adept coder...... so I'm wondering if you get stuck a lot, if you still can succeed so to speak :)
idk how good of a coder u are?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 14 2016 07:08pm
Quote (ferf @ Mar 14 2016 08:05pm)

The reason i asked carteblanche personally though, is cuz i already know he's an adept coder...... so I'm wondering if you get stuck a lot, if you still can succeed so to speak :)
idk how good of a coder u are?


i dont actually know much about coding. i just google the questions and copy/paste the responses and links. azrad is way better.
Member
Posts: 34,575
Joined: Mar 25 2009
Gold: 12,633.00
Mar 14 2016 07:10pm
Quote (carteblanche @ Mar 14 2016 09:08pm)
i dont actually know much about coding. i just google the questions and copy/paste the responses and links. azrad is way better.


oh ok haha
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll