
Hola gyz, today we are going to learn about a frequently used coding technique in java. The if statements.
Alright now, think of a simple program that asks you to input your age and tells you whether you are eligible to vote in the next election or not. (Usually, in most countries, the minimum required age to get the voting right is 18 years).
So how do we write a code for this simple problem??
Here’s the step-by-step process.
- Asks the user to enter his/her age and save the input to a variable, say, age.
- If the age is equal to or above 18 years, then print that he/she is eligible to vote.
- Else, print that he/she is ineligible to vote.
In this case, If-else statements in java come to help. Here’s how to write the actual code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Scanner; public class IfStatements { public static void main(String[] args){ int age; Scanner input = new Scanner(System.in); System.out.print("Please enter your age: "); age = input.nextInt(); if(age>=18){ System.out.println("You have the right to vote."); } else{ System.out.println("Sorry, you are not eligible to vote."); } } } |
The basic format of If-else statements is
if (<condition>)
{
<statement/s>
}
else
{
<statement/s>
}
Actually, if only one statement is inside the block, then we don’t need a block. You may write;
if (<condition>)
<statement/s>
else
<statement/s>
So the previous example can be rewritten as follows, without the blocks.
1 2 3 4 5 |
if(age>=18) System.out.println("You have the right to vote."); else System.out.println("Sorry, you are not eligible to vote."); |
The output of (age>=18) is a boolean value. That is either true or false. If the condition is true, then the statement under if is executed. Else the statement under else is executed.
Now maybe you have seen that the way the computer runs the program has changed. Previously, the computer read the code line by line.
But now, definitely, some part of the code will skip according to the input.
Suppose you have entered 10 for the input. Now, since the if condition is false, the statement inside if block is not executed. It just skips and moves to else statement.
Therefore, if statement is said to be a “control structure” in java.
Imagine some guy has entered 500 or -23 as his age. Those values for the ages are quite impossible. Now we can do something about it. Use an if statement.
1 2 3 4 5 6 7 8 9 10 11 12 |
if (age>120||age<0) { System.out.println("Sorry, the age you entered is not valid"); System.out.print("Please enter your age again: "); age = input.nextInt(); } if(age>=18) System.out.println("You have the right to vote."); else System.out.println("Sorry, you are not eligible to vote."); |
Now let’s see how this works. First if statement checks whether the age you entered is larger than 120 OR less than 0. If you have entered an acceptable answer, then the first if block is skipped and you jump to the bottom of the code that have the previous if-else blocks.
In the statement, if (age>120 || age<0) means that “if age is larger than 120 OR age is less than 0………”
Either one of these two relational operators should be true in order to execute if block.
One final point. In previous example, you used if-else statements to run the program. But you can have infinite conditions by having else if statement in the middle.
if (<condition>)
{
<statement/s>
}
else if (<condition>)
{
<statement/s>
}
else if (<condition>)
{
<statement/s>
}
.
.
.
else
{
<statement/s>
}
If nothing in the if and else if conditions is true, then finally the last else statement is executed.
You can of course exclude the else statement. But then, if neither of the if or else if conditions are true, no any statement inside those if and else if blocks will be executed.
The program just proceeds down..
Huge lesson huh.. 🙁 Boring…. Don’t be upset. We have to sacrifice a bit to get something. Stay focused. Let’s move to our next lesson…!