Selenium-Class-Notes-3
Selenium-Class-Notes-3
com)
1) if statement
2) switch statement
Negative condition
if (b<a){
.
.
}
if (!(b>a)){
.
.
}
Positive Condition:
Syntax:
if (condition){
Statements
……………
……………
……………
}
Example:
int a=100, b=500;
if (a>b){
System.out.println("A is a Big Number");
}
if (!(b>a)){
System.out.println("A is a Big Number");
}
Syntax:
if (condition) {
Statements
……………
……………
…………..
}
else
{
Statements
……………
……………
…………..
}
Example:
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");
}
Syntax:
if ((condition1) && or ||(condition2) && or || (condition3)){
statements
……………
……………
…………..
}
Example:
int x=100, y=90, z=700;
if ((x>y) || (x>z)){
System.out.println("A is a Big Number");
}
Syntax:
if ((condition1) && or ||(condition2) && or || (condition3)){
statements
……………
……………
…………..
}
else {
……………
……………
…………..
}
Example:
int x=100, y=900, z=70;
Syntax:
if (condition){
Statements
……………
……………
else if (condition){
Statements
……………
……………
else if (condition){
Statements
……………
……………
else if (condition){
Statements
……………
……………
else {
Statements
……………
…………..
}
Solution:
Input:
1) 50
2) 150
3) 1500
4) 15000
5) 0
6) -100
else {
System.out.println("val is either Zero or Negative Number");
}
Syntax:
if (condition1){
if (condition2){
if (condition3){
Statements
……………
……………
}
}
}
Example:
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");
}
}
}
Syntax:
if (condition1){
if (condition2){
if (condition3){
Statements
……………
……………
}
else {
Statements
……………
……………
}
}
else {
Statements
……………
……………
}
}
else {
Statements
……………
……………
}
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 - 3rd Condition is False");
}
}
else {
System.out.println("A is Not a Big Number - 2nd Condition is False");
}
}
else {
System.out.println("A is Not a Big Number - 1st Condition is False");
}
Problem: Find the biggest value (Integer values) among four values
Java Program:
int a=100, b=100, c=100, d=100;
}
}
}
Syntax:
switch (expression){
case1 value:
statements
……………
……………
break;
case2 value:
statements
……………
……………
break;
case3 value:
statements
……………
……………
break;
case4 value:
statements
……………
……………
break;
default:
statements
……………
……………
}
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");
}
Note:
You can use Condition/s only in your program,
or Loop/s only in your Program,
You can insert Loop/s in Condition/s and vice versa.
1) for loop
Syntax:
for (StratValue; EndValue; Increment / Decrement){
Statements
…………
……………
}
Examples:
Print 1 to 10 Numbers
for (int i=1; i<=10; i++){
System.out.println(i);
}
2) while loop
Syntax:
Initialization
while (condition){
Statements
…………
……………
Increment/Decrement
}
Example/s:
Print 1 to 10 Numbers
int i=1;
while (i<=10){
System.out.println(i);
i++;
}
System.out.println(i);
}
i++;
}
int i=10;
while (i>=1){
System.out.println(i);
i--;
}
3) do while
Syntax:
Initialization
do
{
Statements
…………
……………
Increment / Decrement
} while (condition);
Example:
Print 1 to 5 Numbers:
int i=1;
do
{
System.out.println(i);
i++;
} while (i<=5);
int i=10;
do
{
System.out.println(i);
i++;
} while (i<=5);
do
{
if (i!=3){
System.out.println(i);
}
i++;
} while (i<=5);
Syntax:
Array Declaration;
for (declaration: Expression / Array){
Statements
…………
……………
}
Example:
String [] languages = {"COBOL", "C", "Java", "VBScript"};
mathOperations[0] = a+b;
mathOperations[1] = a-b;
mathOperations[2] = a*b;
1) break
• break statement is used to stop the execution and comes out of loop
• Mostly break statements are used in switch and in loops….
Example:
for (int i=1; i<=10; i++){
System.out.println(i);
if (i==4){
break;
}
}
2) continue
Example:
for (int i=1; i<=10; i++){
if (i%2 != 0){
continue;
}
System.out.println(i);
}
3) return
Example:
public class ControFlow {
System.out.println(obj.add(100, 200));//300
}
public int add(int a, int b){
int result=a+b;
return result;
}
}
//Create Object
Syntax:
String a="India";
String b="100";
int c=100;
String d="India123";
string e="$%^";
String f= "India123#$";
Operations on Strings:
1) Concatenating Strings
Example:
System.out.println("Selenium"+"Testing");//SeleniumTesting
System.out.println("Selenium"+1+1);//Selenium11
System.out.println(1+1+"Selenium");//2Selenmium
System.out.println(1+11);//12
2) String Comparison
A to Z (65 to 90)
a to z (97 to 122)
0- to 9 (48 to 57)
Example:
5) Java Arrays
• In Java, Array is an Object that holds a fixed number of values of a
single data type
• The length of Array is established when the Array is created.
• Array length is fixed and index starts from zero to n-1,
Declaration of Arrays
1st Method
Syntax:
dataType arrayName[]; Declare an Array/ Create an Array
arrayName = new dataType[size]; //Define size
arrayName[index] = value; //Assign a value
.
.
.
Example:
int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
System.out.println(a[1] + a[2]);//50
Assign values to Array elements that more than the length of Array (Run-
time Error)
int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
a[2] = 30;
a[10] =40;
System.out.println(a[1] + a[2]);//50
int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
System.out.println(a[0] + a[1]);//30
int a[];
a=new int[3];
a[0] = 10;
a[1] = 20;
//a[2] =10.23;
System.out.println(a[0] + a[1]);//30
2nd Method
Example:
int [] a = new int [3];
a[0] =10;
a[1] =20;
a[2] =30;
System.out.println(a[1] + a[2]);//50
3rd Method
Example:
int [] a= {10, 20, 30, 40};
System.out.println(a[1] +a[3]);//60
System.out.println(a[2]);//30
System.out.println(d[3]);//7.890
System.out.println(e[0]);//true