If Else And Nested If Else In Java

CONTROL STRUCTURE :

In java program, control structure is can divide in three parts :

  • Selection statement
  • Iteration statement
  • Jumps in statement

Selection Statement :

Selection statement is also called as Decision making statements because it provides the decision making capabilities to the statements.

In selection statement, there are two types:

  • if statement
  • switch statement

These two statements are allows you to control the flow of a program with their conditions.

if Statement :

The “if statement” is also called as conditional branch statement. It is used to program execution through two paths. The syntax of “if statement” is as follows :

Syntax
if (condition)
{
Statement 1;
Statement 2;
...
}
else
{
Statement 3;
Statement 4;
...
}

The “if statement” is a commanding decision making statement and is used to manage the flow of execution of statements. The “if statement” is the simplest one in decision statements. Above syntax is shows two ways decision statement and is used in combination with statements.

flow chart of if else statement in java

Simple if statement :

Syntax
//Syntax:
If (condition)
{
Statement block;
}
Statement-a;

In statement block, there may be single statement or multiple statements. If the condition is true then statement block will be executed. If the condition is false then statement block will omit and statement-a will be executed.

if…else statement :

Syntax
//Syntax:
If (condition)
{
True - Statement block;
}
else
{
False - Statement block;
}
Statement-a;

If the condition is true then True - statement block will be executed. If the condition is false then False - statement block will be executed. In both cases the statement-a will always executed. Following figure shows the flow of statement.

flow chart of if else in java

Following program shows the use of if statement.

Program : write a program to check whether the number is positive or negative.

CODE/PROGRAM/EXAMPLE
import java.io.*;
class NumTest
{
public static void main (String[] args) throws IOException
{
int Result=11;
System.out.println("Number is"+Result);
if ( Result < 0 )
{
System.out.println("The number "+ Result +" is negative");
}
else
{
System.out.println("The number "+ Result +" is positive");
}
System.out.println("------- * ---------");
}
}

// Output:
C:\MCA>java NumTest
Number is 11
The number 11 is positive

(All conditional statements in Java require boolean values, and that's what the ==, <, >, <=, and >= operators all return. A boolean is a value that is either true or false. If you need to set a boolean variable in a Java program, you have to use the constants true and false. Boolean values are no more integers than are strings).

For example : write a program to check whether the number is divisible by 2 or not.

CODE/PROGRAM/EXAMPLE
import java.io.*;
class divisorDemo
{
public static void main(String[ ] args)
{
int a =11;
if(a%2==0)
{
System.out.println(a +" is divisible by 2");
}
else
{
System.out.println(a+" is not divisible by 2");
}
}
}

// Output:
C:\MCA>java divisorDemo
11 is not divisible by 2

Nesting of if-else statement :

Syntax
Syntax:
if (condition1)
{
If(condition2)
{
Statement block1;
}
else
{
Statement block2;
}
}
else
{
Statement block3;
}

Statement 4 :

If the condition1 is true then it will be goes for condition2. If the condition2 is true then statement block1 will be executed otherwise statement2 will be executed. If the condition1 is false then statement block3 will be executed. In both cases the statement4 will always executed.

Nesting of if-else statement in java

For example : Write a program to find out greatest number from three numbers.

CODE/PROGRAM/EXAMPLE
class greatest
{
public static void main (String args[ ])
{
int a=10;
int b=20;
int c=3;
if(a>b)
{
if(a>c)
{
System.out.println("a is greater number");
}
else
{
System.out.println("c is greater number");
}
}
else
{
if(c>b)
{
System.out.println("c is greater number");
}
else
{
System.out.println("b is greater number");
}
}
}
}

// Output:
C:\MCA>java greatest
b is greater number
#control_structure_in_java #Selection_Statement_in_java #if_Statement_in_java,if_else_in_java #nested_if_else_in_java

(New page will open, for Comment)

Not yet commented...