Conditional Statements
Boolean Expressions:We remember that the Boolean data type can hold one of two values, either TRUE or FALSE. We can use these Boolean values to make decisions in our code. In computer programming, an expression is a combination of values, variables and operators that computes a “return” value. Just like in mathematics this process is called evaluating the expression.
Example:
Example:
(50 < 100) would return a value of TRUE because 50 is less than 100
What is the output of the following code?
println(80 < 50);
This code would output false because 80 not less than 50.
The general syntax of a Boolean expression is as follows:
(operand1 relational-operator operand2)
A relational operator tests or defines the relationship between the two operand values.
Example:
- (10 + 30) is an expression and is evaluated to return a value of 40
- Boolean expressions are expressions that when evaluated return a Boolean (true or false) value.
Example:
(50 < 100) would return a value of TRUE because 50 is less than 100
What is the output of the following code?
println(80 < 50);
This code would output false because 80 not less than 50.
The general syntax of a Boolean expression is as follows:
(operand1 relational-operator operand2)
A relational operator tests or defines the relationship between the two operand values.
*NOTE: A single = is used to assign a value, a double == is used to compare values.
Example:
What is the output of the following?
println( 16 >= 16);
int paperSales = 18;
println(paperSales > 16);
int paperSales = 18;
println( 25 <= paperSales);
Fill in the blank so the expression returns TRUE
int capacity = 5;
println( 4 capacity);
** NOTE: Operands can be any of the primitive data types. Strings and other objects cannot be compared using relational operators.
What is the output of the following?
println( 16 >= 16);
int paperSales = 18;
println(paperSales > 16);
int paperSales = 18;
println( 25 <= paperSales);
Fill in the blank so the expression returns TRUE
int capacity = 5;
println( 4 capacity);
** NOTE: Operands can be any of the primitive data types. Strings and other objects cannot be compared using relational operators.