Java for Selenium Part 5

Java Loop Statements, String Handling

i) Java Loop Statements

ii) String Handling inwards Java
---------------------------------------------
Java Conditional Statements

6) Decide with 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 course of teaching ='X';

switch (grade){
case 'A':
System.out.println("Excellent");
break;

case 'B':
System.out.println("Good");
break;

case 'C':
System.out.println("Better");
break;

default:
System.out.println("Invalid Grade");   
}
----------------------------------------------
i) Java Loop Statements

Four Loop Structures inwards Java

1) for loop

2) spell loop

3) produce spell loop

4) Enhanced for loop
-----------------------------------
1) for loop

Description:

It repeats a block of statements for a specified publish of times.

Syntax:

for (dataType variableName = startValue; endValue; increment/decrement){
Statements
---------------
-----------
}

Example 1: Print 1 to 10 Numbers

for (int i=1; i <= 10; i++){
        System.out.println(i);
    }
---------------------------------------------------
Example 2: Print 10 to 1 Numbers

for (int i=10; i >= 1; i--){
System.out.println(i);
}

Example 3: Print 1 to 10 Numbers except quaternary Number

for (int i=1; i <= 10; i++){
if (i!=4){
System.out.println(i);
}
}
----------------------------------------------
Example 3: Print 1 to 10 Numbers except quaternary as well as seventh Numbers

for (int i=1; i <= 10; i++){
if ((i!=4) && (i!=7)){
System.out.println(i);
}
}
-----------------------------------------------
2) spell loop

Description

It repeats a block of statements spell status is true.

Syntax:

Initialization
while (condition){
Statements
--------------
--------------
increment/decrement
}

Example:

int i =1;
while (i<=10){
System.out.println(i);
i++;
}
-------------------------------------------
int i =10;
while (i>=1){
System.out.println(i);
i--;
}
----------------------------------------------
int i =1;
while (i<=10){
if (i != 6){
System.out.println(i);
}
i++;
}
----------------------------------------------
3) produce spell loop

Description:

It repeats a block of statements spell status is true.
It executes a block of statements at to the lowest degree i time irrespective of the condition.

Syntax:

Initialization;
do
{
Statements
-----------
----------
increment/decrement;
} spell (condition);

Example:

int i=1;
do
{
System.out.println(i);   
i++;
} spell (i<=10);
--------------------------------------
int i=20;
do
{
System.out.println(i);   
i++;
} spell (i<=10);
-------------------------------------------
4) Enhance for loop

Description:

It executes all elements inwards an Array

Syntax:

Array Declaration;

for (declaration: Array){
Statements
-------------
}

Example:

String [] languages = {"C", "COBOL", "Java", "VBScript"};

for (String lang: languages){
    System.out.println(lang);
}

Example 2:

public static void main(String[] args) {
int a=10, b=20;
   
int [] mathOperations = novel int [3];
mathOperations[0] = a+b;
mathOperations[1] = a-b;
mathOperations[2] = a*b;

for (int operation: mathOperations){
System.out.println(operation);
}
--------------------------------------------------------
we tin use,

a) Only Condition blocks

Example:

if (a > b){
System.out.println("A is a Big Number");
}
else
{   
System.out.println("B is a Big Number");
}
-----------------------------
Or

b) Only Loop Blocks

for (int i = 1; i<=5; i++){
System.out.println(i);
}


Or

c) Condition/s with inwards a Loop block

for (int i = 1; i<=5; i++){
if (i != 3){
System.out.println(i);
}
}

Or

d) Loop/s with inwards a Condition

in a Java program.
----------------------------------------------------
Assignment:

Loops with inwards a Condition
Write an Example: Insert a loop disceptation inwards If Condition
----------------------------------------------
ii) String Handling inwards Java

What is String?

String is a sequence of characters written inwards double quotes.

String may bring Alphabets, Numbers as well as Special Characters.

Example:

"India"
"123"
"India123"
"India*"
"India123*&"
"&*^%"
"Selenium Testing"
"Test Automation using Selenium as well as Java"
------------------------------------------------------
Data Types inwards Computer Programming

Numbers (Integer ,Float as well as Double information types)

Integer (ex: 100)

byte, short, int, long

Floating cry for / Decimal values (Ex: 1.234)

float, double

Characters (Ex: 'A' or 'a' or '1' or '*')

char information type

Boolean / Logical Values (Ex: true/false)

boolean  information type
------------------------------------------
Strings (Ex: "India", "123")

Using String Object
--------------------------------------------
123 -Integer
1.234 - Decimal type
A - character
true/false - Logical value
"India123*" - String
-----------------------
Creating Strings

String myTool = "LoadRunner"; // String Variable
String [] myTools = {"Selenium", "UFT", "RFT", "SilkTest"}; //Array of Strings

System.out.println(myTool);

for (String abc: myTools){
System.out.println(abc);
}
-----------------------------------------------------
Operations on Strings

1) Concatenating Strings

String str1 = "Selenium ";
String str2 ="UFT";

System.out.println(str1 + str2);//Selenium UFT
System.out.println(str1.concat(str2));//Selenium UFT
System.out.println("Selenium " + "UFT");//Selenium UFT
System.out.println("Selenium"+ 1 + 1);//Selenium11
System.out.println(1 + 1 + "Selenium");//2Selenium
--------------------------------------------------------
Ex:

String  + String - Concatenation

String + Integer - Concatenation

Integer + Integer - Addition
----------------------------------------
"Selenium"+ 1 + 1 //Selenium11

Selenium1 + 1 - Selenium11
-------------------------------------
1+1+"Selenium

2 + "Selenium" - 2Selenium
----------------------------------------------
2) String Comparison

We bring 2 types of Comaparison inwards Computer Programming

i) 2-way Comparison (True/False)

ii) 3-way Comparison (=, >, <)

Result Criteria:

if string1 = string2 as well as thus 0
if string1 > string2 as well as thus Positive value (Greater than 0)
if string1 < string2 as well as thus negative value (Less than 0)
------------------------------------------------------------
ANSI Character codes

//A to Z (65 to 90)
//a to z (97 to 122)
//0 to ix (48 to 57)
-------------------------------------------------------
a) String Comparison using Relational Operator (==)
Supports 2-way comparison

b) String Comparison using equals() method
Supports 2-way comparison

c) String Comparison using compareTo() Method
Supports 3-way comparison

Example:

public static void main(String[] args) {
String str1 = "SELENIUM";
String str2 = "selenium";
String str3="SELENIUM";
String str4 = "zselenium";

//String Comparison using Relational Operator (==)
System.out.println(str1 == str2);//false
System.out.println(str1 == str3);//true

//String Comparison using equals() method
System.out.println(str1.equals(str2));//false
System.out.println(str1.equals(str3));//true

//String Comparison using compareTo() Method
System.out.println(str1.compareTo(str2)); //Negative value/Less than 0
System.out.println(str1.compareTo(str3));//0
System.out.println(str4.compareTo(str2));//Positive value / Greater than 0
}
--------------------------------------------

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