473,770 Members | 7,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2027
> 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 "compliment s" 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 "compliment s" 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

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

Similar topics

33
3553
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
3821
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
2824
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 the scope of the for loop? The same code on gcc doesn't give this error (not on the default settings at least). Is VC being fussy, and can I turn this fussiness off? -- Nick H
3
2670
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 value of y (works well). create table test (a integer) @ create table debug1 (a integer) @ create trigger test_1 after insert on test referencing new as ins for
9
4179
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
2140
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 increment if an exception is thrown. I can only figure out how to exit the loop. 'read the text file
14
3559
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
4531
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
7525
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
4048
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
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10058
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8886
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.