user3211857
user3211857

Reputation: 117

When to use a while loop or if statement?

Can you give some examples of situations where a while loop and a if loop would be appropriate?

I am working on this project where an Arduino reads an analog input from a variable resistor. This is how I have it read the raw input:

int intputValue = analogRead(A0);

Then I convert the raw input into a number between 0 and 100 for percentage:

double percentValue = inputValue * (1.0/10.23);

So then I use this percentValue to determine whether the Arduino needs to send signal(s) through several of its digital pins. I have signals going to a 4 channel relay module. Basically my idea is that if the percentValue is between 0-25, one of the relays would turn on, hence only one digital pin would need to be activated. Between 26-50, two pins, 51-75, three pins, and 76-100, four pins.

Here's my question: Should I use a if statement:

if(percentValue >=0 && percentValue <=25){
   digitalWrite(pin1, HIGH);      //turns on pin 1
}

Or use a while loop:

while(percentValue >= 0 && percentValue <=25){
   digitalWrite(pin1, HIGH);       //turns on pin 1
}

Then I'm going to do a similar thing for the rest of the percentValue ranges. Is there a difference between using "if" and "while" in this case?

Thanks for any help.

Upvotes: 0

Views: 7744

Answers (3)

SemperAmbroscus
SemperAmbroscus

Reputation: 1388

While loops are used to run a specific code block as long as certain parameters are met. An if statement is similar but it will only run the said code block once but a while statement will run until told otherwise.

So effectively:

while(1 == 1)
{
    System.out.println("Hello World");
}

Will print Hello World indefinitely. On the other hand:

if(1 == 1)
{
    System.out.println("Hello World");
}

Will print Hello World once.

Just for fun since your understanding of loops is shady; a for loop will run a specified number of times:

for(int i = 0; i < 3; i++)
{
    System.out.println("Hello World");
}

Would print Hello World 3 times.

refer to:

While loop

For loop

If statement

General Java Tutorials

Upvotes: 1

UncleO
UncleO

Reputation: 8449

"Then I'm going to do a similar thing for the rest of the percentValue ranges."

This implies you should use an if statement, and not a while loop, especially if you want to do anything else with the device.

Presumably, this code will be placed in the Arduino loop() function, which is called repeatedly, giving you a loop. You don't want the Arduino to get stuck in a while loop of your own.

It appears that you want to light up different LEDs depending on the reading. You will want to turn off the other LEDs in the body of your if statements as well. Otherwise, the Arduino will just eventually have all 4 LEDs lit up.

if(percentValue >=0 && percentValue <=25){
   digitalWrite(pin1, HIGH);      //turns on pin 1
   digitalWrite(pin2, LOW);
   digitalWrite(pin3, LOW);
   digitalWrite(pin4, LOW); 
}
// etc.

Upvotes: 0

Fei Zheng
Fei Zheng

Reputation: 141

There should be a setup and loop function in your code, you can put if in your loop function.

void setup() {
  // put your setup code here, to run once:
  int intputValue = analogRead(A0);
}

void loop() {
  // put your main code here, to run repeatedly: 
  double percentValue = inputValue * (1.0/10.23);
  if(percentValue >= 0 && percentValue <= 25){
     digitalWrite(pin1, HIGH);      //turns on pin 1
  }
}

Upvotes: 1

Related Questions