×

Java If Keyword

Definition:

  • The if statement specifies a section of Java code that will run if an if statement's condition is false.
  • The following conditional statements can be used in Java:
    • To provide a block of program to run or execute only if a certain condition is true, then we need to use the if block to execute the program.
    • If the given or provided condition is false, then we will describe it in the else block of code that should be printed.
    • To define various code blocks to be performed, use the switch command.
    • In if-else statement, the Java keyword or reserved word else is used in executing the opposite statement to the if keyword.
  • When an if statement is false, the otherwise statement is always carried out. It is a branching statement component that is optional.
  •  Optional else part statements are carried out when the if statement returns false.

Syntax

Below is the declaration or Syntax of an else keyword in java programming language.

if(condition)
{
<state> //the block or portion of if statements
}
else
{
<state> //the portion or part where the else statements are involved or executed.
}

Control flow of the program starts with the if section and performs those statements if the Boolean expression is false.

Simple Example:

if(test_expression) {
//true condition is excuted in this block of statements
System.out.println("True condition: Raju is well characterised person");
}
else {// false conditional statement is executed in this portion
System.out.println("False condition:Raju is not a characterised person");
}

Let us consider the example programs to understand better about else keyword.

Examples for If Keyword in Java

Example 1:

class If{
public static void main (String args []) {
double age=21;
if(age>21) {
//It is executed if condition is true
System.out.println("you are eligible for marriage");
}
else {//executed if the given condition is false statement
System.out.println("you are not eligible for marriage ");
}
}
}

Output of the code is executed; it produced the following result:

You are not eligible for marriage

Example 2:

//A Java Program to demonstrate the use of if-else statement.  
//It is a program of odd and even number
public class If_lseEx {  
public static void main (String [] s) {  
    //variable declaration
    int integer=13;
    ///Verify whether the number is divisible by two.
    if(integer%2==0) {  
        System.out.println("Given integer is even");
    } else {  
        System.out.println("Given integer is odd");
    }  
}  
}  

Output of the given program is:

Given integer is odd

Example 3:

Example program of leap year

If a year can be divided by 4 and 400, it is a leap year. not by 100, though.

public class LeapYearEx3 {    
public static void main (String [] s) {    
double year=2020.    
    if (((year % 4 ==0) && (year % 100! =0)) || (year % 400==0)) {  
        System.out.println("The year is a leap year");
    }  
    else {  
        System.out.println("The year is not a leap year");
    }  
}    
}    

Output of the program is given by:

The year is a leap year

Example 4:

/Java programme to show how the If-else-if ladder is used.
/
It is a programme that assigns grades for failure, a D, a C, a B, an A, and an A+.
public class If_Else_IfEx4 {  
public static void main (String [] s) {  
    int score=83;


    if(score<50) {  
        System.out.println("unsuccess");
    }  
    else if(score>=50 &&score<60) {  
        System.out.println("D points");
    }  
    else if(score>=60 &&score<70) {  
        System.out.println("C points");
    }  
    else if(score>=70 &&score<80) {  
        System.out.println("B points");
    }  
    else if(score>=80 &&score<90) {  
        System.out.println("A points");
    } else if(score>=90 &&score<100) {  
        System.out.println("A+ points");
    } else {  
        System.out.println("Invalid statement!");
    }  
}  
}  

Output of the program is:

C points

Example 5:

Program to check whether a given number is POSITIVE, NEGATIVE or ZERO:

public class P_N_Ex5 {    
public static void main (String [] s) {    
    int Integer=-182;
    if (Integer>0) {  
    System.out.println("The number is positive");
    } else if (Integer<0) {  
    System.out.println("The number is negative");
    } else {  
    System.out.println("The number is zero");
   }  
}    
}    

Output of the program is

The number is negative.

Example 6:

/Java Program to Show Nested If Statements in Action.
public class NestedIfEx6 {    
public static void main (String [] s) {    
doubleyears=20;
doubleheight=120;
    //applying condition on years and height  
    if(years>=18) {    
        if(height>50) {  
            System.out.println("can donate the blood");
        }    
    }    
}}  

Output of the program is:

Can donate the blood

Related Topics

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

How to add 4 Hours to the Current Date in Java?

In this tutorial, we will learn how to add 4 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java String endsWith() method

Checks whether this String ends with the specified suffix or not. Syntax public boolean endsWith(String Suffix) Parameter Suffix : It takes suffix as a parameter  i.e. Sequence of characters Returns It returns true when the current...

1 minute read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.