Endless use for the cycle continues
I am writing code that will run through every bit in the histogram and check if there are non-zero bins. If so, it issues an error message. My problem is that I want it to skip bin because that bin shouldn't be empty, but check all other cells.
This alone creates an endless loop. Here's my code
Int_t y;
for (int i = 0; i <= 100; i++) {
y = hist - > GetBinContent(i)
if (i = 1) continue;
else if (y != 0) {
std: cout << * * * * * ERROR * * * * * << std: endl;
break;
}
}
What happens is it evaluates it for i = 0
, skips i = 1
it and then deletes it i = 2
and just continually evaluates it over and over. If I choose "if (i = 1) continue;" line then it works.
Any ideas?
source to share
When you have bugs with loops, it sometimes helps to run it in debug mode with a breakpoint in the loop, or put a print expression in it. Your error comes from a string: if (i=1) continue;
. The segment i=1
sets i to one and returns i, which is interpreted as true (since it is nonzero). It then moves on to the next iteration where it sets the i to one again. Most likely it was if (i==1) continue;
. This does the comparison operator you intended.
source to share