473,414 Members | 1,667 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,414 software developers and data experts.

basic static variables question

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

Nov 19 '07 #1
13 1200
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?

--
Sohail Somani
http://uint32t.blogspot.com
Nov 19 '07 #2
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?

--
Sohail Somanihttp://uint32t.blogspot.com
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
Nov 19 '07 #3
pa**********@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
>On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?

--
Sohail Somanihttp://uint32t.blogspot.com

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
Nov 19 '07 #4
On Nov 19, 5:47 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
pauldepst...@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?
--
Sohail Somanihttp://uint32t.blogspot.com
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- 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
Nov 19 '07 #5
On Nov 19, 5:47 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
pauldepst...@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?
--
Sohail Somanihttp://uint32t.blogspot.com
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- 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
Nov 19 '07 #6
pa**********@att.net wrote:
>>for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}
}
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.
Nov 19 '07 #7
On Nov 19, 6:10 pm, Juha Nieminen <nos...@thanks.invalidwrote:
pauldepst...@att.net wrote:
>for (int counter = 0; counter < 100; ++ counter) {
{ static int blah = 0;
if (blah == 0)
// do something;
blah ++;
}
}
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.
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
Nov 19 '07 #8
pa**********@att.net wrote:
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;
}
}
Nov 19 '07 #9
pa**********@att.net wrote:
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.
Nov 19 '07 #10
On Nov 19, 6:28 pm, Juha Nieminen <nos...@thanks.invalidwrote:
pauldepst...@att.net wrote:
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;
}

}- 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<doubleSpamIsAnnoying = 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

Nov 19 '07 #11
On Nov 19, 6:32 pm, Juha Nieminen <nos...@thanks.invalidwrote:
pauldepst...@att.net wrote:
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.
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.
Nov 19 '07 #12
On Mon, 19 Nov 2007 01:03:29 -0800 (PST), pa**********@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
>On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?

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<intseen;
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.

--
Joel Yliluoma - http://iki.fi/bisqwit/
Nov 19 '07 #13
On Nov 19, 10:41 pm, Joel Yliluoma <bisq...@iki.fiwrote:
On Mon, 19 Nov 2007 01:03:29 -0800 (PST), pauldepst...@att.net wrote:
On Nov 19, 4:55 pm, Sohail Somani <soh...@taggedtype.netwrote:
On Mon, 19 Nov 2007 00:44:46 -0800, pauldepstein wrote:
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?
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<intseen;
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.

--
Joel Yliluoma -http://iki.fi/bisqwit/- Hide quoted text -

- Show quoted text -
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
Nov 20 '07 #14

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
2
by: Jimmy | last post by:
This is a basic question. Does Page related member variable values only be set in the Page they are executed? Example code like below public partial class _Default : System.Web.UI.Page { string...
2
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
0
by: Luis Zarrabeitia | last post by:
Quoting Joe Strout <joe@strout.net>: I'm sure your credentials are bigger than mine. But he is right. A lot of languages have ditched the "concept" of a static variable on a method (how do you...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
0
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...

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.