Está en la página 1de 30

Curso: COIS 394 Prof. J.

Aponte,DBA

Operadores Aritmticos Operadores de Relacin Operadores Lgicos If Statement If-Else Statement Estudio de casos

Nos permiten realizar los eventos matemticos.


+ * / % = = = = = suma resta multiplicacin divisin divisin 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 smbolos 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 > .25) && (income < 20000)

(temperature <= 75) || (humidity < .70)


(age > 21) && (age < 60) (age == 21) || (age == 22)

Existen dos( 2) tipos de estructuras de control para las condiciones:


Seleccin (Selection/branching)

Repeticin (Repetition (looping))

Selection

if if else switch/case for loop while loop

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 ( z == 8) { v=5; System.out.println(POSITIVO); j=A; }

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 (Expression) { statementsA; } else { statementsB; }

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

taxDue = income * taxRate; System.out.print(TaxDue); }

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

if (creditsEarned >= 90)


System.out.print(SENIOR STATUS); else if (creditsEarned >= 60) System.out.print(JUNIOR STATUS); else if (creditsEarned >= 30) System.out.print(SOPHOMORE STATUS);

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);

if (index < 35)


System.out.print(Pleasant);

else if (index <= 60)


System.out.print(Unpleasant);

else
System.out.print(Health Hazard);

También podría gustarte