using a first time boolean c++ or java
boolean, c++, java
Solution
If you don't have a count already, you can use one instead of a boolean
long counter = 0;
for(String arg: args) {
if(counter++ == 0) {
// first
}
}
An alternative which uses a boolean is
boolean first = true;
for(String arg: args) {
if(first && !(first = false)) {
// first
}
}
For sets there is a similar pattern
Set<String> seen = ...
for(String arg: args) {
if(seen.add(arg)) {
// first time for this value of arg as its only added once.
}
}
Problem
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.