Reference.If History

Hide minor edits - Show changes to markup

May 31, 2010, at 10:58 PM by Equipo Traduccion -
Changed lines 47-49 from:

if también puede ser parte de una estructura de control de ramificación usando la construcción if...else].

to:

if también puede ser parte de una estructura de control de ramificación usando la construcción if...else.

May 31, 2010, at 10:57 PM by Equipo Traduccion -
Changed lines 43-46 from:

Ten cuidado de no usar un signo de igual sólo (ej. if (x = 10) ). Un signo de igual sólo es el operador que indica la asignación de un valor, y va a asignar 10 a x. En su lugar usa el signo de igual doble (ej. if (x == 10) ), el cual es el operador de comparación, y compryeba si x equivale a 10 o no. El último ejemplo sólo da true si x equivale a 10, pero el ejemplo anterior (con un sólo símbolo =) dará siempre true.

Esto es porque C evalua la declaración if (x=10) de la siguiente manera: 10 es asignado a x (Recuerda que un signo = sólo, es el operador de asignación), por lo tanto x ahora contiene 10. Entonces el condicional 'if' evalúa 10, el cual siempre resulta TRUE, debido a que cualquier valor numérico mayor a 0 es evaluado como TRUE. Consecuentemente, if (x = 10) siempre será evaluado como TRUE, lo cual no es el resultado deseado cuando se usa una declaración if. Adicionalmente, la variable x será definida en 10, lo que tampoco es una acción deseada.

to:

Ten cuidado de no usar un signo de igual solo (ej. if (x = 10) ). Un signo de igual solo es el operador que indica la asignación de un valor, y va a asignar 10 a x. En su lugar usa el signo de igual doble (ej. if (x == 10) ), el cual es el operador de comparación, y compryeba si x equivale a 10 o no. El último ejemplo sólo da true si x equivale a 10, pero el ejemplo anterior (con un sólo símbolo =) dará siempre TRUE.

Esto es porque C evalúa la declaración if (x=10) de la siguiente manera: 10 es asignado a x (Recuerda que un signo = solo, es el operador de asignación), por lo tanto x ahora contiene 10. Entonces el condicional if evalúa 10, el cual siempre resulta TRUE, debido a que cualquier valor numérico mayor a 0 es evaluado como TRUE. Consecuentemente, if (x = 10) siempre será evaluado como TRUE, lo cual no es el resultado deseado cuando se usa una declaración if. Adicionalmente, la variable x será definida en 10, lo que tampoco es una acción deseada.

May 31, 2010, at 10:47 PM by Equipo Traduccion - Necesita una segunda opinión, sugiero una traducción menos literal del inglés.
Changed lines 1-5 from:

if (conditional) and ==, !=, <, > (comparison operators)

if, which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

[@if (someVariable > 50)

to:

if (condicional) y ==, !=, <, > (operadores de comparación)

if, el cual puede ser usado en conjunto con uno o más operadores de comparación, comprueba si cierta condición se cumple, por ejemplo, si un input posee un valor mayor a cierto número. El formato para una comprobación if es el siguiente:

[@if (algunaVariable > 50)

Changed line 7 from:
  // do something here
to:
  // hacer algo aquí.
Changed lines 11-13 from:

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code.

The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.

to:

Este programa comprueba si la variable algunaVariable es mayor a 50. Si lo es, el programa toma una acción particular. Dicho de otra forma, si la declaración escrita dentro de los paréntesis es verdadera (true), el código dentro de las llaves se ejecutará. Sino, el programa ignora dicho código.

Las llaves pueden ser omitidas luego de una declaración if. De hacer esto, la siguiente línea (definida por el punto y coma) será la única afectada por la condición.

Changed lines 27-29 from:

} // all are correct

to:

}

// todos los ejemplos son correctos.

Changed lines 32-49 from:

The statements being evaluated inside the parentheses require the use of one or more operators:

Comparison Operators:

 x == y (x is equal to y)
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x >  y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y)

Warning:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

