Correción del titulo (#29190)
Corregí el titulo y el titulo local para que se viera correctamente la tabla.
This commit is contained in:
committed by
Randell Dawson
parent
70405d074a
commit
ce23e9a86a
@ -1,95 +1,96 @@
|
|||||||
---
|
---
|
||||||
title: C++ If Statement
|
title: C++ If Statement
|
||||||
localeTitle: C ++ If Declaración
|
localeTitle: C ++ If Declaración
|
||||||
---
|
---
|
||||||
# La declaración IF.
|
# La declaración IF.
|
||||||
|
|
||||||
**¿Qué hace una sentencia if?**
|
**¿Qué hace una sentencia if?**
|
||||||
|
|
||||||
* La instrucción `if` evalúa la expresión de prueba presente dentro del paréntesis.
|
* La instrucción `if` evalúa la expresión de prueba presente dentro del paréntesis.
|
||||||
* La instrucción `if` usa operadores relacionales y lógicos para hacer expresiones lógicas.
|
* La instrucción `if` usa operadores relacionales y lógicos para hacer expresiones lógicas.
|
||||||
|
|
||||||
* * *
|
* * *
|
||||||
|
|
||||||
La forma general de la declaración `if` :
|
La forma general de la declaración `if` :
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
if (Test Expression / Condition)
|
if (Test Expression / Condition)
|
||||||
{
|
{
|
||||||
// Block of statements if test expression is True
|
// Block of statements if test expression is True
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Si el valor de la expresión de prueba es **verdadero** , entonces el bloque de Código dentro de la sentencia if se ejecuta.
|
Si el valor de la expresión de prueba es **verdadero** , entonces el bloque de Código dentro de la sentencia if se ejecuta.
|
||||||
|
|
||||||
Si el valor de la expresión de prueba es **falso** , entonces el bloque de el código dentro de la sentencia if se omite y su código continúa.
|
Si el valor de la expresión de prueba es **falso** , entonces el bloque de el código dentro de la sentencia if se omite y su código continúa.
|
||||||
|
|
||||||
Ejemplo `if` declaración:
|
Ejemplo `if` declaración:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
int a = 10;
|
int a = 10;
|
||||||
|
|
||||||
// true statement
|
// true statement
|
||||||
if (a < 20)
|
if (a < 20)
|
||||||
{
|
{
|
||||||
// execute this block of code
|
// execute this block of code
|
||||||
}
|
}
|
||||||
|
|
||||||
// false statement
|
// false statement
|
||||||
if (a < 0)
|
if (a < 0)
|
||||||
{
|
{
|
||||||
// Skip this block of code.
|
// Skip this block of code.
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Ejemplo en C ++:
|
Ejemplo en C ++:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// Program to check if number entered by the user is positive
|
// Program to check if number entered by the user is positive
|
||||||
// If negative, the block of code is skipped
|
// If negative, the block of code is skipped
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int no ;
|
int no ;
|
||||||
cout << "Enter a number: ";
|
cout << "Enter a number: ";
|
||||||
cin >> no;
|
cin >> no;
|
||||||
|
|
||||||
// if statement to check if the number is positive
|
// if statement to check if the number is positive
|
||||||
if ( no > 0)
|
if ( no > 0)
|
||||||
{
|
{
|
||||||
cout << "You have entered a positive number: " << no << endl;
|
cout << "You have entered a positive number: " << no << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If number is not positive, then if statement is skipped a program continues
|
// If number is not positive, then if statement is skipped a program continues
|
||||||
cout << "This step is always printed" << endl;
|
cout << "This step is always printed" << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Salida:**
|
**Salida:**
|
||||||
|
|
||||||
SALIDA 1:
|
SALIDA 1:
|
||||||
```
|
```
|
||||||
Enter a number: 5
|
Enter a number: 5
|
||||||
You have entered a positive number: 5
|
You have entered a positive number: 5
|
||||||
This step is always printed
|
This step is always printed
|
||||||
```
|
```
|
||||||
This is the output when the number entered is positive.
|
This is the output when the number entered is positive.
|
||||||
|
|
||||||
OUTPUT 2:
|
OUTPUT 2:
|
||||||
```
|
```
|
||||||
|
|
||||||
Introduce un número: -1 Este paso siempre se imprime. \`\` \` Esta es la salida cuando el número introducido es negativo.
|
Introduce un número: -1 Este paso siempre se imprime. \`\` \` Esta es la salida cuando el número introducido es negativo.
|
||||||
|
|
||||||
[Pruebe el código usted mismo! :)](https://repl.it/Mg9X)
|
[Pruebe el código usted mismo! :)](https://repl.it/Mg9X)
|
||||||
|
|
||||||
_FELICIDADES . Este es el final del artículo sobre la declaración IF._
|
_FELICIDADES . Este es el final del artículo sobre la declaración IF._
|
||||||
|
|
||||||
**Buena suerte a todos ustedes**
|
**Buena suerte a todos ustedes**
|
||||||
|
|
||||||
**¡Feliz codificación! :)**
|
**¡Feliz codificación! :)**
|
||||||
|
|
||||||
|
**No dude en preguntar cualquier duda en la página de GitHub de [FreeCodeCamp](https://forum.freecodecamp.org/) o en [el foro de FreeCodeCamp.](https://forum.freecodecamp.org/)**
|
||||||
|
Reference in New Issue
Block a user