Java for Selenium Part 4


Java Operators as well as Conditional Statements

i) Java Operators

ii) Java Conditional Statements
-------------------------------------------------
i) Java Operators

Operators are used to perform Mathematical, Comparison as well as Logical Operations.

Important Categories of Operators

1) Arithmetic Operators

2) Relational Operators

3) Assignment Operators

4) Logical Operators
-----------------------------
1) Arithmetic Operators
Mathematical Operations

Addition

Subtraction

Multiplication

Division

Integer Division

Modules

Exponentiation
------------------------------
Java Arithmetic Operators

a) Addition + (Addition, String Concatenation)

b) Subtraction - (Subtraction, Negation)

c) Multiplication *

d) Division /

e) Modules %

f) Increment ++

g) Decrement --
----------------------------------------------
Example:

public static void main(String[] args) {
int a =10, b=3, e=-100;
String c ="Selenium", d = " Testing";

System.out.println("Addition of a, b is "+ (a+b));
System.out.println(c+d);//Selenium Testing

System.out.println(a-b);//7
System.out.println(e);

System.out.println(a*b);//30

System.out.println(a/b);//3
System.out.println(10.0/3);//3.333333

System.out.println(a % b);//1

b=10;
a = ++b;
System.out.println(a);//11

b=10;
a = --b;
System.out.println(a);//9

double x = Math.pow(10, 4); //10000
System.out.println(x);
//-------------------------------------
float y =1.2F, p=2.34f;
double z =1.345678, q=234.456787;

System.out.println(y+p);//3.54
System.out.println(z+q);
}
}
------------------------------
2) Relational Operators

= (Assignment)
== (Comparison/Relation)
-------------------------------
a) ==

b) !=

c) >

d) >=

e) <

f) <=
----------------------------------------------
Note: Relational Operators furnish Boolean / Logical Result (true/false)

Example:

public static void main(String[] args) {
int a, b;
a=10; b=20;

System.out.println(a == b);//false
System.out.println(a != b);//true
System.out.println(a > b);//false
System.out.println(a >= b); //false
System.out.println(a < b); //true
System.out.println(a <= b); //true
}
-------------------------------
3) Assignment Operators

a) Assignment =

b) Add as well as Assign +=

c) Subtract as well as Assign -=

d) Multiply as well as Assign *=
--------------------------------------------
Example:

public static void main(String[] args) {
int a =10;
System.out.println(a);//10

a += 10;
System.out.println(a);//20

a -= 10;
System.out.println(a);//10

a *= 10;
System.out.println(a);//100
}
------------------------
4) Logical Operators

a) Logical Not operator !

b) Logical And operator &&

c) Logical Or operator ||
---------------------------------------------------------
Result Criteria for Logical Not Operator

Operand 1    Operand 2    Result
---------------------------------------------------
true        true        false
true        false        true
false        true        true
false        false        true
--------------------------------------------------
Result Criteria for Logical And Operator

Operand 1    Operand 2    Result
---------------------------------------------------
true        true        true
true        false        false
false        true        false
false        false        false
---------------------------------------------------
Result Criteria for Logical Or Operator

Operand 1    Operand 2    Result
---------------------------------------------------
true        true        true
true        false        true
false        true        true
false        false        false
----------------------------------------------------
Example:

public static void main(String[] args) {
boolean a = true, b = false;
System.out.println(!(a && b));//true
System.out.println(a && b);//false
System.out.println(a || b);//true
}
-------------------------------------------------------
Example 2:

public static void main(String[] args) {
int a =10000, b =5000, c =700;
if ((a > b) && (a > c)){
System.out.println("A is a Big Number");   
}
else {
System.out.println("A is Not a Big Number");   
}
}
----------------------------------------------------
public static void main(String[] args) {
int a =10000, b =50000, c =70000;
if ((a > b) || (a > c)){
System.out.println("A is a Big Number");   
}
else {
System.out.println("A is Not a Big Number");   
}
}
----------------------------------
public static void main(String[] args) {
int a =100, b =50, c =70;
if (!(a > b) && (a > c)){
System.out.println("A is a Big Number");   
}
else {
System.out.println("A is Not a Big Number");   
}
}
------------------------------------------------------------------------------
Java Control Flow
    i) Conditional Statements
    ii) Loop Statements
-------------------------------------------------
ii) Java Conditional Statements

a) Usage of Conditional Statements inwards Test Automation

1) To insert Verification Points

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

1) if statement

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

1) Single Condition (Positive as well as Negative)

ex:

Positive Condition

if (a > b) {
.
.
}

Negative Condition

if (!(a < b)) {
.
.
}
--------------------------------------
2) Compound Condition

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

Or

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

Or

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 status is True.


Syntax

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

Example:

Positive Condition

public static void main(String[] args) {
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 =10, b =50;

if (!(a < b)){
System.out.println("A is a Big Number");
}
}
---------------------------------------------------------------
2) Execute a block of statements when a chemical compound status is True.
Syntax:

if ((condition 1) && Or || (condition 2)){
Statements
------------------
----------
}

Example:

public static void main(String[] args) {
int a =100, b =50, c = 400;

if ((a > b) && (a > c)){
System.out.println("A is a Big Number");
}
}
------------------------------------------------------
3) Execute a block of statements when status is truthful otherwise execute roughly other block of statements.

Syntax:

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

Example:

public static void main(String[] args) {
int a =100, b =50;

if (a > b) {
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");   
}
}
----------------------------------------
4) Decide with 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 as well as verify the range

if the Number is inwards betwixt 1 as well as 100 as well as then display "Number is a Small Number"
if the Number is inwards betwixt 101 as well as M as well as then display "Number is a Medium Number"
if the Number is inwards betwixt 1001 as well as 10000 as well as then display "Number is a Big Number"
if the Number is to a greater extent than than 10000 as well as then display "Number is High Number"
otherwise display "Number is either Zero or Negative Number"

Java Program:

-----------------------------------------
Input Data

1st Iteration 50

2nd Iteration 400

3rd Iteration 4000

4th Iteration 11000

5th Iteration 0

6th Iteration -100
-------------------
public static void main(String[] args) {
int a = -100;

if (( a > 0) && ( a <= 100)){
System.out.println("A is a Small Number");
}
else if ((a > 100) && ( a <=1000)){
System.out.println("A is a Medium Number");   
}
else if ((a > 1000) && ( a <=10000)){
System.out.println("A is a Big Number");   
}   
else if (a > 10000){
System.out.println("A is High Number");       
}
else{
System.out.println("A is Either Zero or Negative value");   
}
}
------------------------------------------------------------------
5) Execute a block of statements when to a greater extent than than i status is True (Nested if).

Syntax:

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

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 =50, c=70, d =900;

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");   
}
--------------------------------------------------
Advantage of Nested Condition over Compound Condition

In Nested Condition nosotros tin write multiple else parts

In Compound status unmarried else purpose only.
-----------------------------------------
Problem: Get Biggest Number out of Four Numbers

Hint: usage Compound weather condition as well as else if structures

Solution:

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

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");       
}
---------------------------------------------------


Java for Selenium purpose v Link

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post