Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Aponte,DBA
Operadores Aritmticos Operadores de Relacin Operadores Lgicos If Statement If-Else Statement Estudio de casos
5 5 5 5 5
+ 2 es 7 2 es 3 * 2 es 10 / 2 es 2.5 % 2 es 1
X <= 0 Power > Max Z >= Y Codigo == B N1 != N2 X < (min + max) (max + min) > X
Nos permite establecer relaciones compuestas (if statement) ! (Not) && (and) || (or)
age is 21 or 22
Selection
Repetition
donde se realiza o no los statements (puede ser una instruccin simple o un bloque entero)
TRUE expression
Statement(s)
FALSE
if (Expression)
Statement; Ejemplo: 1. if (a > 5) b=a + c; System.out.println(Saludos);
if (Expression)
StatementA;
else
StatementB;
entre la ejecucin de una de dos clausuras (la del if clause o la del else clause)
TRUE
expression
FALSE
if clause
else clause
if clause
else clause
Ejemplo: import java.io.*; public class TercerPrograma { public static void main(String[]args) throws IOException { int carDoors, driverAge; double premium, monthlyPayment ; //Realizar proceso interactivo para las variables System.out.print() if ((carDoors == 4) && (driverAge > 24)) { premium = 650.00 ; System.out.print( LOW RISK); } else { premium = 1200.00 ; System.out.print( HIGH RISK); } monthlyPayment = premium / 12.0 + 5.00 ; System.out.print(monthlyPayment ); } }
Ejercicio:
Assign value .25 to discountRate and assign value 10.00 to shipCost if purchase is over 100.00 Otherwise, assign value .15 to discountRate and assign value 5.00 to shipCost Either way, calculate totalBill
if (purchase > 100.00) { discountRate = 0.25 ; shipCost = 10.00; } else { discountRate = 0.15 ; shipCost = 5.00; } totalBill = purchase * (1.0 - discountRate) + shipCost ;
1. If taxCode is T, increase price by adding taxRate times price to it. 2. If code has value 1, read values for income and taxRate from keyboard, and calculate and display taxDue as their product.
1. if (taxCode == T) price = price + taxRate * price; 2. if (code = = 1) { // realizar proceso interactivo para las variables
is also called multi-way branching, and can be accomplished by using NESTED if statements.
if ( Expression1 ) Statement1;
else if ( Expression2 ) Statement2;
. . .
else if ( ExpressionN ) StatementN; else Statement N+1; EXACTLY 1 of these statements will be executed.
Each Expression is evaluated in sequence, until some Expression is found that is true.
Only the specific Statement following that particular true Expression is executed. If no Expression is true, the Statement following the final else is executed.
AN EXAMPLE . . .
else
System.out.print(FRESHMAN STATUS);
1. Display one word to describe the int value of number as Positive, Negative, or Zero.
2. Your city classifies a pollution index less than 35 as Pleasant, 35 through 60 as Unpleasant, and above 60 as Health Hazard. Display the correct description of the pollution index value.
if (number >
0)
System.out.print(Positive);
else
if (number < 0)
System.out.print(Negative);
else System.out.print(Zero);
else
System.out.print(Health Hazard);