-
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.