Java Language / Java Conditional Statements

It tells us to execute which block / Lines of code has to be executed in the program based on the conditions.
Ther are 4 conditional statements in java.They were

1. if Statement.
2. if else Statement.
3. else if Statement.
4. Ternary Operator.
5. Switch Statement.

if Statement
Block / Lines of code to be executed if the condition is true in if statement.

Syntax
if (condition)
{
// block / Lines of code will be executed if the condition is true
}

Sample Program Output
public class Biggerclass
{
public static void main(String[] args)
{
if (5 > 2)
{
System.out.println("5 is bigger than 2");
}
}
}
5 is bigger than 2


else Statement
Block1 / Lines of code will be executed if the condition is true in if statement.
Block2 / Lines of code will be executed if the condition is true in else statement.

Syntax
if (condition)
{
// block1 / Lines of code to be executed if the condition is true
}
else
{
// block2 / Lines of code to be executed if the condition is false
}

Sample Program Output
public class Biggerclass
{
public static void main(String[] args)
{
if (2 > 5)
{
System.out.println("2 is bigger than 5");
}
else
{
System.out.println("5 is bigger than 2");
}
}
}
5 is bigger than 2


else if Statement
Block1 / Lines of code will be executed if the condition1 is true in if statement.
Block2 / Lines of code will be executed if the condition1 is false and condition2 is true.
Block3 / Lines of code will be executed if the condition1 and condition2 is false.

Syntax
if (condition1) { // block1 code will be executed if condition1 is true } else if (condition2) { // block2 code will be executed if the condition1 is false and condition2 is true } else { // block2 code will be executed if the condition1 and condition2 are false }

Sample Program Output
public class Biggerclass
{
public static void main(String[] args)
{
if (2 > 5)
{
System.out.println("2 is bigger than 5");
}
else if (5 > 2)
{
System.out.println("5 is bigger than 2");
}
else
{
System.out.println("Both are equal");
}
}
}
5 is bigger than 2


Ternary Operator
In the expression variable = Expression1 ? Block1 code : Block2 code if Expression1 is true it will Block1 code will be execute other wise Block2 code will be execute.

Sample Program Output
public class Biggerclass
{
public static void main(String[] args)
{
n1 = 5;
n2 = 2;
big=(n1>n2) ? n1; n2;
System.out.println("Bigger:" + big);
}
}
Bigger: 5


Java Switch Statements
Here one of the block is executed based on Condition /switch Value which match with the case value other wise it will execute the default block.

Syntax
switch(Condition /switch Value)
{
case 1: //block1 break;
case 2: // block2 break;
case 3: // block3 break;
default: // block4
}

Sample Program Output
public class Biggerclass
{
public static void main(String[] args)
{
switch (2)
{
case 1: System.out.println("5>2"); break;
case 2: System.out.println("2>5"); break;
default: System.out.println("Both are equal"); break;
}
}
}
2 > 5


Home     Back