Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Nested if else statement


Nested If-Else Statement

Nesting means writing one statement into another statement.Nested if else allows us to write one if else inside another if else.

Nesting is required when we want to check another condition inside one condition.

Syntax

if ( condition1 )
{
	condition1 statement;
	if( sub-condition )
	{
		sub-condition statement;
	}
	else
	{
		sub-condition else statement;
	}
}
else
{
	condition1 else statement;
}

Flowchart

Nested If-Else Statement Flowchart

How nested if-else works?

First condition1 is checked, if it is true then control enteres inside block and again check for the sub-conditions same as normal if else

If condition1 becomes false then control jumps to the else block and execute it.

Example

Finding the largest number from given three numbers using nested if else

public class FindThreeGreatestEx 
{
    public static void main(String[] args) 
    {
	int first = 30, second = 20, third = 50;
		
	if(first>second)
	{
	    if(first>third)
		System.out.println("Greater is "+first);
	    else
		System.out.println("Greater is "+third);
	}
	else 
	{
	    if(second>third)
		System.out.println("Greater is "+second);
	    else
		System.out.println("Greater is "+third);
	}
    }
}
Output
Greater is 50
Best Examples for Practice
  • Check Number is Even or Odd program
  • Check number is Positive or Negative program
  • Find largest number from two/three numbers

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo