Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

labelled break nested for loop

public class Main2 {
  public static void main(String args[]) {

    int len = 100;
    int key = 50;
    int k = 0;
    out: {
      for (int i = 0; i < len; i++) {
        for (int j = 0; j < len; j++) {
          if (i == key) {
            break out;
          }
          k += 1;
        }
      }
    }
    System.out.println(k);
  }
}

Open in new window


above code produced below output

5000



i am not clear on logic how it produced 5000
please advise
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Miguel Oz
2 nested loops:
loop1: i=0 to len- 1 (99)
loop2: j=0 to  len- 1  (99)   increment k until i=50
code could be read better if written this way:
for (int i = 0; i < len; i++) {
	if (i == key) {
	  break out;
	}
	for (int j = 0; j < len; j++) {
	  k += 1;
	}
}

Open in new window

the it is clear  that inner loop increments k by 100 for 50 times (0 to 49) thus  k=50*100
Avatar of gudii9

ASKER

so, k incremented 50*100
where we gave this command to multiply 50 with 100 in the code which line?
i only see there
    k += 1;

please advise
k += 1;  same as k = k+ 1;
where we gave this command to multiply 50 with 100 in the code which line?

read my post @ ID: 42318716 carefully...

we loop i and for each i, we loop j
then inside inner loop, we increment k by 1...
so inner loop runs 5000 times
Avatar of gudii9

ASKER

i = 49 : j=0 to 99 (k=4951 to 50000)

you  mean to say as below right instead of above? i.e one zero less
i = 49 : j=0 to 99 (k=4951 to 5000)
Avatar of gudii9

ASKER

if outer loop breaks inner loop automatically breaks

if inner loop breaks outer does not break automatically right? please advise
yes this is correct :)

i = 49 : j=0 to 99 (k=4951 to 5000)

50 iteration in outer loop, 100 iteration in inner loop for each outer loop
Avatar of gudii9

ASKER

code could be read better if written this way:
for (int i = 0; i < len; i++) {
      if (i == key) {
        break out;
      }
      for (int j = 0; j < len; j++) {
        k += 1;
      }
}

how you bring if condition out of inner loop by simplifying it?
How to simplify like code like this without effecting existing code logic? Any tips on developing this skill?
probably you can label inner loop like this

public class Main2 {
  public static void main(String args[]) {

    int len = 100;
    int key = 50;
    int k = 0;
    out: {
      for (int i = 0; i < len; i++) {
        in: {
          for (int j = 0; j < len; j++) {
            if (i == key) {
              break out;
            }
            k += 1;
          }
        }
      }
    }
    System.out.println(k);
  }
}

Open in new window


* but in 20 years of coding, never wrote such loops wit breaking/jumping/early exiting etc... :)
Avatar of gudii9

ASKER

for (int j = 0; j < len; j++) {
            if (i == key) {
              break out;
            }
            k += 1;
          }

Open in new window



how to decide if we can safely move if loop away from inner for loop or not?please advise
how to decide if we can safely move if loop away from inner for loop or not?please advise

i dont get what the question here but all depends on your logic...
you can break any loop any time you want...
here in this example, it says, in inner loop, check the value of outer loop value...
if it is = k then break out all loops...
Avatar of gudii9

ASKER

for (int j = 0; j < len; j++) {
            if (i == key) {
              break out;
            }
            k += 1;
          }

Open in new window



ABOVE REwritten as below right in comment ID: 42319013

for(...................){
  if (i == key) {
              break out;
            }
for (int j = 0; j < len; j++) {
         
            k += 1;
          }


i mean how  to decide if we can safely move if loop away from inner for loop or not as this case?please advise
they are the same thing...
one checks before entering inner loop, the other one checks inside inner loop...
I prefer the second one... no need to enter second loop...
Avatar of gudii9

ASKER

one checks before entering inner loop, the other one checks inside inner loop..
how to prefer one over other or decide one over other?
Avatar of gudii9

ASKER

how to move the code like that without breaking existing code?
try

public class Main2 {
  public static void main(String args[]) {

    int len = 100;
    int key = 50;
    int k = 0;
    out: {
      for (int i = 0; i < len; i++) {
        if (i == key) {
          break out;
        }
        for (int j = 0; j < len; j++) {
          k += 1;
        }
      }
    }
    System.out.println(k);
  }
}

Open in new window


or, no need to label in this case

public class Main2 {
  public static void main(String args[]) {

    int len = 100;
    int key = 50;
    int k = 0;
      for (int i = 0; i < len; i++) {
        if (i == key) {
          break;
        }
        for (int j = 0; j < len; j++) {
          k += 1;
        }
      }
    System.out.println(k);
  }

Open in new window

}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

2 nested loops:
loop1: i=0 to len- 1 (99)
loop2: j=0 to  len- 1  (99)   increment k until i=50
so we are
2 nested loops:
loop1: i=0 to len- 1 (99)
loop2: j=0 to  len- 1  (99)   increment k until i=50 in the inner loop with varialbe j by moving if condition out of inner loop to outer loop body above

Actually it makes more readable and has better performance.
i got readable part after reading code many times. But how it improves performance? please adivse