Descargar la presentación
La descarga está en progreso. Por favor, espere
Publicada porArnaldo De Gonia Modificado hace 9 años
1
Haga clic para modificar el estilo de subtítulo del patrón Prof. Addys de Lam INSTRUCCIONES DE CONTROL OBJETIVOS UTILIZAR LAS INSTRUCCIONES DE CONTROL DE ALTERNATIVAS O DECISIONES SIMPLES, DOBLES, COMPUESTAS Y ANIDADAS. UTILIZAR LOS OPERADORES LOGICOS. UTILIZAR LAS INSTRUCCIONES DE CONTROL DE ALTERNATIVAS MULTIPLES.
2
Prof. Addys de Lam INSTRUCCIONES DE CONTROL ALTERNATIVAS SENTENCIA if Formato: if( Condición ) sentencia; Ejemplos: if (ds!= 0) c= dd/ds; if (edad>=18) { votante+=1; System.out.println(“Mayor de edad”); }
3
Prof. Addys de Lam La sentencia if/else Prof. Addys de Lam Formato: if( expresion ) { sentencias; } else { sentencias; }
4
Prof. Addys de Lam if (num%2==0) System.out.println("El Numero "+num+" es PAR"); else System.out.println("El Numero "+num+" es IMPAR"); if (num%2==0){ System.out.println("El Numero "+num+" es PAR"); num*=num; } else { System.out.println("El Numero "+num+" es IMPAR"); num*=3; } EJEMPLOS:
5
Prof. Addys de Lam EJEMPLOS La instrucción if/else ANIDADAS if (i == j ) { if (j == k) System.out.println(“i igual a k); } else System.out.println(“i no es igual a j”); if (num>0) np+=1; else if(num<0) nn+=1; else nc+=1;
6
Prof. Addys de Lam La instrucción else if, para if anidados Prof. Addys de Lam if (n == 1 ) { // Ejecuta el bloque 1 } else if (n == 2) { // Ejecuta el bloque 2 } else { // Si todo falla ejecuta bloque 3 }
7
Prof. Addys de Lam USO DE LOS OPERADORES LOGICOS EN ESTRUCTURAS DE SELECCIÓN COMPUESTAS AND if ( condición1 ) { if ( condición2 ) { sentencia1; } else { sentencia2; } } else { sentencia2; } Prof. Addys de Lam if (condición1 && condición2){ sentecia1; } else { sentencia2; }
8
Prof. Addys de Lam USO DE LOS OPERADORES LOGICOS EN ESTRUCTURAS DE SELECCIÓN COMPUESTAS OR if ( condición1 ){ sentencia1; } else { if ( condición2 ) { sentencia1; } else { sentencia2; } Prof. Addys de Lam if ( condición1 || condición2 ) { sentencia1; } else { sentencia2; }
9
Prof. Addys de Lam EJEMPLO Un año es bisiesto si es múltiplo de 4 (año % 4 == 0 ), pero no debe ser múltiplo de 100 (( año % 4 == 0 ) && ( año % 100 != 0 )), a menos que sea múltiplo de 400.((( año % 4 == 0 ) && ( año % 100 != 0 )) || ( año % 400 == 0 ))), por lo tanto la estructura compuesta: if ((( año % 4 == 0 ) && ( año % 100 != 0 )) || ( año % 400 == 0 )) System.out.println("Es bisiesto"); else System.out.println("No es bisiesto"); Prof. Addys de Lam
10
USO DE LOS OPERADORES LOGICOS EN ESTRUCTURAS DE SELECCIÓN COMPUESTAS NOT if ( condición1) { sentencia1; } else { sentencia2; } Prof. Addys de Lam if ( ! condición1) { sentencia2; } else { sentencia1; }
11
Prof. Addys de Lam EJEMPLO import java.io.* ; class Division { private int dv,ds; Division ( int n, int d) { dv = n; ds = d ;} public int divide () { int coc=0; try { coc = dv/ds;} catch (ArithmeticException e) { System.out.println ("\n ERROR DIVISION POR CERO " ); } return coc; } Prof. Addys de Lam
12
EJEMPLO import java.io.* ; class Pdivide{ public static void main(String arg[]) throws IOException { BufferedReader dato = new BufferedReader(new InputStreamReader (System.in)); System.out.println("Entre Dividendo"); int num = Integer.parseInt(dato.readLine()); System.out.println("Entre Divisor"); int den = Integer.parseInt(dato.readLine()); System.out.print(num + "/”+ den + "="); Division coci= new Division(num,den); Prof. Addys de Lam
13
EJEMPLO if (coci.divide()!=0){ System.out.println(coci.divide()); System.out.println("\nResultado Satisfactorio"); }else{ System.out.println("\nResultado Dudoso"); } Prof. Addys de Lam
14
INSTRUCCIONES DE CONTROL ALTERNATIVAS Alternativa múltiple: Formato: switch( expr1 ) { case expr2: sentencias; break; case expr3: sentencias; break; default: sentencias; break; }
15
Prof. Addys de Lam EJEMPLO switch(op){ case '1': System.out.println(“Seleccionó... opción1"); break; case '2': System.out.println(“Seleccionó... opción2"); break; case '3': System.out.println(" Seleccionó... opción3"); break; default: System.out.println("Error: solo son de 1 a 3"); }
16
Prof. Addys de Lam La instrucción switch EJEMPLO switch(nota) { case 10: case 9: System.out.println(‘SB’); break; case 8: case 7: System.out.println (‘N’); break; case 6: case 5: System.out.println (‘A’); break; default: System.out.println (‘S’); break; }
17
Prof. Addys de Lam EJEMPLO Prof. Addys de Lam switch (mes) { case 1: System.out.println("Enero"); break; case 2: System.out.println("Febrero"); break; case 3: System.out.println("Marzo"); break; case 4: System.out.println("Abril"); break; case 5: System.out.println("May0"); break; case 6: System.out.println("Junio"); break; case 7: System.out.println("Julio"); break; case 8: System.out.println("Agosto"); break; case 9: System.out.println("Septiembre"); break; case 10: System.out.println("Octubre"); break; case 11: System.out.println("Noviembre"); break; case 12: System.out.println("Diciembre"); break; }
18
Prof. Addys de Lam EJEMPLO Prof. Addys de Lam switch (mes) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numeroDias = 31; break; case 4: case 6: case 9: case 11: numeroDias = 30; break; case 2: if ( ((ano % 4 == 0) && !(ano % 100 == 0)) || ano % 400 == 0) ) numeroDias = 29; else numeroDias = 28; break; }
Presentaciones similares
© 2024 SlidePlayer.es Inc.
All rights reserved.