Improved spanish information about for loop (#31404)
* Improved information about for loop Improved range for loop info Changed int[5] array to int array[] = {1, 2, 3, 4, 5}; * fix: changed c++ to cpp
This commit is contained in:
@ -9,14 +9,14 @@ El bucle for se distingue de otras declaraciones de bucle a través de un contad
|
|||||||
Por lo tanto, un bucle for es una estructura de control de repetición que le permite escribir de manera eficiente un bucle que necesita ejecutarse un número específico de veces.
|
Por lo tanto, un bucle for es una estructura de control de repetición que le permite escribir de manera eficiente un bucle que necesita ejecutarse un número específico de veces.
|
||||||
|
|
||||||
## Sintaxis
|
## Sintaxis
|
||||||
```
|
```cpp
|
||||||
for ( init; condition; increment ) {
|
for ( init; condition; increment ) {
|
||||||
statement(s);
|
statement(s);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Se permite colocar el incremento en el bucle for como en un bucle while. Significar una sintaxis como esta también puede funcionar.
|
Se permite colocar el incremento en el bucle for como en un bucle while. Significar una sintaxis como esta también puede funcionar.
|
||||||
```
|
```cpp
|
||||||
for ( init; condition;) {
|
for ( init; condition;) {
|
||||||
statement(s);
|
statement(s);
|
||||||
increment;
|
increment;
|
||||||
@ -48,8 +48,10 @@ La instrucción de actualización se usa para alterar la variable del bucle medi
|
|||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}```
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```
|
||||||
Output:
|
Output:
|
||||||
value of a: 10
|
value of a: 10
|
||||||
value of a: 11
|
value of a: 11
|
||||||
@ -61,31 +63,45 @@ La instrucción de actualización se usa para alterar la variable del bucle medi
|
|||||||
value of a: 17
|
value of a: 17
|
||||||
value of a: 18
|
value of a: 18
|
||||||
value of a: 19
|
value of a: 19
|
||||||
|
```
|
||||||
|
|
||||||
##Single lined loop
|
##Bucle for en una sola linea
|
||||||
The body of the for loop need not be enclosed in braces if the loop iterates over only one satatement.
|
Al igual que if y while, si después de una instrucción for no hay un bloque formado por { ... } se iterará sobre la siguiente instrucción.
|
||||||
##Example
|
##Ejemplo
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
#using namespace std;
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
// Línea única para bucle para
|
||||||
|
for(int a = 10; a <20; a = a + 1)
|
||||||
|
cout << "valor de a:" << a << endl;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
c ++ #incluir utilizando namespace std;
|
|
||||||
|
|
||||||
int main () { // Línea única para bucle para (int a = 10; a <20; a = a + 1) cout << "valor de a:" << a << endl;
|
|
||||||
|
|
||||||
devuelve 0; } \`\` \`
|
|
||||||
|
|
||||||
Esto generaría la misma salida que el programa anterior. es decir Salida: valor de a: 10 valor de a: 11 valor de a: 12 valor de a: 13 valor de a: 14 valor de a: 15 valor de a: 16 valor de a: 17 valor de a: 18 valor de a: 19
|
Esto generaría la misma salida que el programa anterior. es decir Salida: valor de a: 10 valor de a: 11 valor de a: 12 valor de a: 13 valor de a: 14 valor de a: 15 valor de a: 16 valor de a: 17 valor de a: 18 valor de a: 19
|
||||||
```
|
|
||||||
## Explanation
|
# Bucle for basado en rangos
|
||||||
Here's the initialization condition is first set to a=10. The loop first checks for this condition. It then checks for the condition expression ie a<20 which holds true as 10<20(for the first case). Now the body of the loop is executed and we get the output "Value of a: 10". Then the update expression is executed which adds the number 1 to 'a' and the value of 'a' gets updated to 11 and the same steps are followed (as above) until the value of v reaches less than 20 ie 19.
|
C++ también permite iterar sobre un contenedor usando rangos. En el bucle se especifican el contenedor a recorrer (un std::vector por ejemplo) y una variable con el valor del elemento del contenedor. También permite usar referencias.
|
||||||
|
|
||||||
# Range-based for-loop
|
## Sintaxis
|
||||||
C++ also has what we call range-based for loops which iterates through all the elements of a container(eg array).
|
```cpp
|
||||||
|
|
||||||
## Syntax
|
para (elemento: contenedor) declaración (es);
|
||||||
|
|
||||||
|
int array[] = {1, 2, 3, 4, 5} para (int i: array) cout << i << endl; }
|
||||||
|
|
||||||
|
Salida: 1 2 3 4 5
|
||||||
```
|
```
|
||||||
|
|
||||||
para (elemento: contenedor) declaración (es); }
|
Si se desea usar la referencia del elemento del contenedor, por ejemplo para asignarlo simplemente sería:
|
||||||
|
```cpp
|
||||||
int \[5\] array = {1, 2, 3, 4, 5} para (int i: array) cout << i << endl; }
|
int array[] = {1, 2, 3, 4, 5};
|
||||||
|
for(int& i : array) {
|
||||||
Salida: 1 2 3 4 5 \`\` \`
|
i += 1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Al usar & se obtiene la referencia y se puede asignar los valores, por lo que con el ejemplo anterior el contenedor tendría
|
||||||
|
```
|
||||||
|
{2, 3, 4, 5, 6};
|
||||||
|
```
|
||||||
|
Reference in New Issue
Block a user