gudii9
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);
}
}
above code produced below output
5000
i am not clear on logic how it produced 5000
please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so, k incremented 50*100where 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
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)
ASKER
if outer loop breaks inner loop automatically breaks
if inner loop breaks outer does not break automatically right? please advise
if inner loop breaks outer does not break automatically right? please advise
yes this is correct :)
50 iteration in outer loop, 100 iteration in inner loop for each outer loop
i = 49 : j=0 to 99 (k=4951 to 5000)
50 iteration in outer loop, 100 iteration in inner loop for each outer loop
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
* but in 20 years of coding, never wrote such loops wit breaking/jumping/early exiting etc... :)
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);
}
}
* but in 20 years of coding, never wrote such loops wit breaking/jumping/early exiting etc... :)
ASKER
for (int j = 0; j < len; j++) {
if (i == key) {
break out;
}
k += 1;
}
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...
ASKER
for (int j = 0; j < len; j++) {
if (i == key) {
break out;
}
k += 1;
}
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...
one checks before entering inner loop, the other one checks inside inner loop...
I prefer the second one... no need to enter second loop...
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?
ASKER
how to move the code like that without breaking existing code?
try
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;
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);
}
}
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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
2 nested loops:so we are
loop1: i=0 to len- 1 (99)
loop2: j=0 to len- 1 (99) increment k until i=50
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
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:
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