Page 1 of 1

About Question enthuware.ocajp.i.v7.2.960 :

Posted: Mon Oct 08, 2012 10:56 pm
by Javanaut
I am confused when the middle loop boolean test is no longer true the control goes to the loop above it and not the loop below... Does anyone have a 'hard and fast' rule for this type of behavior in code?

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Wed Dec 12, 2012 3:09 pm
by ksnortum
Are you talking about this?

Code: Select all

B: for(int j = 0; j < 2; j++){
When j is less than 2 it exits the B for loop. It's not that it's going to the A loop, it's that it's done and exits. The next thing that happens is A increments.

Think of it this way: when the B loop gets to a false condition, it goes to the "next thing." It's finished with the C loop so it would never go there. The "next thing" in this case happens to be the A loop.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Mon Mar 18, 2013 6:11 am
by muttley
This question take me a lot of time to resolving it. Do you have some tip to solve questions like that?

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Fri Mar 22, 2013 12:21 am
by Guard
Admin??

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Fri Mar 22, 2013 6:48 am
by admin
I am sorry, but there is no specific tip in this case.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Fri Apr 19, 2013 4:36 pm
by computerinfoscience
The only tip I thought of while working out this problem was that really because there are no outside influences on the variable value, all you have to do is run through one cycle of the outer loop and then double the result.

In other words, the first time through, c = 5. You know that it will only run as long as i < 2, so it can only run one more time. This means c will = 10. Other than that these looping questions do seem to take longer than anything else.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Thu Nov 28, 2013 7:40 am
by Zoryanat
that's an excellent tip, computerinfoscience,
thanks.

Zoryana

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Thu Nov 28, 2013 7:57 am
by Zoryanat
Those loops drive me crazy.
When I try to "run loops in my head" to figure out a result in this case, I come up with 11, and this is how I come up with it:
i j k c
-------------------------
0 0 0 0
0 0 1 1
0 0 2 2
0 1 0 3
0 1 1 4
0 1 2 5
1 0 0 6
1 0 1 7
1 0 2 8
1 1 0 9
1 1 1 10
1 1 2 11
Where is an error?? Why is c equal 10 in the end? I mean, c incremented before break occurs, so it gets incremented 12 times, starting from 0 :/

Many thanks!
Regards
Zoryana

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Wed Jan 01, 2014 11:30 pm
by dalibo
c=0 1 2
i=0...
j=0...
k=0...
=>Break to B: and j=1

c=2 3 4 5
i=0...
j=1...
k=0...
=>Break to B and j++ which breaks the B:
=>Now we are in A: and i++ makes i=1 and this is what
drives me crazy when trying to figure out where am i
=>j=0 j is back to 0; fresh start for j!

c=5 6 7
i=1...
j=0...
k=0...
=>Break to B: and j=1

c=7 8 9 10
i=1...
j=1...
k=0...
=>Break to B: j++ breaks it; Break to A:i++ breaks it.

What caused me problems before and what i realized analyzing this example was that when i enter a loop from top - it goes fresh from start, and when i break to new iteration from *inside* it uses its increment and goes from there. I haven't realized that and would always lost my track where am i in the loop and counters.

Sorry if i made mistakes, but you get the overall picture, i hope it will help someone. These loops drive me crazy,they sometimes take couple of minutes just to pick the wrong answer. :)

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Fri Jun 06, 2014 6:21 pm
by __Bill
Since the outer loop runs twice (and only twice) you know the result will be an even number. Narrows it down to two choices anyway... If it ran three times the right answer would be divisible by three and so on.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Fri Jan 02, 2015 1:03 pm
by disznoperzselo
It is easy if you try to apply highschool mathematics.
As a first step understand what the innermost loop does:

Code: Select all

 
for(int k = 0; k < 3; k++){ 
   c++;  
   if(k>j)
     break; 
}
Clearly, it increments c with the following sum:

Code: Select all

 
sum{ 1: 0 <=  k < = j + 1}  =  j + 2
 
Now, you have to run the refactored nested loops in your head :)
- or sum it up using an equivalent math formula -

Code: Select all

  
for(int i = 0; i < 2; i++){
   for(int j = 0; j < 2; j++){ 
        c += j + 2;
   }
}
That is

Code: Select all

sum { j + 2 :  j = 0,1}  =  5 
c = sum { 5 : i = 0,1 } = 10.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Tue Mar 31, 2015 8:54 am
by EelcoD
I did it this way:
If you leave out the break-statement, the "c++" statement is executed 12 times (3x2x2).

Quickly write down all the loops:
i-j-k
1) 0-0-0
2) 0-0-1
3) 0-0-2
4) 0-1-0
5) 0-1-1
6) 0-1-2
7) 1-0-0
8) 1-0-1
9) 1-0-2
10) 1-1-0
11) 1-1-1
12) 1-1-2

As soon as (k>j) the next loop (if any) is not executed.

Above this means that the loop 3) is not executed, since loop 2) already satisfied the condition.
Loop 9) is also not executed, since loop 8) already satisfied the condition.

I hope this helps anyone.

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Sat Oct 07, 2017 6:17 am
by JuergGogo
The tip of computerinfoscience works fine.
As a first step forget about the outer loop A, since it has no effect on the if-break statement. So the question is reduced to two nested loops which can be analyzed easily. --> c=5
At the end double c *= 2 since Loop A runs twice. --> c=10

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Sun Sep 02, 2018 9:47 am
by RobinDRG
I added an statement in the inner loop to help me understand how it works. Hope help someone else:


i:0 j:0 k:0 C:1
i:0 j:0 k:1 C:2
i:0 j:1 k:0 C:3
i:0 j:1 k:1 C:4
i:0 j:1 k:2 C:5
i:1 j:0 k:0 C:6
i:1 j:0 k:1 C:7
i:1 j:1 k:0 C:8
i:1 j:1 k:1 C:9
i:1 j:1 k:2 C:10
final c= 10

Re: About Question enthuware.ocajp.i.v7.2.960 :

Posted: Sat Aug 08, 2020 7:16 am
by Dreamweaver
I simplify on paper like this:

c = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
i = 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1
j = 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1
k = 0 | 0 | 1 | 0 | 1 | 2 | 0 | 1 | 0 | 1 | 2