Introducción a JAVA COMP 250
Estructura de Selección selection statements Escoger cuál acción ejecutar dependiendo de dos ó más alternativas a evaluar Las condiciones son expresiones booleanas, esto es, el resultado de una comparación es un valor booelano (true / false)
Estructura de Selección Operadores de comparación operador nombre ejemplo resultado < Menor que Radius < 0 false <= Menor o igual a Radius <= 0 > Mayor que Radius > 0 true >= Mayor o igual a Radius >= 0 == Igual a Radius == 0 != No igual a Radius != 0 Observar que el radio de un círculo no puede ser cero ó negativo (menor que cero)
Estructura de Selección Ejemplo double radius = 1; System.out.println(radius > 0); El valor a mostrar en el output debe ser true
Estructura de Selección if statements One-way if statements Formato: if (boolean-expression) { statement(s); } false (radius >= 0) true area = radius * radius * PI; System.out.println(“The area for the circle of radius “ + radius + “ is “ + area);
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; true false Boolean expression Statements for the true case Statements for the false case
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’;
Estructura de Selección switch statement T Compute tax for single filers status = 0 break F T Status = 1 Compute tax for married filers break default Default actions
Estructura de Selección Ejemplo: switch (status) { case 0: compute taxes for single filers; break; case 1: compute taxes for married filers; default: System.out.println(“Errors: invalid status”); System.exit(0);
Formatting console output Formato general: System.out.printf(format, item1, item2, …itemz) Donde format es un string que puede consistir de varios formatos especializados
Formatting console output specifier output example %b A boolean value True or false %c A character ‘a’ %d A decimal integer 200 %f A floating-point number 45.460000 %e A number in standard scientific notation 4.556000e+01 %s A string “Java 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
Formatting console output example output %5c Output the character and add four spaces before the character item %6b Output the boolean value and add one space before the false value and two spaces before the true value %5d Output the integer with width at least 5. %10.2f Output the floating-point number with width at least 10 including a decimal point and two digits after the point. %10.2e Output the floating-point item with width at least 10 including a decimal point , two digits after the point and the exponent part. %12s Output the string with width at least 12 characters
Logical Operators operator name description ! not Logical negation && and Logical conjunction || or Logical disjunction ^ Exclusive or Logical exclusion Ejemplos: (asumir que age = 24, gender = ‘F’) !(age > 18) …………………………………………………..false !(gender == ‘M’)…………………………………………..true