Descargar la presentación
La descarga está en progreso. Por favor, espere
1
Introducción a JAVA COMP 250
2
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)
3
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)
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); } false (radius >= 0) 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; true false Boolean expression 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 T Compute tax for single filers status = 0 break F T Status = 1 Compute tax for married filers break default Default actions
9
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);
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
specifier output example %b A boolean value True or false %c A character ‘a’ %d A decimal integer 200 %f A floating-point number %e A number in standard scientific notation e+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
12
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
13
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
Presentaciones similares
© 2025 SlidePlayer.es Inc.
All rights reserved.