basic static variables question

P

pauldepstein

I have code that looks like this:

for (int counter = 0; counter < 100; ++ counter)
{
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}

}

This is already legal code. My problem is that I want it to execute
the inner loop 100 times. Each time it increments counter, I want it
to do the inner loop exactly once -- once for each value of counter.
So I made the blah variable static to enforce this once-only property
(to enforce that do something was never done more than once per value
of counter). But now I expect it will only do something once in
total, not 100 somethings -- once for each value of the counter.

Any solution to what must be a standard problem.

Thank you very much for your help,

Paul Epstein
 
S

Sohail Somani

I have code that looks like this:

for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}

}

This is already legal code. My problem is that I want it to execute the
inner loop 100 times.

There is no inner loop. Only a single loop. Is that what you meant to
post?
 
P

pauldepstein

There is no inner loop. Only a single loop. Is that what you meant to
post?

Sohail,

Thanks a lot for following up. The code posted is as intended. There
is one block ( hence the { } ) inside the for loop. I was
carelessly calling the inner block a loop. I want it to do what's in
the inner block exactly once for each value of the counter.

Thanks again,

Paul Epstein
 
K

Kai-Uwe Bux

Sohail,

Thanks a lot for following up. The code posted is as intended. There
is one block ( hence the { } ) inside the for loop. I was
carelessly calling the inner block a loop. I want it to do what's in
the inner block exactly once for each value of the counter.

I am still not following. Why would

for (int counter = 0; counter < 100; ++ counter)
{
//do something;
}

not execute the block exactly once for each value of the counter?



Best

Kai-Uwe Bux
 
P

pauldepstein

I am still not following. Why would

for (int counter = 0; counter < 100; ++ counter)
{
//do something;
}

not execute the block exactly once for each value of the counter?

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

good question. The answer is that it is _not_ simple sequential
code. There is no main() statement for example. The code can respond
to events like an XL spreadsheet being changed under the xll.


Paul Epstein
 
J

Juha Nieminen

I want it to do what's in
the inner block exactly once for each value of the counter.

If you remove the static variable and the if, the block will be
executed once for each value of 'counter'. I can't see the problem.
 
P

pauldepstein

I am still not following. Why would

for (int counter = 0; counter < 100; ++ counter)
{
//do something;
}

not execute the block exactly once for each value of the counter?

Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

Here's an example where your idea would not work

suppose do something means " sing one song by Britney spears in
response to each time someone clicks on the active XL sheet".

Then, if someone clicks twice while the counter has the same value,
you won't get the effect I want. So I thought of a static int
variable to get around this.

Paul Epstein
 
P

pauldepstein

If you remove the static variable and the if, the block will be
executed once for each value of 'counter'. I can't see the problem.

Thanks. I can see why you thought this, and I wasn't clear at the
outset. The problem is that this is event-driven code driven by
clicks on an XL spreadsheet etc. With no static trick, the code could
be exercised twice for a particular value of the counter with 2 XL
clicks.

Paul Epstein
 
J

Juha Nieminen

Then, if someone clicks twice while the counter has the same value,
you won't get the effect I want.

Mouse clicks are usually events which call a function. You can't have
a mouse event jumping into the body of a loop.

If what you are doing is polling something and doing things according
to that, why can't you simply do something like:

for(counter ...)
{
code_done_once_per_loop;

while(poll())
{
code_done_for_each_polled_value;
}
}
 
J

Juha Nieminen

Thanks. I can see why you thought this, and I wasn't clear at the
outset. The problem is that this is event-driven code driven by
clicks on an XL spreadsheet etc. With no static trick, the code could
be exercised twice for a particular value of the counter with 2 XL
clicks.

Exactly how does an event-driven system in C++ manage to jump into the
body of a for-loop when an event happens? I thought events call
functions, they don't jump right in the middle of your code.
 
P

pauldepstein

Mouse clicks are usually events which call a function. You can't have
a mouse event jumping into the body of a loop.

