Java Loop Statements, String Handling

Java Loop Statements, String Handling

i) Java Loop Statements

ii) String Handling in Java
—————————
Java Conditional Statements

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(“Good”);
break;

case ‘C’:
System.out.println(“Better”);
break;

default:
System.out.println(“Invalid Grade”);
}

Java Loop Statements and Java Strings

i) Java Loop Statements

Four Loop Structures in Java

1) for loop

2) while loop

3) do while loop

4) Enhanced for loop
———————————–
1) for loop

Description:

It repeats a block of statements for a specified number 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 4th Number

for (int i=1; i <= 10; i++){
if (i!=4){
System.out.println(i);
}
}
—————————–
Example 4: Print 1 to 10 Numbers except 4th and 7th Numbers

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

Description

It repeats a block of statements while condition 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) do while loop

Description:

It repeats a block of statements while condition is true.
It executes a block of statements at least once irrespective of the condition.

Syntax:

Initialization;
do
{
Statements
———–
———-
increment/decrement;
} while (condition);

Example:

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

Description:

It executes all elements in 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 = new int [3];
mathOperations[0] = a+b;
mathOperations[1] = a-b;
mathOperations[2] = a*b;

for (int operation: mathOperations){
System.out.println(operation);
}
—————————-
we can 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 in a Loop block

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

Or

d) Loop/s with in a Condition

in a Java program.
——————————
Assignment:

Loops with in a Condition
Write an Example: Insert a loop statement in If Condition

ii) String Handling in Java

What is String?

String is a sequence of characters written in double quotes.

String may have Alphabets, Numbers and Special Characters.

Example:

“India”
“123”
“India123”
“India*”
“India123*&”
“&*^%”
“Selenium Testing”
“Test Automation using Selenium and Java”
———————————–
Data Types in Computer Programming

Numbers (Integer ,Float and Double data types)

Integer (ex: 100)

byte, short, int, long

Floating point / Decimal values (Ex: 1.234)

float, double

Characters (Ex: ‘A’ or ‘a’ or ‘1’ or ‘*’)

char data type

Boolean / Logical Values (Ex: true/false)

boolean data 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 have two types of Comparison in Computer Programming

i) 2-way Comparison (True/False)

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

Result Criteria:

if string1 = string2 then 0
if string1 > string2 then Positive value (Greater than 0)
if string1 < string2 then negative value (Less than 0)
———————
ANSI Character codes

//A to Z (65 to 90)
//a to z (97 to 122)
//0 to 9 (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
}
——————————

(175)

You may also like...

Leave a Reply