if can also be part of a branching control structure using the if...else] construction.

to:

Las declaraciones a evaluar dentro de los paréntesis, requieren el uso de uno o más operadores:

Operadores de Comparación:

 x == y (x es igual a y)
 x != y (x no es igual a y)
 x <  y (x es menor a y)  
 x >  y (x es mayor a y) 
 x <= y (x es menor o igual a y) 
 x >= y (x es mayor o igual a y)

Atención:

Ten cuidado de no usar un signo de igual sólo (ej. if (x = 10) ). Un signo de igual sólo es el operador que indica la asignación de un valor, y va a asignar 10 a x. En su lugar usa el signo de igual doble (ej. if (x == 10) ), el cual es el operador de comparación, y compryeba si x equivale a 10 o no. El último ejemplo sólo da true si x equivale a 10, pero el ejemplo anterior (con un sólo símbolo =) dará siempre true.

Esto es porque C evalua la declaración if (x=10) de la siguiente manera: 10 es asignado a x (Recuerda que un signo = sólo, es el operador de asignación), por lo tanto x ahora contiene 10. Entonces el condicional 'if' evalúa 10, el cual siempre resulta TRUE, debido a que cualquier valor numérico mayor a 0 es evaluado como TRUE. Consecuentemente, if (x = 10) siempre será evaluado como TRUE, lo cual no es el resultado deseado cuando se usa una declaración if. Adicionalmente, la variable x será definida en 10, lo que tampoco es una acción deseada.

if también puede ser parte de una estructura de control de ramificación usando la construcción if...else].

August 20, 2009, at 04:52 AM by Paul Badger -
Changed lines 16-17 from:

if (x > 120) digitalWrite(LEDpin, HIGH);

to:

if (x > 120) digitalWrite(LEDpin, HIGH);

Changed lines 21-26 from:

if (x > 120){ digitalWrite(LEDpin, HIGH); } // all are correct

to:

if (x > 120){ digitalWrite(LEDpin, HIGH); }

if (x > 120){

  digitalWrite(LEDpin1, HIGH);
  digitalWrite(LEDpin2, HIGH); 

} // all are correct

February 27, 2009, at 06:30 PM by Paul Badger -
Changed line 21 from:

if (x > 120) {digitalWrite(LEDpin, HIGH);} // all are correct

to:

if (x > 120){ digitalWrite(LEDpin, HIGH); } // all are correct

September 06, 2008, at 08:23 PM by Paul Badger -
Changed lines 37-38 from:

This is because C evaluates the statement if (x=10) as follows: 10 is 'assigned' to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

to:

This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

September 06, 2008, at 08:21 PM by Paul Badger -
Changed lines 37-38 from:

This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

to:

This is because C evaluates the statement if (x=10) as follows: 10 is 'assigned' to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

September 06, 2008, at 07:04 PM by Paul Badger -
Changed lines 1-2 from:

if (conditional) and ==, !=, <, > (comparison operators)

to:

if (conditional) and ==, !=, <, > (comparison operators)

September 06, 2008, at 07:01 PM by Paul Badger -
Changed lines 1-2 from:

if (conditional) and ==, !=, <, > (comparison operators)

to:

if (conditional) and ==, !=, <, > (comparison operators)

September 06, 2008, at 07:01 PM by Paul Badger -
Changed lines 1-2 from:

if (conditional) &nbsp and ==, !=, <, > (comparison operators)

to:

if (conditional) and ==, !=, <, > (comparison operators)

September 06, 2008, at 07:01 PM by Paul Badger -
Changed lines 1-2 from:

if (conditional) and ==, !=, <, > (comparison operators)

to:

if (conditional) &nbsp and ==, !=, <, > (comparison operators)

September 06, 2008, at 06:59 PM by Paul Badger -
Changed lines 1-2 from:

if` (conditional) ` and ==, ` !=, ` <,` > (comparison operators)

to:

if (conditional) and ==, !=, <, > (comparison operators)

September 06, 2008, at 06:58 PM by Paul Badger -
Changed lines 1-2 from:

if ` (conditional) ` and ==, ` !=, ` <,` > (comparison operators)

to:

if` (conditional) ` and ==, ` !=, ` <,` > (comparison operators)

September 06, 2008, at 06:57 PM by Paul Badger -
Changed lines 1-2 from:

if `(conditional) `and ==, `!=, `<,` > (comparison operators)

to:

if ` (conditional) ` and ==, ` !=, ` <,` > (comparison operators)

September 06, 2008, at 06:57 PM by Paul Badger -
Changed lines 1-2 from:

if (conditional) and ==, !=, <, > (comparison operators)

to:

if `(conditional) `and ==, `!=, `<,` > (comparison operators)

September 06, 2008, at 02:44 AM by Paul Badger -
Changed lines 1-2 from:

if, comparison operators

to:

if (conditional) and ==, !=, <, > (comparison operators)

September 06, 2008, at 02:42 AM by Paul Badger -
Changed lines 1-2 from:

if

to:

if, comparison operators

September 06, 2008, at 02:41 AM by Paul Badger -
Changed lines 3-4 from:

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

to:

if, which is used in conjunction with a comparison operator, tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

September 06, 2008, at 02:40 AM by Paul Badger -
September 06, 2008, at 02:40 AM by Paul Badger -
Changed line 26 from:

Operators:

to:

Comparison Operators:

September 06, 2008, at 02:34 AM by Paul Badger -
Changed lines 35-36 from:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10. Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

to:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

April 09, 2008, at 01:32 AM by Paul Badger -
Changed lines 37-38 from:

This is because C evaluates if (x=10) is as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement.

to:

This is because C evaluates the statement if (x=10) as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

September 23, 2007, at 02:57 PM by Paul Badger -
Changed lines 35-36 from:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, and sets x to 10. Instead use the double equal sign, == (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

to:

Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the assignment operator, and sets x to 10. Instead use the double equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

September 23, 2007, at 02:54 PM by Paul Badger -
Changed lines 35-38 from:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, setting x to 10. Instead use the double equal sign, == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

This is because C evaluates if (x=10) is as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement.

to:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, and sets x to 10. Instead use the double equal sign, == (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

This is because C evaluates if (x=10) is as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement.

September 23, 2007, at 02:52 PM by Paul Badger -
Changed lines 34-36 from:

Coding Warning:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

to:

Warning:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, setting x to 10. Instead use the double equal sign, == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true.

This is because C evaluates if (x=10) is as follows: 10 is assigned to x, so x now contains 10. Then the 'if' conditional evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using an 'if' statement.

September 23, 2007, at 02:47 PM by Paul Badger -
Changed lines 35-36 from:

Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

to:

Beware of accidentally using the single equal sign '='(e.g. if (x = 10) ), which is the assignment operator, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

September 23, 2007, at 02:44 PM by Paul Badger -
Changed line 13 from:

The brackets may be omitted after an if statement. If this is done the next line (defined by the semicolon) becomes the only conditional statement.

to:

The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.

September 23, 2007, at 02:43 PM by Paul Badger -
Changed lines 16-17 from:

if (x > 120) digitalWrite(LEDpin, HIGH); if (x > 120) {digitalWrite(LEDpin, HIGH);} // both are correct

to:

if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120) {digitalWrite(LEDpin, HIGH);} // all are correct

September 23, 2007, at 02:41 PM by Paul Badger -
Changed line 13 from:

It is often convenient to use a single line for a compact conditional test, and reaction to the test. In this case, the brackets may be ommited although they may add clarity for beginning programmers:

to:

The brackets may be omitted after an if statement. If this is done the next line (defined by the semicolon) becomes the only conditional statement.

June 12, 2007, at 06:49 AM by Paul Badger -
Changed lines 35-38 from:

See also

If ... Else

to:
June 12, 2007, at 06:48 AM by Paul Badger -
Changed lines 35-38 from:
to:

See also

If ... Else

June 12, 2007, at 06:44 AM by Paul Badger -
Changed lines 30-31 from:

Coding Warning: Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

to:

Coding Warning:

Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

May 26, 2007, at 02:36 PM by Paul Badger -
Added lines 11-12:

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code.

Changed line 16 from:

if (x > 120) digitalWrite(LEDpin, HIGH); // both are correct

to:

if (x > 120) digitalWrite(LEDpin, HIGH);

Deleted lines 19-20:

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code.

May 26, 2007, at 02:33 PM by Paul Badger -
Changed lines 16-17 from:
to:

@]

