Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java If-else Statement

The condition is tested using the if statement in Java. It verifies the condition is true or false.

The four different kinds of if-statements are listed below.

  • Normal if statement
  • if-else statement
  • if-else if statement
  • Nested if-statement


1.Normal if statement


Syntax

 if (condition) {  
     statement 1;  // your code  execute when condition is true 
  } 

Flow Chart

 // Decision-Making program using if 
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  7; 
int y =  4; 
if (x-y>2)   
System.out.println( "x-y is greater than 2"); 
 }
}


Output:

x-y is greater than 2 

Example 2

 // Decision-Making program using if 
public class Student { 
	
	public static void main(String[] args) {  
	    
	    int Score=25;   //defining an 'Score' variable  
	   
	    if(Score>20)  //checking the Score  
	    {  
	        System.out.print("Your Score is greater than 20");  
	    }    
      }  
}  

Output:

x-y is greater than 2 

2. if-else statement


Syntax

 if (condition) {  
     statement 1;  // your code  execute when condition is true 
  } 
 else {  
     statement 2;  // your code  execute when condition is false 
  } 


 // Decision-Making program using if...else  
 import java.io.*;
 public class Control{  
public static void main(String args[ ]){
int x =  8; 
int y =  10; 
if (x-y>5)   
System.out.println( "x-y is greater than 5"); 
else  
System.out.println("X is less  than 5"); 
 }
}

Output:

X is less  than 5 

Tip

if one statement is inside if and else no need to open curley barcket if there is multiple statements then you must use curley bracket to execute that block of code inside it.

Let's see one example

 // Decision-Making program using if...else  
public class Student { 
	public static void main(String[] args) {  
	    
	    int Score=85;   //defining an 'Score' variable  
	   
	    if(Score>85)  //checking the Score  
	    {  
	        System.out.print("Your Score is greater than 60 ");  
	    }
	    else 
	    {
		System.out.println("You Score less than 60 ");
     	     }
      }  
} 

Output:

Your Score is greater than 60 


condition checked Using Ternary Operator


Syntax

A quick method to check the condition is to use a ternary operator. The answer to ? is returned if the condition is true. However, the outcome of : is returned if the condition is untrue.

 // Decision-Making program  Using Ternary Operator  
public class Student { 
	public static void main(String[] args) {  	    
        int number=2020;     //Using ternary operator  
        String output=(((number % 4 ==0) && (number % 100 !=0)) || (number % 400==0))?"leap year":"commom year";    
        System.out.println(output); 
       }  
} 

Output:

leap year 

3. if else-if statement


we may have multiple condition inside our program to check multiple condition of different level java use else if with if statement. else if gives one more option to check condition in case first if block is not met a specific condition

Syntax

 if (condition1) {  
     statement 1;  // your code  execute when condition 1 is true 
  } 
 else if (condition2) {  
     statement 2;  // your code  execute when condition 2 is true 
  } 
 else {  
     statement 3;  // your code  execute when condition is false 
  } 


 // Decision-Making program using if - else if  
public class Student { 
	public static void main(String[] args) {  	    
	   int number = 0;
 
	    if (number < 0)  // checks if number is less than 0
	    {
	      System.out.println("The number is positive.");
	    }
	   
	    else if (number > 0)   //  checks if number is greater than 0
	    {
	      System.out.println("The number is negative.");
	    }
	      
	    else  // if both condition is false else will execute
	    {
	      System.out.println("The number is 0.");
	    }
      }  
}  

Output:

The number is 0. 

4. Nested if-statement


Nested if-statements are when one if or if-else statement can be found inside another if or else-if statement.

Syntax

 if (condition1) {  
 if (condition1)   {   
     statement 1;  // your code  execute when condition 2 is true 
  } 
 else {  
     statement 2;  // your code  execute when condition is false 
  } 
  } 


 // Decision-Making program using nested if - else if  
import java.util.Scanner;
public class Student { 
	public static void main(String[] args) {  	      
        Scanner sc = new Scanner(System.in);   
        System.out.print("Enter your age : "); 
        int age = sc.nextInt();
        System.out.print("Enter your gender : ");      
        String gender = sc.next();               
        if(gender.equals("male"))   // Check the gender is equal to 'male' or not
        {            
            if(age > 18)
            {
                System.out.print("You are in Men Group");
            }
            else
            {
                System.out.print("You are in Child Group");
            }
        }
        else
        {
            System.out.print("You are in Female Group ");
        }              
        
        sc.close(); //Close the scanner object
    }  
} 

Input:

Enter your age : 18 
Enter your gender : Female 

Output:

You are in Female Group  
java switch Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.