Boolean And Relational Operators And Logic: Essay Example

Type of paper: Essay

Topic: Temperature, Boolean, Expression, Print, Room, Java, Evaluation, Operator

Pages: 4

Words: 1100

Published: 2020/10/08

New WowEssays Premium Database!

Find the biggest directory of over
1 million paper examples!

Boolean expressions are those expressions that evaluate to Boolean values, that is, values that are either of two choices i.e. true or false, 1 or 0, yes or no, etc.. Boolean operators cat on Boolean values and include simple English statements such as AND, OR, and NOT which are denoted as “&&”, “| |”, and “!” respectively (without the quotes). Relational operators can also be used to test Boolean expressions, and they include: > (greater than), < (lesser than), <= (lesser than or equal to), and >= (greater than or equal to).
In order to make decisions on the premise of Boolean expressions being either true or false, if statements are used. There are four types of if statements: the simple if statement, the if-else statement, the if-else-if statement, and the nested if-else statement.
All if statements consist of two parts, a Boolean expression enclosed in parentheses (), and the block of statements (body) enclosed within curly braces {} as shown below.

If (Boolean_expression)

{
//Statements will execute if the Boolean expression is true
}

For the if-else statement, the structure is:

If (Boolean_expression) {
//Executes when the Boolean expression is true
} else {
//Executes when the Boolean expression is false
}

The following examples show how if statements work and can be used in real life.

