Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java If-else Statement

  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography
  • Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड
    Lanka Kand Summary in Hindi | Ram Vs Ravana | लंका काण्ड Spiritual
  • World Environment Day in Hindi : Objective, Importance, Theme
    World Environment Day in Hindi : Objective, Importance, Theme General Knowledge
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड
    Sunder Kand in Hindi | Hanuman Kand | हनुमान जी का सुंदरकांड Spiritual
  • Ohms law Definition, Formula, Statement
    Ohms law Definition, Formula, Statement | ओम का नियम Science
  • Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी
    Kishore Kumar Biography in Hindi | किशोर कुमार की जीवनी Biography

Java If-else Statement

Posted on August 21, 2021October 11, 2021 By GeekCer Education
Java If-else Statement

A Java if-else statement performs a task, depending on whether the given condition is true or false. Here task is a single statement or group of statements.

In other words, The if-else statement tells the program to execute a specific section of code only if the particular condition is true.

It allows the computer to first test the expression and then, depending on whether the condition is true or false, it transfers the control to a particular statement. So you have to go through the two paths, one for true condition and other for false condition (If available).

Java If-else Statement control statement

Table of Contents

  • What is control statement?
  • Types of If-else statement
    • 1. If statement
    • 2. Java if-else statement
    • 3. Java if-else-if ladder or else-if statement
    • 4. Java nested if statement

What is control statement?

The control statement changes the flow of execution and provides better control to program on the flow of execution.

The control statement, which enables you to execute different block of code based on a simple condition in Java. It is nearly identical to if statements in C or C++. If condition contain the keyword if, followed by a boolean condition, followed by a statement to execute if the condition is true.

Types of If-else statement

There are many ways to use if statement in Java language which are as follows:

  • if statement
  • if-else statement
  • if-else-if ladder or else-if statement
  • nested if statement

1. If statement

In this statement, there is only one condition, If the condition is true then the statement inside the If will execute. This is the most basic statement.

You must put the condition inside the parentheses (<condition>).


if (condition) {
   // Code to be executed if the condition is true
}

2. Java if-else statement

In this statement, If condition-1 is true, then code inside the condition-1 will execute, if condition-1 is false then code inside else will execute.


if (condition-1) {
   // Code to be executed if the condition is true
} else {
   // Code to be executed if the condition is false
}

3. Java if-else-if ladder or else-if statement

In this statement, if condition-1 is true, then code inside condition-1 will execute, if condition-1 is false then condition-2 will be checked if it is true then code inside condition-2 will execute. If condition-2 is false then condition-3 will be checked if it is true then code inside the condition-3 will execute otherwise else part will be executed.


if (condition-1) {
   // Code to be executed if the condition 1 is true
} else if (condition-2) {
   // Code to be executed if the condition 1 is false and condition 2 is true
} else if (condition-3) {
   // Code to be executed if the condition 1 is false and condition 2 is false and condition 3 is true
} else  {
   // Code to be executed if the condition 1, condition 2 and condition 3 are false
}

4. Java nested if statement

The nested if statement means a if statement inside the if statement. In other words, if the condition-1 is true then condition-2 will be checked, if condition-2 is true then code inside the condition-2 will be executed.


if (condition-1) {
    if (condition-2) {
     // Code to be executed if the condition 1 is true and condition 2 is true
   }
} 

In this section, we have used the word “statement”. The statement represents a single time execution from top to bottom.

The difference between if condition in Java and C or C++ is that Java condition must return a boolean value (true or false). Unlike in C, the condition cannot return an integer.

You can use Relational Operators to perform different actions for different decisions.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Java Operators
Next Post: Java Switch Case : Switch fall-through, default, break, examples

More Related Articles

Java ArrayList Java ArrayList Java
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
Internal Working of HashMap in Java | Rehashing , Hash Collision Internal Working of HashMap in Java, Rehashing, Collision Java
Java 11 new Features (With example Programs) Java 11 new Features (With example Programs) Important
Java Vector Vector in Java Collection, Constructors, Example Programs Java
  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved