Nested Loop
Parte de la sección Fundamentos del Journey de Java de Coddy — lección 48 de 73.
Un bucle anidado es simplemente un bucle dentro de otro bucle. El bucle interno completará todas sus iteraciones por cada iteración individual del bucle externo.
Una buena analogía para esto es un reloj: por cada hora (bucle externo), el minutero (bucle interno) debe completar su ciclo completo de 60 minutos.
Ejemplo de un bucle anidado:
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
System.out.println(x + " " + y);
}
}
// Esto mostrará:
// 0 0
// 0 1
// 1 0
// 1 1El bucle externo (x) se ejecuta dos veces, y por cada una de esas veces, el bucle interno (y) se ejecuta dos veces.
Desafío
PrincipianteEscribe un programa que imprima un rectángulo de asteriscos (*) con un ancho y alto dados.
Entrada: Dos números enteros: width y height
Por ejemplo:
Si width = 5 y height = 3, la salida debería ser:
*****
*****
*****Si width = 4 y height = 6, la salida debería ser:
****
****
****
****
****
****Pruébalo tú mismo
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int width = scanner.nextInt();
int height = scanner.nextInt();
// Escribe tu código a continuación
}
}Esta lección incluye un breve cuestionario. Empieza la lección para responderlo y registrar tu progreso.
Todas las lecciones de Fundamentos
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else