Tutorial.IfStatement History

Hide minor edits - Show changes to markup

May 31, 2010, at 11:38 PM by Equipo Traduccion -
Changed lines 1-6 from:

Examples > Control Structures

If Statement (Conditional Statement)

The If statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

to:

Ejemplos > Estructuras de control

Declaración If (estructura condicional)

La declaración If es la estructura de control más básica de todas. Te permite hacer algo o no sólo si se cumple una condición (si es verdadera). Se escribe así:

Changed line 8 from:
   // do stuff if the condition is true
to:
   // hace cosas si la condición es verdad
Changed lines 12-13 from:

There is a common variation called if-else that looks like this:

to:

Existe una variación llamada if-else que se escribe:

Changed line 16 from:
   // do stuff if the condition is true
to:
   // hace cosas si la condición es cierta
Changed line 18 from:
   // do stuff if the condition is false
to:
   // hace cosas si la condición es falsa
Changed lines 22-23 from:

There's also the else-if, where you can check a second condition if the first is false:

to:

Todavía queda el else-if, en la que se comprueba una segunda condición en caso de que la primera sea falsa:

Changed line 26 from:
   // do stuff if the condition is true
to:
   // hace cosas si la condición es cierta
Changed lines 28-29 from:
   // do stuff only if the first condition is false
   // and the second condition is true
to:
   // hace cosas sólo si la primera condición es falsa
   // y la segunda es cierta
Changed lines 33-39 from:

You'll use if statements all the time. The example below turns on an LED on pin 13 (the built-in LED on many Arduino boards) if the value read on an analog input goes above a certain threshold.

Circuit

click the image to enlarge

to:

Usarás la declaración If todo el tiempo. El ejemplo siguiente enciende un LED conectado al pin 13 (el LED incorporado en la placa en muchos Arduinos) si el valor leído en la entrada analógica supera cierto valor.

Circuito

click en la imagen para aumentar

Changed lines 42-48 from:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

click the image to enlarge

to:

imagen creada usando Fritzing. Para más ejemplos de circuitos, ver la página del proyecto Fritzing

Esquema:

click en la imagen para aumentar

Changed line 53 from:

Code

to:

Código

February 24, 2010, at 04:05 AM by Tom Igoe -
Changed line 57 from:
to:
February 24, 2010, at 04:04 AM by Tom Igoe -
Changed lines 57-84 from:
 /*
   Conditionals - If statement
  
  This example demonstrates the use of if() statements.
  It reads the state of a potentiometer (an analog input) and turns on an LED
  only if the LED goes above a certain threshold level. It prints the analog value
  regardless of the level.
  
  The circuit:
  * potentiometer connected to analog pin 0.
  Center pin of the potentiometer goes to the analog pin.
  side pins of the potentiometer go to +5V and ground
  * LED connected from digital pin 13 to ground
  
  * Note: On most Arduino boards, there is already an LED on the board
  connected to pin 13, so you don't need any extra components for this example.
  
  created 17 Jan 2009
  by Tom Igoe
  
   http://arduino.cc/en/Tutorial/IfStatement
  
  */

 // These constants won't change:
 const int analogPin = 0;     // pin that the sensor is attached to
 const int ledPin = 13;       // pin that the LED is attached to
 const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input
to:
Deleted lines 58-82:
 void setup() {
   // initialize the LED pin as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communications:
   Serial.begin(9600);
 }

 void loop() {
   // read the value of the potentiometer:
   int analogValue = analogRead(analogPin);

   // if the analog value is high enough, turn on the LED:
   if (analogValue > threshold) {
     digitalWrite(ledPin, HIGH);
   } 
   else {
     digitalWrite(ledPin,LOW); 
   }

   // print the analog value:
   Serial.println(analogValue, DEC);

 }

October 18, 2009, at 09:41 PM by David A. Mellis -
Changed line 88 from:
   pinMode(LED, OUTPUT);
to:
   pinMode(ledPin, OUTPUT);
August 27, 2009, at 08:47 PM by Tom Igoe -
Added lines 43-45:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:25 PM by Tom Igoe -
Changed lines 52-55 from:

[@ /*

  Conditionals - If statement
to:
Changed lines 83-86 from:
 This example demonstrates the use of if() statements.
 It reads the state of a potentiometer (an analog input) and turns on an LED
 only if the LED goes above a certain threshold level. It prints the analog value
 regardless of the level.
to:
 void setup() {
   // initialize the LED pin as an output:
   pinMode(LED, OUTPUT);
   // initialize serial communications:
   Serial.begin(9600);
 }
Changed lines 90-94 from:
 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 13 to ground
to:
 void loop() {
   // read the value of the potentiometer:
   int analogValue = analogRead(analogPin);
Changed lines 94-95 from:
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
to:
   // if the analog value is high enough, turn on the LED:
   if (analogValue > threshold) {
     digitalWrite(ledPin, HIGH);
   } 
   else {
     digitalWrite(ledPin,LOW); 
   }
Changed lines 102-103 from:
 created 17 Jan 2009
 by Tom Igoe
to:
   // print the analog value:
   Serial.println(analogValue, DEC);
Changed line 105 from:
  http://arduino.cc/en/Tutorial/IfStatement
to:
 }
Deleted line 106:
 */
Changed lines 108-136 from:

// These constants won't change: const int analogPin = 0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input

void setup() {

  // initialize the LED pin as an output:
  pinMode(LED, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);

}

void loop() {

  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);

} @]

to:
June 25, 2009, at 09:36 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:35 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:32 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:31 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:30 PM by Tom Igoe -
Changed lines 47-50 from:

to:

June 25, 2009, at 09:30 PM by Tom Igoe -
June 25, 2009, at 09:15 PM by Tom Igoe -
Added lines 1-106:

Examples > Control Structures

If Statement (Conditional Statement)

The If statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

if (someCondition) {
   // do stuff if the condition is true
}

There is a common variation called if-else that looks like this:

if (someCondition) {
   // do stuff if the condition is true
} else {
   // do stuff if the condition is false
}

There's also the else-if, where you can check a second condition if the first is false:

if (someCondition) {
   // do stuff if the condition is true
} else if (anotherCondition) {
   // do stuff only if the first condition is false
   // and the second condition is true
}

You'll use if statements all the time. The example below turns on an LED on pin 13 (the built-in LED on many Arduino boards) if the value read on an analog input goes above a certain threshold.

Circuit

click the image to enlarge

Schematic:

click the image to enlarge

Code

/*
  Conditionals - If statement

 This example demonstrates the use of if() statements.
 It reads the state of a potentiometer (an analog input) and turns on an LED
 only if the LED goes above a certain threshold level. It prints the analog value
 regardless of the level.

 The circuit:
 * potentiometer connected to analog pin 0.
 Center pin of the potentiometer goes to the analog pin.
 side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 13 to ground

 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.

 created 17 Jan 2009
 by Tom Igoe

  http://arduino.cc/en/Tutorial/IfStatement

 */

// These constants won't change:
const int analogPin = 0;     // pin that the sensor is attached to
const int ledPin = 13;       // pin that the LED is attached to
const int threshold = 400;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  // initialize the LED pin as an output:
  pinMode(LED, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }

  // print the analog value:
  Serial.println(analogValue, DEC);

}

Share