May 26, 2007, at 02:33 PM by Paul Badger -
Added lines 10-16:

It is often convenient to use a single line for a compact conditional test, and reaction to the test. In this case, the brackets may be ommited although they may add clarity for beginning programmers: [@

if (x > 120) digitalWrite(LEDpin, HIGH); // both are correct if (x > 120) {digitalWrite(LEDpin, HIGH);} // both are correct

April 17, 2007, at 12:02 AM by David A. Mellis -
Changed line 7 from:
  # do something here
to:
  // do something here
April 16, 2007, at 05:52 PM by Paul Badger -
Changed lines 22-23 from:

Coding Warning: Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

to:

Coding Warning: Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable, as a (probably unwanted) side-effect.

April 16, 2007, at 05:51 PM by Paul Badger -
Changed lines 1-2 from:

if

to:

if

April 16, 2007, at 04:14 PM by Paul Badger -
Changed lines 26-29 from:
to:
March 31, 2006, at 04:58 AM by Tom Igoe -
Changed lines 22-23 from:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

to:

Coding Warning: Beware of accidently using = (e.g. if (x = 10) ), which sets a variable, instead of using == (e.g. if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

March 31, 2006, at 04:55 AM by Tom Igoe -
Changed lines 22-23 from:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is set if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

to:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is true if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

March 31, 2006, at 04:38 AM by Tom Igoe -
Changed line 18 from:
 x > y (x is greater than y) 
to:
 x >  y (x is greater than y) 
March 31, 2006, at 04:38 AM by Tom Igoe -
Changed lines 15-21 from:
  • x == y (x is equal to y)
  • x != y (x is not equal to y)
  • x < y (x is less than y)
  • x > y (x is greater than y)
  • x <= y (x is less than or equal to y)
  • x >= y (x is greater than or equal to y)
to:
 x == y (x is equal to y)
 x != y (x is not equal to y)
 x <  y (x is less than y)  
 x > y (x is greater than y) 
 x <= y (x is less than or equal to y) 
 x >= y (x is greater than or equal to y)
March 31, 2006, at 04:37 AM by Tom Igoe -
Changed lines 22-26 from:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not.

The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is set if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

to:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not. The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is set if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

if can also be part of a branching control structure using the if...else] construction.

March 31, 2006, at 04:36 AM by Tom Igoe -
Changed lines 15-21 from:
  • x == y(x is equal to y)
  • x != y (x is not equal to y)
  • x < y (x is less than y)
  • x > y (x is greater than y)
  • x <= y (x is less than or equal to y)
  • x >= y (x is greater than or equal to y)
to:
  • x == y (x is equal to y)
  • x != y (x is not equal to y)
  • x < y (x is less than y)
  • x > y (x is greater than y)
  • x <= y (x is less than or equal to y)
  • x >= y (x is greater than or equal to y)
March 31, 2006, at 04:35 AM by Tom Igoe -
Changed lines 15-21 from:
  • == (equals to)
  • != (not equals)
  • < (less than)
  • > (grater than)
  • <= (less than or equals)
  • >= (greater than or equals)
to:
  • x == y(x is equal to y)
  • x != y (x is not equal to y)
  • x < y (x is less than y)
  • x > y (x is greater than y)
  • x <= y (x is less than or equal to y)
  • x >= y (x is greater than or equal to y)
March 31, 2006, at 04:33 AM by Tom Igoe -
Changed lines 10-11 from:

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in brackets is true, the statements inside the brackets are run. If not, the program skips over the code.

to:

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in parentheses is true, the statements inside the brackets are run. If not, the program skips over the code.

March 31, 2006, at 04:32 AM by Tom Igoe -
Changed lines 3-4 from:

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

to:

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

March 31, 2006, at 04:32 AM by Tom Igoe -
Changed lines 1-4 from:

if statements

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

to:

if

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

March 31, 2006, at 04:31 AM by Tom Igoe -
Changed lines 3-12 from:

This helps the control and flow of the programs, used to check if condition has been reached, the computer determines whether the expression (in the brackets) is true or false. If true the statements (inside the curly brackets) are executed, if not, the program skips over the code.

To test for equality is ==

A warning: Beware of using = instead of ==, such as writing accidentally

   if ( i = j ) ..... 

This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in "j" into "i", and delivers this value, which will then be interpreted as TRUE if j is non-zero. This is called assignment by value -- a key feature of C.

to:

if tests whether a certain condition has been reached, such as an input being above a certain number. The format for an if test is:

if (someVariable > 50)
{
  # do something here
}

The program tests to see if someVariable is greater than 50. If it is, the program takes a particular action. Put another way, if the statement in brackets is true, the statements inside the brackets are run. If not, the program skips over the code.

The statements being evaluated inside the parentheses require the use of one or more operators:

Changed lines 22-31 from:

Example

 
if(expression)
{
   statement;
   statement;
}

to:

Coding Warning: Beware of accidently using =, which sets a variable ( if (x = 10) ), instead of == ( if (x == 10) ), which tests whether x is equal to 10 or not.

The latter statement is only true if x equals 10, but the former statement will always be true, because the value of x = 10 is set if the assignment is successful. Mistaking = for == will result in a test that is always passed, and which resets your variable as a (probably unwanted) side-effect.

March 24, 2006, at 11:36 PM by Jeff Gray -
Changed lines 1-2 from:

if

to:

if statements

March 24, 2006, at 11:36 PM by Jeff Gray -
Changed lines 1-2 from:

setup

to:

if

March 24, 2006, at 11:35 PM by Jeff Gray -
Changed lines 1-2 from:

setup

to:

setup

March 24, 2006, at 11:34 PM by Jeff Gray -
Changed lines 1-6 from:

if(expression) {

   statement;
   statement;

}

to:

setup

Changed lines 5-7 from:

To test for equality is ==

to:

To test for equality is ==

Deleted line 8:
Deleted line 10:
Changed lines 13-19 from:

Other operators:

!= (not equals)

 < (less than)  
 > (grater than) 

<= (less than or equals)

>= (greater than or equals)

to:

Operators:

  • == (equals to)
  • != (not equals)
  • < (less than)
  • > (grater than)
  • <= (less than or equals)
  • >= (greater than or equals)

Example

if(expression)
{
   statement;
   statement;
}

Reference Home

February 14, 2006, at 04:46 PM by Erica Calogero -
Changed lines 12-14 from:

A warning: Beware of using ``= instead of ``==, such as writing accidentally

to:

A warning: Beware of using = instead of ==, such as writing accidentally

February 14, 2006, at 04:45 PM by Erica Calogero -
Added lines 1-26:

if(expression) {

   statement;
   statement;

}

This helps the control and flow of the programs, used to check if condition has been reached, the computer determines whether the expression (in the brackets) is true or false. If true the statements (inside the curly brackets) are executed, if not, the program skips over the code.

To test for equality is ==

A warning: Beware of using ``= instead of ``==, such as writing accidentally

   if ( i = j ) ..... 

This is a perfectly LEGAL C statement (syntactically speaking) which copies the value in "j" into "i", and delivers this value, which will then be interpreted as TRUE if j is non-zero. This is called assignment by value -- a key feature of C.

Other operators:

!= (not equals)

 < (less than)  
 > (grater than) 

<= (less than or equals)

>= (greater than or equals)

Share