Basic if statements are used to allow computers to take one of two courses of action and is an example of a branching or decision statement
If else statements are used every day to allow us to make decisions and perform different activities as we compare the pros and cons of our decisions. In most cases, the process is automatic since we have performed the activity for so long we do not think of it as performing if else statements.
An example of an everyday if else statement is with travel arrangements for example travelling by bus. In order to take the bus, you need a valid bus pass, the exact change for bus fare or more than the bus fare, assuming the bus driver does not give change. In this case the if else statement could be: If I have a valid bus pass, bus fare or more for my trip then I can take the bus otherwise I cannot board the bus. In terms of pseudo code we can write
If (bus pass is valid OR I have bus fare or more) then
(I can take the bus)
Else
(I cannot take the bus)
This example has two branches, one where the condition is true and one where the condition is false. In the branch where the condition is true, I have a valid bus pass, bus fare or more than bus fare so I can take the bus while in the second branch, the condition is false so I do not have a bus pass or have less than the bus fare so I cannot take the bus.
Relational operators are used to determine the relationship between operands i.e. whether one operand is less than, greater than, equal to or not equal to another operand . The example has only one relational operator used which is greater than or equal to “> =”. It is used when considering whether I have bus fare or more i.e. is the money I have >= bus fare.
Boolean operators are also known as logical operators and return a Boolean result based on the Boolean result of one or two other expressions. Examples of Boolean operators are AND, OR, Conditional AND, Conditional OR, NOT and Xor. In the example the Boolean operator used is OR “||” when we check whether I have a valid bus pass OR sufficient bus fare.
If the Boolean operator is changed, the outcome will change. For example if we replace OR with AND then I would need to have a bus pass AND sufficient bus fare in order to board the bus. If I initially had only bus fare then using OR means I will be able to take the bus but changing this to AND means I cannot take the bus because I do not have a bus pass as well.
Assuming bus fare is $2.00, the Java program to check for the bus fare is below
Import java.util.*;
Public class BoardBus {
public static void main(String[] args){
//Initialize values
double busfare = 0;
string buspass = “N”;
Scanner input = new Scanner (System.in);
//Check if user has bus pass
System.out.println (“Do you have a bus pass (Y/N) :”);
buspass = input.next();
//If user does not have bus pass, check if they have bus fare
//Any answer other than Y means no bus pass
If ((busspass != “Y”)||(busspass != “y”)) {
System.out.println("Please enter the money you have for bus fare:");
busfare = input.nextDouble();
}
//Check if user can board bus and display appropriate message
if((buspass == “Y”) || (buspass == “y”) || (busfare >= 2.00)){
System.out.println("You can board the bus");
}
Else {
System.out.println("Sorry, you cannot board the bus.");
}
}
}
References
Eck, D. J. (2011). Introduction to Programming Using Java (7th ed.). Retrieved from Hobart and William Smith College Math and Computer Science Department: http://math.hws.edu/javanotes/c3/s1.html
if else statement in Java. (2016). Retrieved from Tutorials Point: http://www.tutorialspoint.com/java/if_else_statement_in_java.htm
if Statement. (2016). Retrieved from Microsoft Developer Network: https://msdn.microsoft.com/en-us/library/windows/desktop/bb509610(v=vs.85).aspx
Lowe, D. (2016). Logical Operators in Java. Retrieved from For Dummies: ww.dummies.com/how-to/content/logical-operators-in-java.html
The Java Tutorials. (2015). Retrieved from Oracle: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html