About Question enthuware.ocajp.i.v7.2.960 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.960 :
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?
-
- Posts: 30
- Joined: Fri Dec 07, 2012 6:09 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
Are you talking about this?
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.
Code: Select all
B: for(int j = 0; j < 2; j++){
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.
-
- Posts: 19
- Joined: Thu Feb 28, 2013 9:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
This question take me a lot of time to resolving it. Do you have some tip to solve questions like that?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
I am sorry, but there is no specific tip in this case.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed Mar 27, 2013 7:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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.
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.
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
that's an excellent tip, computerinfoscience,
thanks.
Zoryana
thanks.
Zoryana
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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
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
-
- Posts: 4
- Joined: Fri Dec 27, 2013 3:39 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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.
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.
-
- Posts: 25
- Joined: Thu Mar 27, 2014 11:35 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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.
-
- Posts: 28
- Joined: Fri Jan 02, 2015 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
It is easy if you try to apply highschool mathematics.
As a first step understand what the innermost loop does:
Clearly, it increments c with the following sum:
Now, you have to run the refactored nested loops in your head
- or sum it up using an equivalent math formula -
That is
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;
}
Code: Select all
sum{ 1: 0 <= k < = j + 1} = j + 2
- 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;
}
}
Code: Select all
sum { j + 2 : j = 0,1} = 5
c = sum { 5 : i = 0,1 } = 10.
-
- Posts: 10
- Joined: Sat Jan 24, 2015 8:09 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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.
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.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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
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
-
- Posts: 6
- Joined: Sat Jun 23, 2018 10:17 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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
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
-
- Posts: 32
- Joined: Mon Dec 29, 2014 4:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.960 :
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
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
Who is online
Users browsing this forum: Bing [Bot] and 210 guests