Curso: COIS 394 Prof. J.

Aponte,DBA

   


Operadores Aritméticos Operadores de Relación Operadores Lógicos If… Statement If-Else Statement Estudio de casos

Nos permiten realizar los eventos matemáticos.
◦ ◦ ◦ ◦ ◦ + * / % = = = = = suma resta multiplicación división división modular(residuo)

◦ ◦ ◦ ◦ ◦ 5 5 5 5 5 + 2 es 7 – 2 es 3 * 2 es 10 / 2 es 2.5 % 2 es 1 .

 Son los símbolos que nos ayudan a establecer comparaciones.(if statement) ◦ ◦ ◦ ◦ ◦ ◦ == > < >= <= != igual a mayor que menor que mayor o igual menor o igual no es igual .

       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)    .

    (A > B) || (C == 5) (A > B) && (C == 5) (A > B) || (C == 5) && (D < E) !(A < B) && (C == 3) .

taxRate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22 .

(taxRate > .70) (age > 21) && (age < 60) (age == 21) || (age == 22) .25) && (income < 20000) (temperature <= 75) || (humidity < .

 Existen dos( 2) tipos de estructuras de control para las condiciones: Selección (Selection/branching) Repetición (Repetition (looping)) .

 Selection if if …else switch/case for loop while loop  Repetition .

donde se realiza o no los statements (puede ser una instrucción simple o un bloque entero) TRUE expression Statement(s) FALSE .

println(”Saludos”).out. Ejemplo: 1.if (Expression) Statement. . if (a > 5) b=a + c. System.

} .println(“POSITIVO”). j=‘A’. System.if ( z == 8) { v=5.out.

if (Expression) StatementA. . else StatementB.

entre la ejecución 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” . } else { statementsB.if (Expression) { statementsA.

public class TercerPrograma { public static void main(String[]args) throws IOException { int carDoors. driverAge.out.00 .print(“ HIGH RISK”). } } .print(“ LOW RISK”). System.0 + 5.out.print(monthlyPayment ). } monthlyPayment = premium / 12.io.out. //Realizar proceso interactivo para las variables System.00 .out.00 .print() if ((carDoors == 4) && (driverAge > 24)) { premium = 650.*. } else { premium = 1200.Ejemplo: import java. monthlyPayment . double premium. System. System.

15 to discountRate and assign value 5.00 to shipCost if purchase is over 100. calculate totalBill . assign value .00 to shipCost Either way.Ejercicio: Assign value .25 to discountRate and assign value 10.00 Otherwise.

25 .if (purchase > 100. } else { discountRate = 0.discountRate) + shipCost .00.00. .0 . shipCost = 10. } totalBill = purchase * (1. shipCost = 5.15 .00) { discountRate = 0.

2. read values for income and taxRate from keyboard. If taxCode is ‘T’. and calculate and display taxDue as their product.1. . increase price by adding taxRate times price to it. If code has value 1.

print(TaxDue). if (taxCode == ‘T’) price = price + taxRate * price.1. } . System. if (code = = 1) { // realizar proceso interactivo para las variables taxDue = income * taxRate.out. 2.

. and can be accomplished by using NESTED if statements.is also called multi-way branching.

. . else if ( ExpressionN ) StatementN.if ( Expression1 ) Statement1. else if ( Expression2 ) Statement2. EXACTLY 1 of these statements will be executed. . . else Statement N+1.

AN EXAMPLE . . Only the specific Statement following that particular true Expression is executed. If no Expression is true. the Statement following the final else is executed.Each Expression is evaluated in sequence. until some Expression is found that is true. . .

else if (creditsEarned >= 30) System. else if (creditsEarned >= 60) System.print(“JUNIOR STATUS”).if (creditsEarned >= 90) System.print(“SENIOR STATUS”).out.print(“SOPHOMORE STATUS”).out. else System. .out.out.print(“FRESHMAN STATUS”).

Display one word to describe the int value of number as “Positive”. or “Zero”. and above 60 as “Health Hazard. “Negative”. Your city classifies a pollution index less than 35 as “Pleasant”. 2. 35 through 60 as “Unpleasant”.” Display the correct description of the pollution index value. .1.

print(“Positive”).out.out. .if (number > 0) System.print(“Negative”). else System.out.print(“Zero”). else if (number < 0) System.

print(“Health Hazard”).print(“Pleasant”). else System. else if (index <= 60) System. .if (index < 35) System.out.out.print(“Unpleasant”).out.

Sign up to vote on this title
UsefulNot useful