Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 1 January 2017

java if else

 

Java if-else Statements

java if else

java if statement is used to test the condition. java if statement checks boolean condition true or false. There are various types of if statements in java.
  • if Statement
  • if-else Statement
  • if-else-if Statement
  • Nested if Statement


Java if Statement

The java if statement test the boolean condition. It executes the if block if condition is true.

Syntax:

if(boolean condition)
{
//Statements will execute if boolean condition is true.
}

For example:

class ifStatement
{
public static void main(String[] s)
{
int  age = 20;
if(age > 18)
{
System.out.println("your age is greater than 18");
}
}
}

output: your age is greater than 18



Java if-else Statement


The java if-else statement is also test the boolean condition. It executes the if block if condition is true , if condition is false else block is executed.

Syntax:


if(boolean condition)

{
//Statement will executes if the boolean condition is true.
}
else
{
//Statement will executes if the boolean condition is false.
}

For example:


class ifelseStatement
{
public static void main(String[] args)
{
int age = 20;
if(age < 18)
{
System.out.println("your age is greater than 18");
}
else
{
System.out.println("your age is less than 18");
}
}
}

output: your age is less than 18



Java if-else-if Statement


The if-else-if statement execute one condition from multiple statements.

Syntax:


if(boolean condition1)

{
//Statement will executes if the boolean condition1 is true.
}
else if(boolean condition2)
{
//Statement will executes if the boolean condition2 is true.
}
else if(boolean condition3)
{
//Statement will executes if the boolean condition3 is true.
}
else if(boolean condition4)
{
//Statement will executes if the boolean condition4 is true.
}
else 
{
//Statement will executes if all the boolean condition is false.
}

For example:


class IfelseifStatement

{
public static void main(String...s)
{
int a = 30;

if(a == 10)

{
System.out.println("value of a is 10");
}
else if(a == 20)
{
System.out.println("value of a is 20");
}
else if(a == 30)
{
System.out.println("value of a is 30");
}
else
{
System.out.println("value are not matched");
}
}
}

For example 2:

In this example we will find given alphabet is vowel or constant.

class IfelseifStatement1
{
public static void main(String...s)
{
char ch = 'o';

if(ch == 'a' || ch =='A')
System.out.println(ch +"is vowel");
else if(ch == 'e' || ch =='E')
System.out.println(ch +"is vowel");
else if(ch == 'i' || ch =='I')
System.out.println(ch +"is vowel");
else if(ch == 'o' || ch =='O')
System.out.println(ch +"is vowel");
else if(ch == 'u' || ch =='u')
System.out.println(ch +"is vowel");
else
System.out.println(ch +"is constent");
}
}

output: o is vowel


Nested if Statement

you can use one if or else if statements inside another if or else if statement. Nesting of if statements is very useful when you have something to do more than one decision.

Syntax:

if(boolean condition1)
{
//Statement will execute if boolean condition 1 is true.
if(boolean condition2)
{
//Statement will execute if boolean condition 2 is true.
}
}

For example:

class NestedIf
{
public static void main(String args[])
{
int x = 10;
int y = 20;
if(x == 10)
{
if(y ==20)
{
System.out.println("value of y is "+y);
}
}
}
}

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate