Page 1 of 1
MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010
Posted: Tue Feb 20, 2018 3:55 pm
by ashishrai.kv
Code: Select all
public class TestClass{
public static void main(String[] args){
Fo : for(int i = 0; i< 10; i++){
for (int j = 0; j< 10; j++){
if ( i+ j > 10 ) break fo;
}
System.out.println( "hello");
}
}
}
why hello is printed only twice and breaks out of the loop while i debug this on eclipse and suddenly after i=2 it breaks out of the loop, and j can still go till 9th iteration,
please explain in detail its very confusing?
Re: MOCK TEST-2, Q:-46
Posted: Tue Feb 20, 2018 11:14 pm
by admin
When i is 0 or 1, j can go from 0 to 9 without breaking because i+j cannot be > 10 in these cases. This means hello will be printed once when 1 is 0 and second when i is 1. But when i becomes 2, as soon as j becomes 9, break fo; will be executed breaking the outer loop. Hence, the print statement will not be executed.
Re: MOCK TEST-2, Q:-46
Posted: Tue Feb 20, 2018 11:15 pm
by admin
Btw, please put question id in the subject line so that this topic will be associated with that question.
Re: MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010
Posted: Wed Feb 21, 2018 1:46 am
by ashishrai.kv
so that means it will print the statement only when J loop will finish all its iterations and if it doesnt then it will break out of the loop and the statement. is it?
Re: MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010
Posted: Wed Feb 21, 2018 5:39 am
by admin
Yes, break fo; breaks the outer loop.
Re: MOCK TEST-2, Q:-46 : qid: enthuware.ocajp.i.v8.2.1010
Posted: Wed Feb 21, 2018 5:49 am
by ashishrai.kv
thanks
