If Else Statement -Selenium WebDriver
if, if else and nested if else statements are useful to take the decision based on conditional match.When you wants to execute some part of code if condition returns true then you need to use this kind of conditional statements.
Simple If Statement
Part of code will be executed only if specified condition returns true. If condition will return false then that code will be not executed.
Example :
if (i<j)
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
In above given example, message will be printed only and only if value of variable i is less than value of variable j.
Example :
if (i<j)
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
In above given example, message will be printed only and only if value of variable i is less than value of variable j.
If else Statement
If condition returns true then part of if block will be executed. If condition returns false then part of else block will be executed.
Example :
if (i>=j)
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of j("+j+")." );
}else
{
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
}
In above given example, if block's message will be printed if value of variable i is greater than or equals to value of variable j. else block will be executed if value of variable i is less than value of variable j.
Example :
if (i>=j)
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of j("+j+")." );
}else
{
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
}
In above given example, if block's message will be printed if value of variable i is greater than or equals to value of variable j. else block will be executed if value of variable i is less than value of variable j.
Nested If Else Statements
You can use nested if else statement when you wants to check multiple conditions and take decision based on it.
Example :
if (k<i)
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)
{
System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And Value Of Value Of j("+j+")" );
}else
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}
In above given example, first (if) block will be executed if value of variable k is less than the value of variable i.
Second (else if) block will be executed if value of variable k is greater than or equals to value of variable i and less than or equals to value of variable j.
Third (else) block will be executed if value of variable k is greater than value of value of variable j. You can make a chain of if else statement if you wants to check more conditions.
This way, You can use any of above conditional statement based on your requirement.
Example :
if (k<i)
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)
{
System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And Value Of Value Of j("+j+")" );
}else
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}
In above given example, first (if) block will be executed if value of variable k is less than the value of variable i.
Second (else if) block will be executed if value of variable k is greater than or equals to value of variable i and less than or equals to value of variable j.
Third (else) block will be executed if value of variable k is greater than value of value of variable j. You can make a chain of if else statement if you wants to check more conditions.
This way, You can use any of above conditional statement based on your requirement.
Run bellow given example in your eclipse by changing the values of variables.
public class IfStatements {
public static void main(String[] args) {
int i = 25;
int j = 50;
int k = 24;
//Simple If statement
System.out.println("***Simple If Statement Example***");
if (i<j) //Bellow given message will be printed only if value of variable i is less than value of variable j.
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
//If Else Statement
System.out.println("");
System.out.println("***If Else Statement Example***");
if (i>=j)//Bellow given message will be printed if value of variable i is greater than or equals to value of variable j.
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of j("+j+")." );
}else//Bellow given message will be printed if value of variable i is less than value of variable j.
{
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
}
//Nested If Else Statement
System.out.println("");
System.out.println("***Nested If Else Statement Part***");
if (k<i)//Bellow given message will be printed if value of variable k is less than value of variable i.
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)//Bellow given message will be printed if value of variable k is greater than or equals to value of variable i and less than or equals to value of variable j.
{
System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And Value Of Value Of j("+j+")" );
}else //Bellow given message will be printed if value of variable k is greater than value of variable j.
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}
}
}
Bellow given output will be printed in your eclipse console when you will run above example.
***Simple If Statement Example***
Value Of i(25) Is Smaller Than Value Of j(50).
***If Else Statement Example***
Value Of i(25) Is Smaller Than Value Of j(50).
***Nested If Else Statement Part***
Value Of k(24) Is Less Than Value Of i(25).
public class IfStatements {
public static void main(String[] args) {
int i = 25;
int j = 50;
int k = 24;
//Simple If statement
System.out.println("***Simple If Statement Example***");
if (i<j) //Bellow given message will be printed only if value of variable i is less than value of variable j.
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
//If Else Statement
System.out.println("");
System.out.println("***If Else Statement Example***");
if (i>=j)//Bellow given message will be printed if value of variable i is greater than or equals to value of variable j.
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of j("+j+")." );
}else//Bellow given message will be printed if value of variable i is less than value of variable j.
{
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
}
//Nested If Else Statement
System.out.println("");
System.out.println("***Nested If Else Statement Part***");
if (k<i)//Bellow given message will be printed if value of variable k is less than value of variable i.
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)//Bellow given message will be printed if value of variable k is greater than or equals to value of variable i and less than or equals to value of variable j.
{
System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And Value Of Value Of j("+j+")" );
}else //Bellow given message will be printed if value of variable k is greater than value of variable j.
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}
}
}
Bellow given output will be printed in your eclipse console when you will run above example.
***Simple If Statement Example***
Value Of i(25) Is Smaller Than Value Of j(50).
***If Else Statement Example***
Value Of i(25) Is Smaller Than Value Of j(50).
***Nested If Else Statement Part***
Value Of k(24) Is Less Than Value Of i(25).
No comments:
Post a Comment