If what you are doing is polling something and doing things according
to that, why can't you simply do something like:

for(counter ...)
{
code_done_once_per_loop;

while(poll())
{
code_done_for_each_polled_value;
}



}- Hide quoted text -

- Show quoted text -

Thanks. Unfortunately, I can't quote the code because of corporate
confidentiality but I hope this explains some of the concepts.

double NewsgroupsAreCool;
std::vector<double> SpamIsAnnoying = PreviouslyDefinedVector;

for (int newsgroup = 0; newsgroup < 100; newsgroup++){ //some
code

if( newsgroup > 0 )
{
TypeOfPointerToSomething A = B;
NewsgroupsAreCool = SpamIsAnnoying[newsgroup-1];
A->SomeFunc(OtherDefinedVector[newsgroup-1]);
PreviouslyDeclaredVector = SomeFunction(NewsgroupsAreCool, A);


}

// more code
}

This is bugged though. What happens is that when I do the intended
mouse click, it keeps on doing this loop again and again for the same
value of newsgroup (which I don't want). In other words, it does the
[if newsgroup > 0] block each time I do the mouse click (so long as
newsgroup > 0).
The XL wrapper function is calling the function I'm trying to write.

Thanks again for all your attempts to help.

Paul Epstein
 
P

pauldepstein

Exactly how does an event-driven system in C++ manage to jump into the
body of a for-loop when an event happens? I thought events call
functions, they don't jump right in the middle of your code.

Yes, you are right. I think the bug lies somewhere else and that I
can resolve it. I misdiagnosed the reason for the bug.
Educational for me, and I hope not too frustrating for others in the
thread.

Paul Epstein.
 
J

Joel Yliluoma

Thanks a lot for following up. The code posted is as intended. There
is one block ( hence the { } ) inside the for loop. I was
carelessly calling the inner block a loop. I want it to do what's in
the inner block exactly once for each value of the counter.

You mean, when you issue the loop multiple times, it will invoke
the particular code only once for each distinct value of the loop?

I.e.
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
if(something here)
std::cout << b << ',';
}
This would output 5,6,7,8,9,10,11,12,13, and nothing else?

You can try something like this:
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
static std::set<int> seen;
if(seen.find(b) == seen.end())
{
seen.insert(b);
std::cout << b << ',';
}
}
Instead of std::set<>, you can use some other data structure
that fits your purpose better if you are so inclined.
 
P

pauldepstein

You mean, when you issue the loop multiple times, it will invoke
the particular code only once for each distinct value of the loop?

I.e.
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
if(something here)
std::cout << b << ',';
}
This would output 5,6,7,8,9,10,11,12,13, and nothing else?

You can try something like this:
for(int c=0; c<10; ++c)
for(int a=0; a<10; ++a)
for(int b=5; b < 5+a; ++b)
{
static std::set<int> seen;
if(seen.find(b) == seen.end())
{
seen.insert(b);
std::cout << b << ',';
}
}
Instead of std::set<>, you can use some other data structure
that fits your purpose better if you are so inclined.

Thanks for trying to help. I was puzzled as to why a piece of code
didn't work. The intended result was to display a column of data
(call it DATA) which depended on a different set of excel inputs (call
this INPUTS) The intended result was that DATA depended only on
INPUTS. However, each time I hit shift F9 to recalculate XL, DATA
changed even though the INPUTS were clearly constant. But my guesses
as to the reason for the bug were way off at the time of the posting.
I had wrongly assumed that I had introduced the bug. In fact one of
the functions that I used had a bug. I had naively trusted it but
this previous legacy function doesn't work properly at all. The
mistake was to place too much trust in the prior code. By overly
trusting the prior code, I was reduced to desperately bad logic to try
and explain the mysterious errors (as Julia pointed out.) She said it
very well -- "Events don't just jump right in the middle of your
code."

Paul Epstein
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top