La descarga está en progreso. Por favor, espere

La descarga está en progreso. Por favor, espere

Introducción a JAVA COMP 250. Estructura de Selección selection statements selection statements – Escoger cuál acción ejecutar dependiendo de dos ó más.

Presentaciones similares


Presentación del tema: "Introducción a JAVA COMP 250. Estructura de Selección selection statements selection statements – Escoger cuál acción ejecutar dependiendo de dos ó más."— Transcripción de la presentación:

1 Introducción a JAVA COMP 250

2 Estructura de Selección selection statements selection statements – Escoger cuál acción ejecutar dependiendo de dos ó más alternativas a evaluar booleanas booelano – Las condiciones son expresiones booleanas, esto es, el resultado de una comparación es un valor booelano (true / false)

3 Estructura de Selección Operadores de comparación operadornombreejemploresultado <Menor queRadius < 0false <=Menor o igual a Radius <= 0false >Mayor queRadius > 0true >=Mayor o igual a Radius >= 0true ==Igual aRadius == 0false !=No igual aRadius != 0true Observar que el radio de un círculo no puede ser cero ó negativo (menor que cero)

4 Estructura de Selección Ejemplo double radius = 1; System.out.println(radius > 0); – El valor a mostrar en el output debe ser true

5 Estructura de Selección if statements – One-way if statements Formato: if (boolean-expression) { statement(s); } (radius >= 0) false true area = radius * radius * PI; System.out.println(The area for the circle of radius + radius + is + area);

6 Estructura de Selección Two-way if statement – Formato: if(boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } Boolean expression falsetrue Statements for the true case Statements for the false case

7 Estructura de Selección Nested if statements if (score >= 90.0) grade = A; else if (score >= 80.0) grade = B; else if (score >=70.0) grade = C; else if (score >= 60.0) grade = D; else grade = F;

8 Estructura de Selección switch statement status = 0 Compute tax for single filers break Status = 1 Compute tax for married filers break Default actions default T T F

9 Estructura de Selección Ejemplo: switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married filers; break; default: System.out.println(Errors: invalid status); System.exit(0);

10 Formatting console output Formato general: System.out.printf(format, item1, item2, …itemz) Donde format es un string que puede consistir de varios formatos especializados

11 Formatting console output specifieroutputexample %bA boolean valueTrue or false %cA charactera %dA decimal integer200 %fA floating-point number45.460000 %eA number in standard scientific notation 4.556000e+01 %sA stringJava is cool Ejemplo: int count =5; double amount = 45.56; System.out.printf(count is %d and amount is %f, count, amount); Output: count is 5 and amount is 45.560000

12 Formatting console output exampleoutput %5cOutput the character and add four spaces before the character item %6bOutput the boolean value and add one space before the false value and two spaces before the true value %5dOutput the integer with width at least 5. %10.2fOutput the floating-point number with width at least 10 including a decimal point and two digits after the point. %10.2eOutput the floating-point item with width at least 10 including a decimal point, two digits after the point and the exponent part. %12sOutput the string with width at least 12 characters

13 Logical Operators operatornamedescription !notLogical negation &&andLogical conjunction ||orLogical disjunction ^Exclusive orLogical exclusion Ejemplos: (asumir que age = 24, gender = F) !(age > 18) …………………………………………………..false !(gender == M)…………………………………………..true


Descargar ppt "Introducción a JAVA COMP 250. Estructura de Selección selection statements selection statements – Escoger cuál acción ejecutar dependiendo de dos ó más."

Presentaciones similares


Anuncios Google