The difference between while and if statements.

I have seen some places say you can use while and if statements interchangeably, while others disagree. Is there a difference between while and if statements and if there is, what is it. I have googled it and looked on the reference page, but there are so many disagreeing answers online, I don't know what to believe. :|

A while statement is a loop: Keep doing this action until this thing happens.

An if statement is a decision: Do this action if this happens, or do this other action. Each action will only happen once.

They are not interchangeable.

I have seen some places say you can use while and if statements interchangeably,

Where?

Pete

for (x=0; x<10; x=x+1){
//do this 10 times
Serial.println(x);
}
x=0;
while (x<10){
//do this 10 times
Serial.println(x);
x=x+1;
}

Won’t these have the same result?

Where?

In an arduino book I have called "Arduino Cookbook" it says while and if statements are interchangeable in most cases. Also in several websites online i have read the same. I do not remember the websites though.

2 bytes different here. These are sort of equivalent:

  while(millis() - lastTime >= DELAY)
  {
    lastTime += DELAY;
    digitalWrite(led,!digitalRead(led)); 
  }
//==========================================
    if (millis() - lastTime >= DELAY)
  {
    lastTime += DELAY;
    digitalWrite(led,!digitalRead(led)); 
  }

Crossroads: That is a for and a while, not an if and a while.

2007jingz: Are you sure you are not confusing for and while, and not if and while?

In any case, the matter is academic, you use the construct that does what you want and expresses it clearly, so that it will be easily understood next year when you come back to it.

Are you sure you are not confusing for and while, and not if and while?

In any case, the matter is academic, you use the construct that does what you want and expresses it clearly, so that it will be easily understood next year when you come back to it.

I am sure the book says if and while.

while(millis() - lastTime >= DELAY) { lastTime += DELAY; digitalWrite(led,!digitalRead(led)); } //========================================== if (millis() - lastTime >= DELAY) { lastTime += DELAY; digitalWrite(led,!digitalRead(led)); }

No those if and while statements would not be the same there. The if statement would only run once unless it was in the void loop() function. If it was in another function or in the loop function with any other statements, it would not work as would the while loop.

Ah - the mind see what it expects to see sometimes!

Yes, while to test and do a repetitive thing, and if to test and do a one time thing, generally.

Both can be mangled to be used, in a similar manner to what I did above.

those if and while statements would not be the same there.

They do the same thing in this case.

looking at this:

while(1)
{
  //Do this
{

and this:

if (1)
{
  //Do this
}

they do not look the same to me

Agreed, the first would get stuck in the { } code,
while the 2nd would do it once and then move on.

Could add the dreaded GOTO and a Label and make the 2nd get stuck also.

Sometimes you'll see

if (Serial.available () > 0)
{
  while (Serial.available () > 0)
  {
    // consume serial characters
  }
}

Here, the "if" is simply redundant, but no way are they interchangeable.

LarryD:

those if and while statements would not be the same there.

They do the same thing in this case.

They only would do the same thing if the statement was in the void loop function and the loop function had no delays. The if statement would only run once every time the loop function ran. The while statement would run until it was not true.

CrossRoads: Agreed, the first would get stuck in the { } code, while the 2nd would do it once and then move on.

Could add the dreaded GOTO and a Label and make the 2nd get stuck also.

What is bad about goto and labels. I use them often without any issues.

What is bad about goto and labels.

Have you heard of spaghetti code?

2007jingz:

CrossRoads: Agreed, the first would get stuck in the { } code, while the 2nd would do it once and then move on.

Could add the dreaded GOTO and a Label and make the 2nd get stuck also.

What is bad about goto and labels. I use them often without any issues.

Consider yourself shunned ]:D

The only thing I can think of is if you are in your void loop. if you had

void loop(){
  if(1){
    //do something
  }
  else{
    //do something else
  }
}
void loop(){
  while(1){
    //do something
  }
}

However if it were in say a main function I do not see how they could be interchangeable at all. I am sure there are ways to make some code so they do the same thing but switching while for if will not result in the same thing most of the time.

edit: was missing a bracket

They do the same thing in this case.

The while statement would run until it was not true.

With this line, it makes it not true immediately. lastTime += DELAY;

2007jingz: "Arduino Cookbook" it says while and if statements are interchangeable in most cases.

In which section? I just gave the if & while parts a cursory glance & couldn't see any such thing.