I usually use a boolean ‘firstTime’ like this:
in C++:
bool firsTime = true;
for (int i = 0; i < v.size(); i++) {
if (firstTime) {
//do something just once
firstTime = false;
}
else {
//do the usual thing
}
}
in java it would be the same using a boolean instead a bool so I don’t put the code.
The questin is, is there anyway in java or c/c++ to use a bool/boolean in an if clause and automatically assign to that bool/boolean the value false?
I know it seems a nonsense but it would save my code A LOT of lines because I’ve a lot of base cases and in big fors or whiles that is critical.
I do want to know if there is anyway to put a value to false after using it in an if clause. I know that in a for or while we can just use:
if (i == 0)
But I was also thinking in calls to functions that needs to know things and that usually is referenced by bools.
If you don’t have a count already, you can use one instead of a boolean
An alternative which uses a boolean is
For sets there is a similar pattern