-2

I have my homework but I have a problem understanding this code because it is new to me, it is called Labeled break statement, I'm having a hard time to translate the code to a flowchart

Code:

1. class LabeledBreak {
2. public static void main(String[] args) {
3.
4. first:
5. for( int i = 1; i < 5; i++) {
6.
7. second:
8. for(int j = 1; j < 3; j ++ ) {
9. System.out.println("i = " + i + "; j = " +j);
10.
11. if ( i == 2)
12. break first;
13. }
14. }
15. }
16.}

I hope someone can help me to translate this into a flowchart and also an explanation will do also so that I can understand how this labeled break statement works.

  • So... do you understand how labelled breaks work? If you do, then you should understand the control flow here. If not, you should do some [reading](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html). – MarsAtomic Nov 05 '20 at 16:55
  • @MarsAtomic the code that I posted wasn't my code it is the example code that needs to be translated to a flowchart. – Arnold De Guzman Nov 05 '20 at 16:59
  • It doesn't matter whose code it is. If you understand the concept of a labelled break, then you can flowchart it. The point is when you're confused by an idea, research it first. Don't immediately ask for help. If you pursue a career in software, you're going to spend a lot of your time working on someone else's code, and you can't always ask the author what the code means. You have to have some base understanding of the language to interpret code. If you don't, that's OK, but you have to take the time to study. – MarsAtomic Nov 05 '20 at 17:11

2 Answers2

0

So basically what this does is print out i and j Here is the flowchart that i created

ShadowGunn
  • 246
  • 1
  • 11
0

A break statement bings the control of the program out of the innermost loop in which the break statement is written.

A labeled break is written as break <label>;. Here label is an identifier, generally given to a loop. In the given program, the outer(i) loop is given the label first and the inner(j) loop is given the label second. So when the break first; statement is encountered, the control goes out of the loop labeled as first, i.e., the outer loop.

So in your program, control goes to line number 15.