Expressions As Operands
Expression as Operand
When using an expression as an operand, the expression is evaluated before doing the comparison
( 6 > (10-5)
Example:
A warehouse has a capacity of 25 boxes. This program displays FALSE if the warehouse is above capacity and TRUE if the warehouse is below capacity.
// The capacity of the warehouse
int capacity = 25;
//The amount of initial product
int product = 50;
//The amount of product sold
int sold = 30;
//display true or false
println(capacity >product-sold)
What is the output?
This program would evaluate as _____________.
What is the output of the following ?
println (36 < 16 + 20 -5);
int product = 16 -10;
println(product > 10);
int firstSum = 10 + 5;
int secondSum = 16 + 2;
println( firstSum <= secondSum);
println(35.5 < 36);
println(25 >= 16 * 2 (5 + 8);
double personalBest = 46.25;
int worldRecord = 65;
println(personalBest > worldRecord);
When using an expression as an operand, the expression is evaluated before doing the comparison
( 6 > (10-5)
- The expression is evaluated FIRST (10-5) = 5
- The program then checks to see if 5 is less than 6
- This expression would evaluate to TRUE
Example:
A warehouse has a capacity of 25 boxes. This program displays FALSE if the warehouse is above capacity and TRUE if the warehouse is below capacity.
// The capacity of the warehouse
int capacity = 25;
//The amount of initial product
int product = 50;
//The amount of product sold
int sold = 30;
//display true or false
println(capacity >product-sold)
What is the output?
This program would evaluate as _____________.
What is the output of the following ?
println (36 < 16 + 20 -5);
int product = 16 -10;
println(product > 10);
int firstSum = 10 + 5;
int secondSum = 16 + 2;
println( firstSum <= secondSum);
println(35.5 < 36);
println(25 >= 16 * 2 (5 + 8);
double personalBest = 46.25;
int worldRecord = 65;
println(personalBest > worldRecord);
Boolean Variables:
A Boolean variable can only have one of two values; TRUE or FALSE. Since the value of the variable is already a Boolean, we do not have to evaluate Boolean variables in the expression
Example:
boolean isDone = TRUE;
println( isDone == TRUE);
Since this is a Boolean variable comparing it to the value is redundant. Instead, write the code like this…
boolean isDone = TRUE;
println( isDone);
Looking at the code..
boolean isTrue = (11>2);
println(isTrue);
Will produce the output: TRUE.
A Boolean variable can only have one of two values; TRUE or FALSE. Since the value of the variable is already a Boolean, we do not have to evaluate Boolean variables in the expression
Example:
boolean isDone = TRUE;
println( isDone == TRUE);
Since this is a Boolean variable comparing it to the value is redundant. Instead, write the code like this…
boolean isDone = TRUE;
println( isDone);
Looking at the code..
boolean isTrue = (11>2);
println(isTrue);
Will produce the output: TRUE.