Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture C
Slide 1 of 64
The if Statement
The Java if statement has the following syntax:
if (boolean-condition)
statement;
If the Boolean condition is true, the statement is
Lecture C
Slide 2 of 64
Tempreture
class Temperature {
static final int THRESHOLD = 65;
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int temperature =
input.requestInt(Enter the temperature:);
System.out.println(Current temperature +
temperature);
if (temperature < THRESHOLD)
System.out.println(Its cold in here!);
}
}
Lecture C
Slide 3 of 64
condition
true
statement
Lecture C
Slide 4 of 64
Boolean Expressions
The condition of an if statement must evaluate to a true
or false result
Java has several equality and relational operators:
Operator
==
Meaning
equal to
!=
not equal to
<
less than
<=
>
greater than
>=
Lecture C
Slide 5 of 64
Block Statements
Several statements can be grouped together into a block
statement
Blocks are delimited by braces
A block statement can be used wherever a statement is
called for in the Java syntax
if (boolean-condition){
statement1;
statement2;
}
Lecture C
Slide 6 of 64
Example - Temperature2
class Temperature2 {
static final int THRESHOLD = 65;
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int temperature =
input.requestInt(Enter the temperature:);
System.out.println(Current temperature +
temperature);
if (temperature < THRESHOLD) {
System.out.println(Its cold in here!);
System.out.println(But well survive.);
}
}
}
Lecture C
Slide 7 of 64
If .. Else Statement
An else clause can be added to an if statement to
Lecture C
Slide 8 of 64
Example - Temperature3
class Temperature3 {
static final int FREEZING_POINT = 32;
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int temperature =
input.requestInt(Enter temperature:);
if (temperature <= FREEZING_POINT)
System.out.println(Its freezing!);
else
System.out.println(Above freezing.);
}
}
Lecture C
Slide 9 of 64
true
statement1
Lecture C
condition
statement2
Slide 10 of 64
Nested If statements
Since an If statement is a statement, it can appear inside
another if statement.
if (condition1)
if (condition2)
statement;
It can also appear in an else clause
if (condition1)
statement1;
else if (condition2)
statement2;
Lecture C
Slide 11 of 64
Nested If Example
// Reads 2 integers and compares them
class CompareExample {
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int a = input.requestInt(First number:);
int b = input.requestInt(Second number:);
if (a != b){
if (a > b)
System.out.println(a+ is greater);
else
System.out.println(b+ is greater);
}else
System.out.println(the numbers are equal);
}
}
Lecture C
Slide 12 of 64
int numberOfItems =
input.requestInt(Enter number of items:);
if (numberOfItems < 0) {
System.out.println(
Number of items must be positive!);
} else {
double price = numberOfItems * ITEM_PRICE;
System.out.println(The total price is: +price);
}
Lecture C
Slide 13 of 64
Lecture C
Slide 14 of 64
Logical Operators
Boolean expressions may be combined using logical
operators
There are three logical operators in Java:
Operator
!
Operation
Logical NOT
&&
Logical AND
||
Logical OR
results
Logical NOT is unary (one operand), but logical AND and
OR are binary (two operands)
Lecture C
Slide 15 of 64
Logical NOT
The logical NOT is also called logical negation or logical
complement
If a is true, !a is false; if a is false, then !a is true
Logical expressions can be shown using truth tables
a
!a
false
true
true
false
Lecture C
Slide 16 of 64
Logical AND
The expression a && b is true if both a and b are true,
a && b
false
false
false
false
true
false
true
false
false
true
true
true
Lecture C
Slide 17 of 64
Logical OR
The expression a || b is true if a or b or both are true,
a || b
false
false
false
false
true
true
true
false
true
true
true
true
Lecture C
Slide 18 of 64
Logical Operators
Logical operators are used to form more complex logical
expressions
if (a<1 || a%2!=0)
System.out.println(
The input should be an even even number!);
Lecture C
Slide 19 of 64
Logical Operators
Full expressions can be evaluated using truth tables
a < 1
a%2!=0
a<1 || a%2=0
false
false
false
false
true
false
true
false
false
true
true
true
Lecture C
Slide 20 of 64
Boolean variables
Boolean expressions can be assigned to Boolean
variables
boolean b, c;
b = (x > 17);
c = (x>17) && (x<60);
Lecture C
Slide 21 of 64
Example - RightTriangle
// Receives the length of the edges of a triangle
// and determine if this is a right triangle
class RightTriangle {
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
float a = input.requestInt(Edge1:);
float b = input.requestInt(Edge2:);
float c = input.requestInt(Hypotenuse:);
boolean test = a*a+b*b == c*c;
if (test)
System.out.println(Its a right triangle);
else
System.out.println(Its not a right triangle);
}
}
Lecture C
Slide 22 of 64
Lecture C
Slide 23 of 64
while (condition)
statement;
If the condition is true, the statement is executed; then
Slide 24 of 64
condition
true
statement
Lecture C
Slide 25 of 64
Example - Counter
// Counts from 1 to 5
class Counter {
static final int LIMIT = 5;
public static void main(String[] args) {
int count = 1;
while (count <= LIMIT) {
System.out.println(count);
count = count + 1;
}
System.out.println(done);
}
}
Lecture C
Slide 26 of 64
Examples - Factors
// Gets an integer and prints its factors
class FactorsExample {
public static void main(String[] args) {
InputRequestor input = new InputRequestor();
int a = input.requestInt(Enter a number:);
int i = 1;
System.out.println(The divisors of +a+ are:);
while (i <= a) {
if (a%i == 0) {
System.out.println(i);
}
i = i + 1;
}
}
}
Lecture C
Slide 27 of 64
Infinite Loops
The body of a while loop must eventually make the
condition false
If not, it is an infinite loop, which will execute until the user
interrupts the program
This is a common type of logical error -- always double
check that your loops will terminate normally
Lecture C
Slide 28 of 64
Example - Forever
// This program contains an infinite loop
class Forever {
static final int LIMIT = 25;
public static void main(String[] args) {
int count = 1;
while (count <= LIMIT) {
System.out.println(count);
count = count - 1;
}
}
}
Lecture C
Slide 29 of 64
Lecture C
Slide 30 of 64
Lecture C
Slide 31 of 64
three operands
Lecture C
Slide 32 of 64
"Dimes" is printed
Lecture C
Slide 33 of 64
Slide 34 of 64
switch (expression) {
case value1:
statement-list1
case value2:
statement-list2
case
}
Lecture C
Slide 35 of 64
as an integer or character
The break statement is usually used to terminate the
statement list of each case, which causes control to jump
to the end of the switch statement
A default case can be added to the end of the list of
cases, and will execute if no other case matches
Lecture C
Slide 36 of 64
Lecture C
Slide 37 of 64
Lecture C
Slide 38 of 64
Lecture C
Slide 39 of 64
Shorthand Operators
Many operations are very commonly used
x = x + 1;
sum = sum + x;
Lecture C
Slide 40 of 64
Lecture C
Slide 41 of 64
Lecture C
Slide 42 of 64
Value Of expression
count++
add 1
old value
++count
add 1
new value
count--
subtract 1
old value
--count
subtract 1
new value
Lecture C
Slide 43 of 64
Lecture C
Slide 44 of 64
"
" +
++sum
"
" +
sum
"
" +
sum--);
27
27
27
Lecture C
Slide 45 of 64
Assignment Operators
Often we perform an operation on a variable, then store
is equivalent to
sum = sum + value;
Lecture C
Slide 46 of 64
Assignment Operators
There are many such assignment operators, always
Operator
Example
Equivalent to
+=
x+=y
x = x + y
-=
x-=y
x = x - y
*=
x*=y
x = x * y
/=
x/=y
x = x / y
%=
x%=y
x = x % y
Lecture C
Slide 47 of 64
Assignment Operators
The right hand side of an assignment operator can be a
complete expression
The entire right-hand expression is evaluated first, then
combined with the additional operation
Therefore
result
/= total-MIN;
result
/= total-MIN;
is equivalent to
result = result / (total-MIN);
Lecture C
Slide 48 of 64
Lecture C
Slide 49 of 64
the do statement
the for statement
Lecture C
Slide 50 of 64
The do Statement
The do statement has the following syntax:
do
statement
while (condition);
The statement is executed until the condition becomes
false
It is similar to a while statement, except that its
termination condition is evaluated after the loop body
Lecture C
Slide 51 of 64
The do Statement
The key difference between a do loop and a while loop
Lecture C
Slide 52 of 64
Do Statement Example
// Gets an integer and prints its factors
class AvgExample {
public static void main(String[] args){
InputRequestor input = new InputRequestor();
double x, sum=0, count=-1;
do {
x = input.RequestDouble(Next number:);
sum += x;
count++;
} while (x != 0);
// 0 is a flag indicating end of input
System.out.println(The average is +sum/count);
}
}
Lecture C
Slide 53 of 64
statement
true
condition
false
Lecture C
Slide 54 of 64
statement
The syntax of the for loop is
initialization;
while (condition) {
statement;
increment;
}
Lecture C
Slide 55 of 64
Lecture C
Slide 56 of 64
Lecture C
Slide 57 of 64
Lecture C
Slide 58 of 64
initialization
condition
false
true
statement
increment
Lecture C
Slide 59 of 64
Lecture C
Slide 60 of 64
Lecture C
Slide 61 of 64
Lecture C
Slide 62 of 64
Lecture C
Slide 63 of 64
Lecture C
Slide 64 of 64