Miquel Coll
Miquel Coll

Reputation: 771

using a first time boolean c++ or java

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.

Upvotes: 3

Views: 2855

Answers (11)

paquetp
paquetp

Reputation: 1651

You could try (C++):

bool checkFirst(bool& first)
{
  bool result = first;
  first = false;
  return result;
}

bool firstTime=true;
// loop here { 
if (checkFirst(firstTime))
{
  // do firsttime stuff
} else {
 // do regular stuff
}
// } // end loop

Java:

public class CheckFirst
{
  private boolean first;

  public CheckFirst()
  {
    first = true;
  }

  boolean check()
  {
    boolean result = first;
    first = false;
    return result;
  }
}

CheckFirst first = new CheckFirst();
// loop here {
if (first.check()) {
  // do first stuff
} else {
  // do regular stuff
}
// } // end loop

Upvotes: 0

Ross Larson
Ross Larson

Reputation: 2437

Edit: See Luchian's edit about using what you have. I agree. It is very clear.

Why not just pull the first loop iteration out of the loop?

//do something just once (if needed, so check)
for (int i = 1; i < v.size(); i++) {
    //do the usual thing
}

Now if there is some chance that you may not do this first thing you would need to check for that, but a lot of times you will probably at least do one iteration.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490398

If the first iteration should just have some extra initialization in addition to the usual "stuff" (rather than "instead of the usual stuff"), I'd consider something like this:

for (int i=0, do_first_time_stuff(); i<v.size(); i++) {
    do_other_stuff();
}

Another possibility would be something like:

Initialization();
std::for_each(v.begin()+1, v.end(), loop_body);

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533680

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.
    }
}

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726839

There is a C++ - specific solution that uses conversion operator to build a single-use guard:

class first_guard {
    bool val;
public:
    first_guard() : val(true) {}
    operator bool() { bool res = val; val = false; return res; }
};

Here is how you use the guard:

first_guard first;
for (int i = 0 ; i != 5 ; i++) {
    if(!first) {
        cout << i << ' ';
    }
}

This code prints 1 2 3 4, skipping 0.

Lack of conversion operators prevents you from replicating this solution in Java.

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258618

I'd just change the condition to

if ( i == 0 )
{ 
}
//...

and get rid of the extra variable altogether.

EDIT After seeing all suggestions on how to do this in one line, I can only come to one conclusion - DON'T. Just use what you have, it's much much clearer.

Upvotes: 7

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

I would try something like this,it works, but I'm not sure if it's totally the right way to go.( http://ideone.com/Bcjva )

boolean boo = true;
if( !((boo) ? (boo = !boo) : boo)) //enters the if successfully, but inside,prints boo as false.
     System.out.println(boo);

Upvotes: 0

Akhi
Akhi

Reputation: 2242

If you are not using the contents of the list in your first time activity.

if(v.size() >=1 ){
  //do Something for 1 time
}
for (int i = 0; i < v.size(); i++) {
        //do the usual thing
}

If you need the content of the list then use

if ( i == 0 )
{ 
}

as suggested by Luchian Grigore

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182829

for (int i = 0, firstTime = 1; i < v.size(); i++, firstTime = 0) {

Upvotes: 2

tdgs
tdgs

Reputation: 1096

why not do something like:

void firstTime() {
    // do stuff
}

void doWork(int i) {
    // do stuff
}

firstTime();
for(int i = 1; i<v.size();i++) {
 doWork(i);
}

Upvotes: 0

Michael
Michael

Reputation: 1239

I would just do whatever you are trying to do the first time outside of the for loop, then start the iterator one up.

if (v.size() > 0) {
    /* first time code */
}
for (int i = 1; i < v.size(); i++) {
    /* second time or more */
}

If you're using a foreach loop, your solution is pretty solid.

Upvotes: 0

Related Questions