Can I use a FOR within an IF ( a || b || c ...) statement?

Can use something like this,

if (for(i=0; i<60; i++)
{
print(x \|\|)
} 
== "?")

so I can essential do an IF OR check on an array

if ( |i| |i+1| |i+2| |i+3| |i+4| |i+5| |i+6| |i+7| |etc| == "?")
{
// Do this.....
}

I want to check if the array contains a ?

No, for is a statement, not an expression.

I want to check if the array contains a ?

What array? Neither the if statement, the for statement, or the body reference an array.

I want to check if the array contains a ?

Here is one of many ways you can improve on

void setup() {
  Serial.begin (115200);
  char MyArray[] = {'z', '?', 5, 5, 'a' , 8 };
  int count = 0;// put your setup code here, to run once:
  do
  {
    Serial.println(MyArray[count]);
  } while (MyArray[count++] != '?' & count != 10);
  Serial.println(count);
  while (1);
}

void loop() {
  // put your main code here, to run repeatedly:
}

julyjim: I want to check if the array contains a ?

Here is one of many ways you can improve on

 while (1);

what is the purpose of that line?

"Can I use a FOR within an IF ( a || b || c ...) statement?"

Sounds like the perfect recipe for un-maintainable code.

...R

Hi,

One of the standard ways of doing this is similar to this…

//assuming byte array

byte chr=mychar;
int k,k1=-1;

for (k=0;k<10;k++)
{
   if (arry[k]==chr)
   {
      k1=k;
      break;
   }
}

//if k1 is -1 then mychar was not found, if k1 is zero or above
// then k1 is the first element where mychar was found.

if (k1>=0)
  do_stuff(k1);
else
  do_otherstuff(k1);

Something like that anyway.

It's all learning, thanks for the suggestions. C

MrAl:
Hi,

One of the standard ways of doing this is similar to this…

//assuming byte array

byte chr=mychar;
int k,k1=0;

for (k=0;k<10;k++)
{
  if (arry[k]==chr)
  {
      k1=k;
      break;
  }
}

//if k1 is zero then mychar was not found, if k1 is non zero k1 is the first
//element where mychar was found.

if (k1>0)
  do_stuff(k1);
else
  do_otherstuff(0);




Something like that anyway.

But what if the character you’re looking for is the first character in the array? Then it would be element 0. So you can’t use 0 as the “didn’t find it” flag. You’ll have to use -1 for that.

int k,k1=0;

Be careful with lines like that. If that’s at local scope then k doesn’t get initialized like that. While C does allow you to use the comma and create more than one variable on a single line, it is generally better practice to do them one at a time.

sarouje: what is the purpose of that line?

To stop the program execution at known point, used during debugging. Jim

julyjim: To stop the program execution at known point, used during debugging. Jim

Ok but it is a bit redundant no? Where else was the program going but into a for(;;) infinite loop.

denski: Can use something like this,

if ( |i| |i+1| |i+2| |i+3| |i+4| |i+5| |i+6| |i+7| |etc| == "?")
{
// Do this.....
}

No. Do it like this:

boolean foundone = false;
for(loop throug all the stuff) {
  if(this one matches) {
    foundone = true;
    break;
  }
}

if(!foundone) {
  // none of them matched
}
else {
  // at least one matched
}

There are many, many ways to do this, each cleverer than the last. But the method above works.

Oh - if you are searching for a character in a string, use strchr() from the standard string library.

Delta_G:
But what if the character you’re looking for is the first character in the array? Then it would be element 0. So you can’t use 0 as the “didn’t find it” flag. You’ll have to use -1 for that.

int k,k1=0;

Be careful with lines like that. If that’s at local scope then k doesn’t get initialized like that. While C does allow you to use the comma and create more than one variable on a single line, it is generally better practice to do them one at a time.

Hi

Yes, that’s what the standard disclaimer is for and i quote myself:
“Something like that anyway”

That leaves it open for critique as you have done, and have done rather nicely. So nicely that i changed the code to reflect that. k1 should have been initialized to -1 as you noted, and it is an integer so if it is -1 it wont be confused with the possible byte 0xFF because as an int that will be decimal 255 not -1.
So chars 0x00 through 0xFF are possible, although the “Something like that” clause allows for other changes too in order to fit the need.

As far as initialization with int k1=-1; i’ve never had a problem but if you want to recommend that we do instead:

int k1;
k1=-1;

that’s fine with me :slight_smile:

So just to recap, the code was changed to initialize k1 as -1 and the exit value checked to see if it is zero or above as ‘found’ and -1 as ‘not found’.

I’ll still be invoking the “Something like that” disclaimer however so feel free to check the new version over, or even come up with your own version :slight_smile:

There may also be a much more obfuscated C code version that works but is hard to read that is similar to the OP’s original idea. Might be fun to look at some examples.

Delta_G: Ok but it is a bit redundant no? Where else was the program going but into a for(;;) infinite loop.

I do that semiautomaticaly in my code. This time it looked superfluous / stupid but the original was part of some other code. As far as for(;; ) loop - my apps NEVER gets a chance to go there! I like to have control over my code, dodging for(;;) loop, no thanks. Jim .

MrAl: As far as initialization with int k1=-1; i've never had a problem but if you want to recommend that we do instead:

int k1; k1=-1;

No, that's not what I'm talking about. You can do that all in one line. What I was talking about was cramming two variable declarations on one line.

int k, k1 = 0;

In that expression k1 will be initialized to 0. But if that is at local scope the k variable isn't guaranteed to get any specific value and will have whatever garbage was left over in that memory location where it is created.

Which in this case is fine, because it is immediately used as the index in a for loop and inited there.

Which in this case is fine, because it is immediately used as the index in a for loop and inited there.

In which case, the variable declaration really should be part of the for statement, not separate from it. IMHO.

KeithRB: Which in this case is fine, because it is immediately used as the index in a for loop and inited there.

All the more reason to point it out. It's one of those things that works here but breaks later if the code changes to say a while loop instead of a for.

Delta_G: No, that's not what I'm talking about. You can do that all in one line. What I was talking about was cramming two variable declarations on one line.

int k, k1 = 0;

In that expression k1 will be initialized to 0. But if that is at local scope the k variable isn't guaranteed to get any specific value and will have whatever garbage was left over in that memory location where it is created.

Hi,

I dont see your point here at all. You seem to be saying that at the local level we can not declare variables unless we initialize them at the same time. I dont think this is a requirement in C or C++ is it?

What else i cant see is why anyone would declare say: int a,b,c;

and then sometime later think that 'c' is a specific value. If they think that then they cant follow code sequentially top to bottom, which is a requirement for understanding most code.

I know when i declare variables i intend to use them AFTER i load them :-)

Also, the Watcom compiler i have does not allow this in C: for(int k=1;...)

so i am forced to do: int k; for(k=1;...)

but it does allow the former in C++.

Local static variables and global variables are always guaranteed to be initialized to zero. Local variables aren't.

But it is easy for people to see

int x, y = 0;

and think that they'd set both to zero when they really didn't. Same goes with the const keyword.