Simple if statement: The simplest if statement comprises a Boolean expression accompanied by a statement or block of statements to be executed if the Boolean expression evaluates to true. If the Boolean expression is evaluated as false, the statements in the block are skipped. Below is an example:
if (temperature > 100){
print (“The temperature above 100 degrees”);
}
The above code executes and prints out the statement “The temperature above 100 degrees” only when the temperatures is above 100 degrees. If the (>=) greater than or equal to relational operator is used instead of the greater than (>) operator, the statement will be printed out when the temperature is at 100 degrees or more.
If else statement: The if-else statement is used when choosing between one or two possible actions i.e. after evaluation of the Boolean expression, only one action is picked but not both. For example:
if (temperature >= 100){
print ("Temperature above 100 degrees");
}
else {
print ("Temperature below 100 degrees");
}
The above code evaluates whether the temperature is equal to or above 100 degrees and if the condition is met, the first statement "Temperature above 100 degrees" is printed. If not, the second statement is printed.
When Boolean operators are included, different results may be expected as shown in the examples below. The AND (&&) Boolean operator evaluates to true only when the two Boolean values being evaluated are true as shown below.
if ((score >= 20) && (temperature <= 27)) {
print ("Temperature within room temperature range”);
else {
print ("Temperatures out of room temperature range");
}
When the temperature is above or equal to 20 degrees, and less than or equal to 27 degrees the Boolean expression evaluates to true and the statement "Temperature within room temperature range” is output. If one of either condition is not met, i.e., the temperature is not within the 20-27 degrees range, the second statement “Temperature out of room temperature range” is printed.
If the AND (&&) operator is replaced with the OR (||) Boolean operator, the statement evaluates to true when the temperature is either greater than or equal to 20 degrees or when it is less than or equal to 27 degrees. The statement is output as long as one of the Boolean values in the expression is evaluated as true e.g. a temperature of 28 degrees is valid since the value is greater than 20 degrees even though it is not less than 27 degrees. On the other hand, a temperature of 19 degrees is less than 27 degrees and for this reason, true even if it not greater than 20 degrees. When the OR (| |) operator is used, any value used satisfies the Boolean expression thus resulting in ambiguity since the expression will never evaluate to false.

If-else ifelse statement: The if-else ifelse is used when more than two options are to be compared. For example:

if (temperature>=100) {
print ("Extremely Hot");
}
else if (temperature>= 75) {
print ("Very Hot");
}
else if (temperature>= 50) {
print ("Hot");
}
else if (temperature>= 25) {
print ("Room Temperature");
}
else {
print ("Temperature below room temperature");
}
The if-else chain analyzes each test top to bottom and if the first test checks as true, the corresponding body is executed and the whole chain is exited. In the above code, if the temperature is 70 degrees or anything between 50-74 degrees, the program prints “Hot” and the chain is exited. The else if chain thus chooses one statement to execute from many while the final else in the chain plays a final catch-all role in the case all the other statements preceding it are false. To design the code above, all expressions must be evaluated in the order they are written, and each test relies on the fact the preceding test has been checked already and deemed false. For example, if the chain reaches the (temperature>= 75) line, it is evident that (temperature>=100) is false, so the temperature is 99 or less.
Nested if statements: This refers to if statements embedded in other if or if-else statements. The general syntax is shown below.

If (Boolean_expression X) {

//Executed when the expression X evaluates as true

If (Boolean_expression Y) {

//Executed when the expression Y evaluates as true
}
}

Java code using Boolean operators, relational operators and if-else statements:

The example will use the if-else-if statements since they combine almost all forms of if-else statements.
public class Temparature_test { //Program to monitor a thermostat
public static void main(String args[]){
int temp = 30; //temperature value variable in degrees Celsius
if( temp <= 100 ){
System.out.println("Extremely Hot");
}else if( temp <= 75 ){
System.out.println("Very Hot");
}else if( temp <=50 ){
System.out.println("Hot");
}else if( temp >=20 )&& (temp <= 25){
System.out.println("Room Temperature");
}else{
System.out.println("Temperature is below room temperature");
}
}
}

References:

Docs.oracle.com,. (2014). the if-then and if-then-else Statements (The Java Tutorials > Learning the Java Language > Language Basics). Retrieved 20 January 2015, from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Math.hws.edu,. (2014). Java notes 7.0, Section 2.5 -- Details of Expressions. Retrieved 20 January 2015, from http://math.hws.edu/javanotes/c2/s5.html
Tutorialspoint.com,. (2014). Java Decision Making - ifelse if and switch statements. Retrieved 20 January 2015, from http://www.tutorialspoint.com/java/java_decision_making.htm

Cite this page
Choose cite format:
  • APA
  • MLA
  • Harvard
  • Vancouver
  • Chicago
  • ASA
  • IEEE
  • AMA
WePapers. (2020, October, 08) Boolean And Relational Operators And Logic: Essay Example. Retrieved September 25, 2021, from https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/
"Boolean And Relational Operators And Logic: Essay Example." WePapers, 08 Oct. 2020, https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/. Accessed 25 September 2021.
WePapers. 2020. Boolean And Relational Operators And Logic: Essay Example., viewed September 25 2021, <https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/>
WePapers. Boolean And Relational Operators And Logic: Essay Example. [Internet]. October 2020. [Accessed September 25, 2021]. Available from: https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/
"Boolean And Relational Operators And Logic: Essay Example." WePapers, Oct 08, 2020. Accessed September 25, 2021. https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/
WePapers. 2020. "Boolean And Relational Operators And Logic: Essay Example." Free Essay Examples - WePapers.com. Retrieved September 25, 2021. (https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/).
"Boolean And Relational Operators And Logic: Essay Example," Free Essay Examples - WePapers.com, 08-Oct-2020. [Online]. Available: https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/. [Accessed: 25-Sep-2021].
Boolean And Relational Operators And Logic: Essay Example. Free Essay Examples - WePapers.com. https://www.wepapers.com/samples/boolean-and-relational-operators-and-logic-essay-example/. Published Oct 08, 2020. Accessed September 25, 2021.
Copy

Share with friends using:

Please remember that this paper is open-access and other students can use it too.

If you need an original paper created exclusively for you, hire one of our brilliant writers!

GET UNIQUE PAPER
Related Premium Essays
Contact us
Chat now