estructuras de control
TRANSCRIPT
![Page 1: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/1.jpg)
Estructuras de Control
Programación 302
![Page 2: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/2.jpg)
2
Instrucciones de flujo if..else
• La sintaxis de la instrucción if (expresión booleana) {
enunciado ó bloque; } if (expresión booleana) {
enunciado ó bloque; } else if (expresión booleana) {
enunciado ó bloque; } else {
enunciado ó bloque; }
if (expresión booleana) {enunciado ó bloque;
} if (expresión booleana) {
enunciado ó bloque; } else if (expresión booleana) {
enunciado ó bloque; } else {
enunciado ó bloque; }
![Page 3: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/3.jpg)
3
public class ControlFlujo{ public static void main(String arg[]){
//Asigacion de variable byte caliEstudi= 100;
//Flujo de control if if( caliEstudi >= 60) System.out.println("Aprobado " + caliEstudi);
}//main}//class
Instrucciones de flujo if..else
![Page 4: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/4.jpg)
4
Instrucciones de flujo if..else
//Flujo de control if if( caliEstudi >= 60){
System.out.println("Aprobado " + caliEstudi);}else{
System.out.println("Reprobado!! " + caliEstudi); }//else
![Page 5: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/5.jpg)
5
Instrucciones de flujo if..else anidado
//if else anidado if(caliEstudi >= 90)
System.out.println("A " + caliEstudi); else
if(caliEstudi >= 80) System.out.println("B " + caliEstudi);
else if(caliEstudi >= 70)
System.out.println("C " + caliEstudi); else if(caliEstudi >= 60) System.out.println("D " + caliEstudi); else
System.out.println("F Reprobado!! " + caliEstudi);
![Page 6: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/6.jpg)
6
Instrucciones de flujo
• La sintaxis de la instrucción swich switch (expr1) {
case constant2:instrucciones;break;
case constant3:instrucciones;break;
default:instrucciones;break;
}
switch (expr1) {case constant2:
instrucciones;break;
case constant3:instrucciones;break;
default:instrucciones;break;
}
![Page 7: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/7.jpg)
7
Instrucciones de flujo switch
Ejemplo: switch (modeloAuto) {
case DELUJO:System.out.printl(“Guao”);break;
case ESTANDARD;
System.out.println(“Bueno”);break;
default:System.out.println(“Hum”);
}
Tipos datos para swich
Permitidos: Int
Promover a int: byte short char
No permitidos: String, float, long
![Page 8: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/8.jpg)
8
Instrucciones de flujo switchpublic class SwitchTemporada{
public static void main(String arg[]){ int mes=4; String estacion; switch(mes){
case 12: case 1: case 2: estacion = "Invierno"; break; case 3: case 4: case 5: estacion ="Primavera"; break;
case 6: case 7: case 8: estacion = "Verano"; break; case 9: case 10: case 11: estacion = "Otoño"; break; default: estacion = "Mes desconocido"; }//fin switch System.out.println("Abril esta en " + estacion + "."); }//main}//class
![Page 9: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/9.jpg)
9
Instrucciones de lazo
• El lazo for
for (declara e inicializa var; condición finalizar; incrementa var){instrucción ó bloque;
}
for (declara e inicializa var; condición finalizar; incrementa var){instrucción ó bloque;
}
Ejemplo: for (int i = 0; i < 10; i++){
System.out.println(“Ya terminaste?”);}System.out.println(“Terminado!”);
for (int i = 0; i < 10; i++){System.out.println(“Ya terminaste?”);
}System.out.println(“Terminado!”);
![Page 10: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/10.jpg)
10
Instrucciones de lazo
• El lazo while
while (boolean) {enunciado ó bloque;
}
while (boolean) {enunciado ó bloque;
}
Ejemplo: int j = 0; while (j< 10) {
System.out.println(“Ya terminaste?”);j++;
}System.out.println(“Hecho !”);
int j = 0; while (j< 10) {
System.out.println(“Ya terminaste?”);j++;
}System.out.println(“Hecho !”);
![Page 11: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/11.jpg)
11
Instrucciones de lazo
• El lazo do..while
do {enunciado ó bloque;
} while ( boolean prueba);
do {enunciado ó bloque;
} while ( boolean prueba);
Ejemplo: int k = 0; do {
System.out.println(“Ya terminaste?”);k++;
} while (k < 10); System.out.println(“Hecho !”);
int k = 0; do {
System.out.println(“Ya terminaste?”);k++;
} while (k < 10); System.out.println(“Hecho !”);
![Page 12: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/12.jpg)
12
Control especial de flujo de lazos
• break (etiqueta);• continue [etiqueta];• etiqueta: enunciado; //donde el enunciado
//debería ser un lazo
![Page 13: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/13.jpg)
13
Ejemplo
do {enunciado;if (condición es verdadera){
break; } enunciado; } while (boolean expresión);
• Instrucción break
![Page 14: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/14.jpg)
14
Ejemplo
• Instrucción continue
do { enunciado;
if (expresión booleana){continue;
}enunciado;
} while (expresión booleana);
do { enunciado;
if (expresión booleana){continue;
}enunciado;
} while (expresión booleana);
![Page 15: Estructuras De Control](https://reader036.vdocumento.com/reader036/viewer/2022083001/557bc1b8d8b42a1c1f8b4e6c/html5/thumbnails/15.jpg)
15
Ejemplo
• Instrucción break con labelelMasAfuera:
do { enunciado; do{ if (expresión booleana) {
break elMasAfuera; } enunciado; }while (expresión booleana);
enunciado;} while (expresión boooleana);