-
Notifications
You must be signed in to change notification settings - Fork 1
Decision Making
This week we will learn the basics of decision making in Java and the different comparison operators used by Java. This guide will help you create a program that will act in different ways based on certain conditions.
Java has several different comparison operators
Operator | Name | Example |
---|---|---|
> | Greater Than | X > Y |
>= | Greater Than Or Equal | X >= Y |
< | Less Than | X < Y |
<= | Greater Than Or Equal | X <= |
== | Equal | X == Y |
!= | Does Not Equal | X != Y |
The If/Else statement is used by Java to determine whether it should run a line (or lines) of code or not. Once the Java program finds the "If" statement it will check the condition in parenthesis and see if the condition is fulfilled before running the following lines of code.
For example:
> public class IfStatement{
> public static void main(String[] args) {
> int x = 7;
> int y = 6;
> if (x > y)
> System.out.println("X is greater than y");
> }
> }
X is greater than y
The "Else" statement exist as a means of telling the program to execute some code if the conditions are NOT fulfilled.
For example:
> public class ElseStatement{
> public static void main(String[] args) {
> int x = 5;
> int y = 6;
> if (x > y)
> System.out.println("X is greater than y");
> else
> System.out.println("X is less than y");
> }
> }
X is less than y
To run several lines of code if a certain condition is fulfilled use the open bracket sign "{" after the if/else condition and the close bracket sign "}" at the end of the line of code you want to run
For example:
> public class IfElseStatement{
> public static void main(String[] args) {
> int x = 5;
> if (x == 5){
> System.out.println("X is 5");
> System.out.println("X is not higher than 5");
> System.out.println("X is not lower than 5");
> }
> else{
> System.out.println("X is not 5");
> }
> }
> }
X is not 5
X is not higher than 5
X is not lower than 5
What if you want to run line X if condition X is fulfilled, line Y if condition Y is fulfilled and line Z if neither is fulfilled? Use the "else if" statements!
For example:
> public class ElseIfStatement{
> public static void main(String[] args) {
> int x = 5;
> int y = 6;
> if (x > y)
> System.out.println("X is greater than y");
> else if (x < y)
> System.out.println("X is less than y");
> else
> System.out.println("X is equal to y");
> }
> }
X is less than 5
Java has two major loop functions. One of them is the While loop which will continuously run some lines of code as long as the conditions are (or is not) fulfilled.
For example:
> public class WhileLoop{
> public static void main(String[] args) {
> int x = 0;
> while (x != 5){
> System.out.println(x);
> x = x+1;
> }
> }
> }
0
1
2
3
4
In the above example, the same lines of code: System.out.println(x); and x = x+1; continuously executed until the while loop condition was fulfilled. Notice how the system did not print 5. The reason is because the while loop condition is: while x DOES NOT equal 5. Once x became 5 the loop condition was broken.
When using If/Else statements, "Else" statements are not always required; however, "If" statements are required before using "Else" statements.
For example:
> public class IfElse2{
> public static void main(String[] args) {
> int x = 5;
> int y = 6;
> if (x > y)
> System.out.println("X is greater than y");
> }
> }
Will run successfully
For example:
> public class ElseStatement{
> public static void main(String[] args) {
> int x = 5;
> int y = 6;
> else
> System.out.println("X is less than y");
> }
> }
Will crash and burn
Normal comparison operators will not work when comparing strings
For example:
> public class CompareString{
> public static void main(String[] args) {
> String x = new String("statement 1");
> String y = new String("statement 1");
> if (x == y)
> System.out.println("X is the same as y");
> else
> System.out.println("X is not the same as y");
> }
> }
X is not the same as y
To compare strings use the ".equals()"
For example:
> public class CompareString2{
> public static void main(String[] args) {
> String x = new String("statement 1");
> String y = new String("statement 1");
> if (x.equals(y))
> System.out.println("X is the same as y");
> else
> System.out.println("X is not the same as y");
> }
> }
X is the same as y
The reason being is that == checks for reference whereas .equals() check for value stored within. You will learn more about this when learning about objects.
The ! operator can be used as NOT when comparing statements
For example:
> public class NotUsage{
> public static void main(String[] args) {
> int x = 5;
> int y = 6;
> if (!(x > y))
> System.out.println("X is not greater than y");
> }
> }
Will print X is not greater than y