473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

loop scope

Hello all,
Sorry for the stupid question, It's probably has a simple solution but
I'm stuck on this for quite a while...

I have this piece of code, I've numbered it for clarification.
As far as I can see it's a nested loop, the first one is (1 to 5 ) and
the inner one is (3 to 4) BUT when I debug it it looks like the inner
loop
is (3-5). why? there are no curly braces so I assumed the 3rd line for
scope is only line 4 and the if scope is line 4.
now, althogh the 5th line "seems" to execute in the debugger the
"hasval" stays 0 no matter what is the value of "digit" , can it be it
is not
really executes?
1. for (i = 0; i < PatternSize-1; i++) /* for each remaining
position in permutation */
2. {digit = perm[i] ; /* initially digit is
value in permutation */
3. for (j = 0; j < i; j++) /* for each previous element in
permutation */
4. if (perm[j] < perm[i]) digit--; /* previous position
contains smaller value, decrement digit*/
5. hashval = hashval * (SIZE - i-1) + digit;} /* multiply digit by
appropriate factor */

Thanks.

Mar 9 '06 #1
10 1998
> 1. for (i = 0; i < PatternSize-1; i++) /* for each remaining
position in permutation */
Just a quick guess: Should condition be <PatternSize-1> or just
PatternSize, as you're already using less_than "<". The last item would
be missed?

2. {digit = perm[i] ; /* initially digit is
value in permutation */
3. for (j = 0; j < i; j++) /* for each previous element in
permutation */
4. if (perm[j] < perm[i]) digit--; /* previous position
contains smaller value, decrement digit*/
5. hashval = hashval * (SIZE - i-1) + digit;} /* multiply digit by
appropriate factor */

Thanks.


Mar 9 '06 #2
zahy[dot]bnaya[At]gmail[dot]com wrote:
Hello all,
Sorry for the stupid question, It's probably has a simple solution but
I'm stuck on this for quite a while...

I have this piece of code, I've numbered it for clarification.
As far as I can see it's a nested loop, the first one is (1 to 5 ) and
the inner one is (3 to 4) BUT when I debug it it looks like the inner
loop
is (3-5). why? there are no curly braces so I assumed the 3rd line for
scope is only line 4 and the if scope is line 4.
now, althogh the 5th line "seems" to execute in the debugger the
"hasval" stays 0 no matter what is the value of "digit" , can it be it
is not
really executes?
1. for (i = 0; i < PatternSize-1; i++) /* for each remaining
position in permutation */
2. {digit = perm[i] ; /* initially digit is
value in permutation */
3. for (j = 0; j < i; j++) /* for each previous element in
permutation */
4. if (perm[j] < perm[i]) digit--; /* previous position
contains smaller value, decrement digit*/
5. hashval = hashval * (SIZE - i-1) + digit;} /* multiply digit by
appropriate factor */


That looks horrible when posted to the newsgroup. Your comments add
very little to the code. You should add a general comment above the
first for loop that explains what it does.

It's also incomplete code.

If I add braces and indent it properly I get:

for (i = 0; i < PatternSize-1; ++i) {
digit = perm[i];
for (j = 0; j < i; j++) {
if (perm[j] < perm[i]) {
digit--;
}
hashval = hashval * (SIZE - i-1) + digit;
}
}

It obvious to see what you've done.

Indent properly and use braces. It's good for your health.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 9 '06 #3
Ben Pope wrote:
zahy[dot]bnaya[At]gmail[dot]com wrote:

1. for (i = 0; i < PatternSize-1; i++) /* for each remaining
position in permutation */
2. {digit = perm[i] ; /* initially digit is
value in permutation */
3. for (j = 0; j < i; j++) /* for each previous
element in
permutation */
4. if (perm[j] < perm[i]) digit--; /* previous position
contains smaller value, decrement digit*/
5. hashval = hashval * (SIZE - i-1) + digit;} /* multiply digit by
appropriate factor */


If I add braces and indent it properly I get:

for (i = 0; i < PatternSize-1; ++i) {
digit = perm[i];
for (j = 0; j < i; j++) {
if (perm[j] < perm[i]) {
digit--;
}
hashval = hashval * (SIZE - i-1) + digit;
}
}

It obvious to see what you've done.


Actually I don't think that is equivalent to what you wrote... Just
stick the braces on, it's so much easier.

If you want to post code here, please make it complete, and minimal, so
that we can paste it into our compilers.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 9 '06 #4
That looks horrible when posted to the newsgroup. Your comments add
very little to the code. You should add a general comment above the
first for loop that explains what it does.


I have to agree, I think that is why he was "stuck on this for quite a
while..." :-)

