Simple if, if else and nested if else statements in Java programming

Simple if statement execute an action if and only if the condition is true. The syntax of a simple if statement is

if(boolean expression ){
statement to be executed (s);
}

Let us do a example in which simple if statement is executed. In this program we are going to calculate the area of circle whose radius is greater than 10. That means this program will not calculate the area if radius is smaller than 10. Lets do it.


import static java.lang.Math.PI;
import java.util.Scanner;

public class ClassExercise {
 public static void main(String[] args) {
 float area;

 Scanner scanner = new Scanner(System.in);

 System.out.println("Enter the radius:");
 float radius = scanner.nextFloat();

 if(radius > 10){
 area = (float) (radius*radius*PI);
 System.out.println("The area is: "+area);
 } }
}

This program will print the area only when the radius is greater than 10.

Result:

Enter the radius: 17
The area is: 907.9203

It doesn’t have any idea what to do if the radius is smaller than 10. In this case we multiple if statement. Let us see the example below:


import static java.lang.Math.PI;
import java.util.Scanner;

public class multipleif {
 public static void main(String[] args) {
 float area;
 float perimeter;
 Scanner scanner = new Scanner(System.in);
 System.out.println("Enter the radius:");
 float radius = scanner.nextFloat();
 if(radius > 10){
 area = (float) (radius*radius*PI);
 System.out.println("The area is: "+area);
 }
 if (radius < 10){
 perimeter = (float)(2*PI*radius);
 System.out.println("The perimeter is: " +perimeter);
 }}

}

Result:

If radius is greater than 10

Enter the radius:
13
The area is: 530.92914

If radius in smaller than 10

Enter the radius:
5
The perimeter is: 31.415926

As you can see this program prints area if the radius is greater than 10 and perimeter if the radius is smaller than 10. Let us do another example which determines whether the number is even or odd. We know that the number divisible by 2 is even and that not divisible by 2 is odd.


import java.util.Scanner;

public class EvenOddUsingIf {

 public static void main(String[] args){

 Scanner scanner = new Scanner(System.in);
 System.out.println("Input the number: ");
 float number = scanner.nextInt();

 if ( number % 2 == 0){
 System.out.println(number+" is a even number");
 } else {
 }
 if(number % 2 != 0){
 System.out.println(number+" is an odd number"); 

 } }}

Result

Input the number: 
7
7.0 is an odd number

Input the number: 
8
8.0 is a even number

The boolean expression should always be enclosed within parenthesis.
For example:

Wrong:

if(number>0) && (number < 50)

Correct:

if ((number>0) && (number < 50))

As we have seen a simple if statement executes an command if the condition specified by Boolean expression is true but it does nothing if the condition specified is false.  So, if the conditions are either true or false then we can use if…..else statements. The general syntax for if….else statement is

if (booleanExpression){
statement(a);
}
else{
statement(b);

We can do the example number 1 using if…else statement as below:


import static java.lang.Math.PI;
import java.util.Scanner;

public class isimple_if_else {
 public static void main(String[] args) {
 float area;
 float perimeter;

 Scanner scanner = new Scanner(System.in);

 System.out.println("Enter the radius:");
 float radius = scanner.nextFloat();

 if(radius > 10){
 area = (float) (radius*radius*PI);
 System.out.println("The area is: "+area);

 }
 else {
 perimeter = (float)(2*PI*radius);
 System.out.println("The perimeter is: " +perimeter);

 } } }

Result:

Enter the radius:
5
The perimeter is: 31.415926

Enter the radius:
13
The area is: 530.92914

As you can see here we replace second if (booleanExpression) with else and there is no change in result. In case of multiple alternatives we can use Nested if statement. As for example:

if (i>k){
if(j>k){
System.out.println(" i and j both are greater than k ")

}
else{
System.out.println("i is less than or equl to k")
}
}

As a common example ae can use grading of student from A to D:


import java.util.Scanner;

public class Grading {

 String grade;

 public static void main(String[] args){

 Scanner scanner = new Scanner(System.in);

 System.out.println("Enter the marks obtained:");

 int marksobtained = scanner.nextInt();

 if(marksobtained >= 80){
 System.out.println("Grade = A");
 }
 else
 if(marksobtained >= 70){
 System.out.println("Grade = B");
 }
 else
 if(marksobtained >=60){
 System.out.println("Grade=C");

 }
 else
 if(marksobtained >=40){

 System.out.println("Grade = D");

 }
 else{
 System.out.println("Fail");
 } }
}

Result:

Enter the marks obtained:
65
Grade=C

Enter the marks obtained:
45
Grade = D

Enter the marks obtained:
25
Fail

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s