Java Conditional Statements


Java Conditional Statements
 

Flow Control
    1) Conditional Statements
    2) Loop Statements
--------------------------------------
Conditional Statements

a) Usage of Conditional Statements in Test Automation

1) To insert Verification points

2) Error Handling
---------------------------------------
b) Two types of Conditional Statements
 

1) if Statement

2) switch Statement
---------------------------------------
c) Types of Conditions
 

1) Single Condition (Positive, Negative)
Ex:
Positive Condition
if (a > b) {
.
}
----------------------
Negative Conditionif (!(a < b)) {
.
}

2) Compound Conditionex:

if ((a > b) && (a > c)) {
.
}

Or

if ((a>b) || (a>c)){
.
}
----------------
if ((a>b) && (a>c) && (a>d)){
.
}
---------------------------------
3) Nested Condition
if (a>b) {
 if (a>c) {
  if (a>d) {
  }
 }
}
---------------------------------------
d) Usage of Conditional Statements

1) Execute a block of Statements when condition is True.
 

Syntax:

if (condition) {
Statements
----------
----------
----------
}

Example:
Positive Condition
int a =100, b =50;
if (a > b){
System.out.println("A is a Big Number");
}
-----------------------
Negative Condition
public static void main(String[] args) {
int a =100, b =50;
if (! (a < b)){
System.out.println("A is a Big Number");
}
}
---------------------------------------------------
2) Execute a block of statements when a Compound condition is True.
 

Syntax:

if ((condition) && Or || (condition)) {
Statements
----------
---------
}
---------------------------
Example:

public static void main(String[] args) {
int a =100, b =50, c =300;
if ((a > b) || (a > c)){
System.out.println("A is a Big Number");
}
}
------------------------------------------------------
3) Execute a block of statements when condition is true otherwise execute another block of statements.
 

Syntax:

if (Condition) {
Statements
----------
-----------
}
else
{
Statements
----------
-----------
}
----------------------
Example:

public static void main(String[] args) {
int a =100, b =100;
if (a > b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}
}
-----------------------------------------------------------
4) Decide among several alternates (else if)
 

Syntax:
if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else if (condition) {
Statements
----------
---------
}
else
{
Statements
----------
---------
}
----------------------------------
Example:
Initialize an Integer variable and verify the range.

if the Number is in between 1 and 100 then display "Number is a Small Number"

if the Number is in between 101 and 1000 then display "Number is a Medium Number"

if the Number is in between 1001 and 10000 then display "Number is a Big Number"

if the Number is more than 10000 then display "Number is High Number"

Otherwise display "Number is either Zero or Negative Number"
-------------------------------------------------
public static void main(String[] args) {
int val = -100;

if ((val >= 1) && (val <= 100)){
System.out.println("Value is a Small Number");
}
else if ((val > 100) && (val <= 1000)) {
System.out.println("Value is a Medium Number");
}
else if ((val > 1000) && (val <= 10000)) {
System.out.println("Value is a Big Number");
}
else if (val > 10000) {
System.out.println("Value is High Number");
}
else {
System.out.println("Value is either Zero or Negative Value");
}
}
------------------------------------------------------------------
5) Execute a block of statements when more than one condition is True (Nested if)
 

Syntax:
if (condition){
 if (condition) {
  if (condition){
Statements
-----------
-----------
----------
}
}
}
---------------------------------
Example:
Check if the value of "a" variable is bigger than , c, d variable values or not?

public static void main(String[] args) {
int a =100, b =90, c=80, d=70;

if (a>b){
 if (a>c){
  if (a>d){
  System.out.println("A is a Big Number"); 
  }
  else{
 System.out.println("A is Not a Big Number");  
  }
 }
 else{
 System.out.println("A is Not a Big Number");     
 }
}
else {
System.out.println("A is Not a Big Number");     
}

}
-----------------------------------------------------------
Using Compound Condition
 

public static void main(String[] args) {
int a =100, b =90, c=80, d=700;

if ((a>b) && (a>c) && (a>d)){
 System.out.println("A is a Big Number"); 
 }
else{
 System.out.println("A is Not a Big Number");  
  }

}
-----------------------------------------------------
Advantages of Nested condition over Compound condition
 

In Nested Condition we can write multiple else parts

In Compound condition single else part only.
-------------------------------------------------------
Problem: Get Biggest Number out of Four numbers.

Solution:

public static void main(String[] args) {
int a =100, b =90, c=80, d=700;

if ((a>b) && (a>c) && (a>d)){
System.out.println("A is a Big Number");
}
else if((b>a) && (b>c) && (b>d)){
System.out.println("B is a Big Number");
}
else if((c>a) && (c>b) && (c>d)){
System.out.println("C is a Big Number");
}
else{
System.out.println("D is a Big Number");
}
}
-------------------------------------------------------------
6) Decide among several alternates (using switch statement)
 

Syntax:
switch (expression){
case value:
Statements
-----------
------------
--------
break;
case value:
Statements
-----------
------------
--------
break;
case value:
Statements
-----------
------------
--------
break;

default:
Statements
------------
------------
---------
}

Example:


public static void main(String[] args) {
char grade ='X';

switch (grade){
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Well Done");
break;
case 'C':
System.out.println("Better");
break;

default:
System.out.println("Invalid Grade");
}
   
}
---------------------------------------------------------

No comments:

Post a Comment