Mar 9 '06 #5
well....
Thank you all for your "compliments" but It's not my code!
I just have to use it, I agree, It's horrible! (much worse then you
think)
Can anyone define the scopes of the loops relative to the line numbers
(In it's current, horrible state)?

Thanks.

Mar 9 '06 #6

zahy[dot]bnaya[At]gmail[dot]com wrote:
well....
Thank you all for your "compliments" but It's not my code!
I just have to use it, I agree, It's horrible! (much worse then you
think)
Can anyone define the scopes of the loops relative to the line numbers
(In it's current, horrible state)?

Thanks.


Replace the code by this:

for (i = 0; i < PatternSize-1; i++)
{ //Scope of for1 begin
digit = perm[i] ;
for (j = 0; j < i; j++)
{//Scope of for2 begin
if (perm[j] < perm[i])
{//begin
digit--;
}//end
}//Scope of for2 end
hashval = hashval * (SIZE - i-1) + digit; //xxx!
} //Scope of for1 end

1) <PatternSize-1> in the outer for(for1) looks fishy!
2) Make sure line marked <xxx!> is doing what you
want it to do - Note Precedence! I suppose its right.

W

Mar 9 '06 #7
Again, I did not write this code and the code is doing what it should.
I just want to revise it.
I was expecting the behavior exactly as described by werasm,
But when running it from the debugger it looks like the line:
hashval = hashval * (SIZE - i-1) + digit;
is being executed as part of for2.
However the hashval variable remains at it's intial value (0) although
digit is changing . It looks like the line is not being
executed at all.
How can this be? can the debugger "lie"?
Hope my question is clearer now..
Thanks.

Mar 9 '06 #8

zahy[dot]bnaya[At]gmail[dot]com wrote:
Again, I did not write this code and the code is doing what it should.
I just want to revise it.
I was expecting the behavior exactly as described by werasm,
But when running it from the debugger it looks like the line:
hashval = hashval * (SIZE - i-1) + digit;
is being executed as part of for2.
However the hashval variable remains at it's intial value (0) although
digit is changing . It looks like the line is not being
executed at all.
How can this be? can the debugger "lie"?
Hope my question is clearer now..
Thanks.


Are you compiling with optimization enabled? If so, the debugger will
typically have great difficulty displaying the correct "current source
line", due to how the code has been reshuffled.

If your debugger, supports it, maybe you should take a peek at the
generated assembly code; that will tell you which code is executed in
which loops.

Mar 10 '06 #9

zahy[dot]bnaya[At]gmail[dot]com wrote:
How can this be? can the debugger "lie"?
Hope my question is clearer now..
How is perm initialised. If perm contains all zeros, digit will remain
at its initial condition. Also perm[j] will never be less than perm[i],
as they'll be equal. If Hash starts out at zero it will remain their
under these circumstances. If we don't see how they're initialised, we
would not be able to comment. On the other hand, that would be doing
your work for you :-0.

W Thanks.


Mar 10 '06 #10
zahy[dot]bnaya[At]gmail[dot]com wrote:
Again, I did not write this code and the code is doing what it should.
I just want to revise it.
I was expecting the behavior exactly as described by werasm,
But when running it from the debugger it looks like the line:
hashval = hashval * (SIZE - i-1) + digit;
is being executed as part of for2.
However the hashval variable remains at it's intial value (0) although
digit is changing . It looks like the line is not being
executed at all.
How can this be? can the debugger "lie"?
Hope my question is clearer now..


You still haven't posted complete code.

I have seen debuggers become confused, and I have seen debuggers
attempting to show you your original code whilst debugging optimised
code. This usually confuses the user.

We can't help you very well, unless you post compilable code that
demonstrates the problem.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 10 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
7
by: Nick Howes | last post by:
When I have a couple of for loops sequentially, and they use the same counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable redeclaration. I thought that the i stayed within...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
4
by: Microsoft | last post by:
I have a for loops that goes from 1 to 256 I test the number and exception is thrown if there is an error. How do I get the program to stop in the middle of the loop and go to the next...
14
by: Michael Moreno | last post by:
Hello, Would you know what is best practice please between say: CODE 1: TimeSpan ts; for (i=0; i<1000; i++) {
8
by: Erik de Castro Lopo | last post by:
Hi all, Consider the following code snippet: do { int r = rand () ; } while (r != 0) ; It seems the compiler I'm using (GCC) does realise that the
9
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
5
by: sgurukrupagmailcom | last post by:
Hi, I haven't come accross an elegant solution to a design problem that I show below. Have a look at the piece of code here: class Exc { Exc () { System.out.println ("Haribol"); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.