# Java

Topics: Logic, For loop, Statement Pages: 3 (457 words) Published: June 24, 2013
Relational Opertors Conditions and Loops
INFS1609/COMP1400 – Week 3
== != > < >= 4 != 2 >= 6 < 5 // // // // // evaluates evaluates evaluates evaluates evaluates to to to to to false. true. true. true. false. int a = 2; int b = 3; int c = 6; a == 5 a*b >= c b+4 > a*c (b=2) == a // // // //

Expressions in Relations

evaluates evaluates evaluates evaluates

to to to to

false since a is not equal to 5. true since (2*3 >= 6) is true. false since (3+4 > 2*6) is false. true.

Logical Operators
• •
! performs the Boolean operation NOT It has only one operand, on the right, and the only thing that it does is invert the value, producing false if its operand is true and true if its operand is false.

Logical Operators
The operator && corresponds to Boolean logical operation AND. This operation results true if both its two operands are true and false otherwise. && OPERATOR a b true true true false false true false false a && b true false false false

!(5 == 5) !(6 6))

// evaluates to false ( true && false ). // evaluates to true ( true || false ).

if-then-else statement
void applyBrakes() { if (isMoving) { currentSpeed--; } else { System.out.println(“The bicycle has already stopped”); } }

if-then-else statement
int testScore = 76; char grade; if (testScore >= 90) grade = 'A'; else if (testScore >= 80) grade = 'B'; else if (testScore >= 70) grade = 'C'; else if (testScore >= 60) grade = 'D'; else grade = 'F'; System.out.println("Grade = " + grade);

int testScore = 76; char grade; if (testScore >= 90) { grade = 'A'; } else { if (testScore >= 80) { grade = 'B'; } else { if (testScore >= 70) { grade = 'C'; } else { if (testScore >= 60) { grade = 'D'; } else { grade = 'F'; } } } }

same statement, different format

while statement
Must evaluate to true or false

while (expression) { statement(s) }

Keep repeating while test is true

while statement
each loop
int count = 1; while...