Lesson Objective
- Understand Selection and how to use IF Statements in Java programming.
KS3, GCSE, A-Level Computing Resources
If statements are used to make decisions in your programs. Different parts of your code will run depending on whether or not a condition is true or false.
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions. The return value of a comparison is either true or false.
Operator | Description | Example |
---|---|---|
== | Equal to | if (total == 5) |
!= | Not equal to | if (total != 5) |
> | Greater than | if (total > 5) |
< | Less than | if (total < 5) |
>= | Greater than or equal to | if (total >= 5) |
<= | Less than or equal to | if (total <= 5) |
&& | AND Operation | if (x < 5 && x < 10) |
|| | OR Operation | if (x < 5 || x < 10) |
! | NOT Operation | if !(total <= 5) |
Java has the following conditional statements:
Use the if statement to specify a block of Java code to be executed if a condition is true.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); int num1, num2; System.out.println("Enter number 1: "); num1 = scan_ob.nextInt(); System.out.println("Enter number 2: "); num2 = scan_ob.nextInt(); if (num1 > num2){ System.out.println("num1 bigger than num2"); } else { System.out.println("not bigger than num2"); } } } //“>” operator is used to check is num1 is bigger than num2
Enter number 1: 3 Enter number 2: 1 num1 bigger than num2
Use the else statement to specify a block of code to be executed if the condition is false.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); int num1, num2; System.out.println("Enter number 1: "); num1 = scan_ob.nextInt(); System.out.println("Enter number 2: "); num2 = scan_ob.nextInt(); if (num1 > num2){ System.out.println("num1 bigger than num2"); } else { System.out.println("not bigger than num2"); } } } //If the if condition is FALSE, then code in the else statement is executed.
Enter number 1: 3 Enter number 2: 4 not bigger than num2
The .equals method must be used when comparing string data types. This is because Strings are non-primitive data types.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); String name; System.out.println("Enter your first name: "); name = scan_ob.nextLine(); System.out.println(name); if (name.equals("Ahmed")){ System.out.println("Welcome MR AHMED"); } else { System.out.println("Sorry, bye!"); } } } // “==” is only used to compare primitive data types. Something about memory locations.
Enter your first name: Ahmed Welcome MR AHMED
When “==” is used the condition will evaluate to FALSE. Even when it appears to match.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); String name; System.out.println("Enter your first name: "); name = scan_ob.nextLine(); System.out.println(name); if (name == ("Ahmed")){ System.out.println("Welcome MR AHMED"); } else { System.out.println("Sorry, bye!"); } } }
Enter your first name: Ahmed Sorry, bye!
If statements are used to make decisions in your programs. Different parts of your code will run depending on whether or not a condition is true or false.
Use the else if statement to specify a new condition if the first condition is false.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); String name; System.out.println("Enter your first name: "); name = scan_ob.nextLine(); System.out.println(name); if (name.equals("Ahmed")){ System.out.println("Welcome MR AHMED"); } else if (name.equals("Shanawaz")) { System.out.println("Welcome MR SHANAWAZ"); } else { System.out.println("Sorry, bye!"); } } } // The else if statement has been used to allow more options in your if statements.
Enter your first name: Shanawaz Welcome MR SHANAWAZ
Instead of writing many if..else statements, you can use the switch statement.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); int num; System.out.println("Enter an integer: "); num = scan_ob.nextInt(); System.out.println(num); switch (num){ case 23: System.out.println("Option 1 working"); break; case 45: System.out.println("Option 2 working"); break; case 67: System.out.println("Option 3 working"); break; case 89: System.out.println("Option 4 working"); break; } } //When Java reaches a break keyword, it breaks out of the switch block. //A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.
Enter an integer: 45 Option 2 working --------------- Enter an integer: 67 Option 3 working
IF Statements inside another IF Statement structures are referred to as nested IF Statements.
if statement can be used within other ifs, to expand options.
import java.util.Scanner; public class Hello_World_1 { public static void main(String args[]){ Scanner scan_ob = new Scanner(System.in); String q1, q2; System.out.println("Do you like Penguins?: "); q1 = scan_ob.nextLine(); if (q1.equals("Yes")){ System.out.println("Can Penguins fly?:"); q2 = scan_ob.nextLine(); if (q2.equals("Yes")){ System.out.println("Awesome, i like too"); } else { System.out.println("You sure??"); } } else { System.out.println("Oh ok, bye!"); } } } // This if statement is inside the other if statement.
Do you like Penguins?: Yes Can Penguins fly?: No You sure??