Java Tutorial iii

Java Tutorial 3
(Java Conditional Statements, Java Loop Statements, String treatment inwards Java, Java Arrays Part-1)

Java Programming Fundamentals as well as OOPS concepts
--------------------------------------------------
Java Environment Setup as well as write First Java Program

I) Comments inwards Java

II) Java Data Types

III) Java Modifiers

IV) Variables inwards Java

V) Operators inwards Java
--------------------------------
VI) Java Conditional Statements
 

a) Types of Conditional statements
 

i) if statement

ii) switch statement
-----------------------------
b) Types of Conditions

i) Single Condition

Ex:

if (a > b) {
Statements
-------
--------
.
.

}

ii) Compound Condition

Ex:

if ((a > b) && or || (a < c)) {
---------
----------
.
.

}
iii) Nested Condition

if (a > b) {
 if (a < c) {
  if (a < d) {
---------
------
}
}
}
-------------------
c) Usage of Conditional statements inwards Selenium Test Automation
 

i) To insert Verification points

ii) For Error handling
-------------------------------
d) Usage of Conditional statements
i) Execute a block of statements when status is True.
Syntax:
if (condition) {
Statements
--------
------
-------
}
One agency (Positive) Condition
Example:

public static void principal (String [] args){
        int a, b;
        a = 10; b= 50;
       
        if (a > b) {
            System.out.println("A is a Big Number");
        }
    }
}
---------------------------------------
One agency (Negative) Condition

public static void principal (String [] args){
        int a, b;
        a = 10; b= 5;
       
        if (! (b > a)) {
            System.out.println("A is a Big Number");
        }
    }
}
-----------------------------------------
ii) Execute a block of statements when status is true; otherwise execute simply about other block of statements.
 

Syntax:

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

Example:

public static void principal (String [] args){
        int a, b;
        a = 10; b= 10;
       
        if (a > b) {
            System.out.println("A is a Big Number");
        }
        else {
            System.out.println("B is a Big Number");
        }
    }
}
--------------------------------------------
3) Execute a block of statements when a Compound status is true. 

Syntax:

if ((condition1) && or || (condition2)) {
Statements
----------
----------
----------
}

Example:

public static void principal (String [] args){
        int a, b, c;
        a = 10; b= 7; c = 5;
       
        if ((a > b) && (a > c)) {
            System.out.println("A is a Big Number");
        }
    }
--------------------------------
public static void principal (String [] args){
        int a, b, c;
        a = 10; b= 7; c = 15;
       
        if ((a > b) || (a > c)) {
            System.out.println("A is a Big Number");
        }
    }
------------------------------------
4) Decide with several alternates (else if)
 

Syntax:

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

Example:

Requirement:
Verify the attain of a value

Conditions:
if the value is inwards betwixt 1 to 100 thus display value is a Small number
if the value is inwards betwixt 101 to 1000 thus display value is a Medium number
if the value is inwards betwixt 1001 to 10000 thus display value is a Big number
Otherwise display the value is either cypher or negative value

Program:

    populace static void principal (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 {
        System.out.println("Value is Either Zero or Negaive Number");
    }
}
--------------------------------------
v) 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
--------
--------
}
}
}

Example:

public static void principal (String [] args){
        int a = 100, b = 90, c = 70, d = 50;
   
        if (a > b){
         if (a > c){
          if (a > d) {
              System.out.println("A is a Big Number");
          }
         }
        }
}
------------------------------
public static void principal (String [] args){
        int a = 100, b = 90, c = 70, d = 50;
   
        if (((a > b) && (a > c) && (a > d))) {
          System.out.println("A is a Big Number");
          }
}
----------------------------------
Difference betwixt Compound as well as Nested Conditions

If it is Compound status nosotros tin write unmarried else exercise only.

If it is Nested status nosotros tin write multiple else parts.
----------------------------------------------------
vi) Decide with several alternates (switch statement)
 

Syntax:

switch (expression) {

case value:
Statements
----------
----------
break;
case value:
Statements
----------
----------
break;
case value:
Statements
----------
----------
break;
default:
Statements
----------
-----
}

Example:

public static void principal (String [] args){
        char kind = 'Z';
       
        switch (grade){
        representative 'A':
            System.out.println("Excellent");
            break;
        representative 'B':
            System.out.println("Well Done");
            break;
        representative 'C':
            System.out.println("Better");
            break;
        default:
            System.out.println("Invalid Grade");
        }
}
------------------------------------------------------
Java Flow Control Statements
    Conditional Statements
    Loop Statement

i) We tin exercise conditional statements alone inwards our programs.

ii) We tin exercise Loop statements alone inwards our programs.

iii) We tin exercise Conditional statements as well as Loop statements together.
---------------------------------------------------------------
VII) Loop Statements
 

Whenever nosotros desire execute a block of statements multiple times thus nosotros exercise Loop structures.

There are 4 types of Loop structures inwards Java.

i) for loop

ii) piece loop

iii) create piece loop

iv) Enhanced for loop
---------------------
i) for loop
 

Description:

It repeats a block of statements for a specified pose out of times.

Syntax:

for (startvalue; endvalue; increment/Decrement){
Statements
----------
----------
----------
}

Example: Print 1 to 10 Numbers
public static void principal (String [] args){
       
        for (int i = 1; i <= 10; i++){
            System.out.println(i);
        }
        }
---------------------------------------------------
Example: Print 10 to 1 Numbers

public static void principal (String [] args){
       
        for (int i = 10; i >= 1; i--){
            System.out.println(i);
        }
        }
-----------------------------------
Example: Print 1 to 10 Numbers, except quaternary as well as seventh numbers
public static void principal (String [] args){
       
        for (int i = 1; i <= 10; i++){
            if ((i != 4) && (i != 7)) {
            System.out.println(i);
            }
        }
        }
-------------------------------------------------
ii) While loop

It repeats a block of statements piece status is True.

Syntax:

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

Example1: Print 1 to 10 Numbers

public static void principal (String [] args){
        int i = 1;
        piece (i <= 10){
            System.out.println(i);
            i++;
        }
        }
---------------------------------
Example2: impress 10 to 1 Numbers
public static void principal (String [] args){
        int i = 10;
        piece (i >= 1){
            System.out.println(i);
            i--;
        }
        }
-------------------------------------------
Example3: impress every tenth Number upward to 100
    populace static void principal (String [] args){
        int i = 10;
        piece (i <= 100){
            System.out.println(i);
            i = i + 10;
        }
        }
---------------------------------------------------
Example4: Print 1 to 10 Numbers, except quaternary as well as seventh numbers
public static void principal (String [] args){
        int i = 1;
        piece (i <= 10){
            if ((i != 4) && (i != 7)){
            System.out.println(i);
            }
            i++;
        }
                }
----------------------------------------------------       
iii) create piece loop

It repeats a block of statements piece status is true.
It executes statements at to the lowest degree in i lawsuit irrespective of the condition.

Difference betwixt piece loop as well as create piece loop:
---------------------------------------------
while loop outset checks the condition, if status is truthful thus it volition execute the statements.

do piece loop outset executes the statements thus it volition banking firm correspond the condition, if condition
is true, it volition proceed otherwise come upward out from the loop.
------------------------------------------------------
Syntax:

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

Example:

public static void principal (String [] args){
        int i = 1;
        create
        {
        System.out.println(i);   
            i++;
        }
        piece (i <= 10);
        }
---------------------------------
public static void principal (String [] args){
        int i = 11;
        create
        {
        System.out.println(i);   
            i++;
        }
        piece (i <= 10);
        }
--------------------------------------
iv) Enhanced for loop
It executes all elements inwards an Array.

Syntax:

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

Example1:

public static void principal (String [] args){
        String [] languages = {"C", "COBOL", "Java"};
        for (String lang: languages){
            System.out.println(lang);
        }
        }
Example2:
public static void principal (String [] args){
        int [] mathoperations = novel int[3];
        int a = 10, b =20;
        mathoperations[0] = a+b;
        mathoperations[1] = a-b;
        mathoperations[2] = a*b;
        for (int operation: mathoperations){
            System.out.println(operation);
        }
        }
------------------------------------------------------------
VIII) String Handling inwards Java
 

What is String?

String is a sequence of grapheme written double quotes.

String may convey Alfa bytes, numbers as well as especial characters.

Example:
System.out.println("Selenium Testing"); // selenium Testing
System.out.println("123 Selenium"); // 123 Selenium
System.out.println("Selenium*&123Testing");//Selenium*&123Testing

Creating strings:

String is considered Object inwards Java.

Example:
public static void principal (String [] args){
    String myTool = "Selenium"; // String variable
    String [] myTools =    {"UFT", "Selenium", "LoadRunner"}; //Array of Strings
   
    System.out.println(myTool);//Selenium
   
    for (String tool: myTools){
        System.out.println(tool);//Print Array of Strings
    }
}
---------------------------------
Concatenating Strings:

public static void principal (String [] args){
    String str1 ="selenium";
    String str2 =" Testing";
    System.out.println(str1 + str2);//Selenium Testing
    System.out.println("Selenium" + " Testing"); // Selenium Testing
    System.out.println(1 + 1 + "Selenium");//2Selenium
    System.out.println("Selenium" + 1 + 1);//Selenium11
    }
-----------------------------------
String Comparison:

i) String comparing using (==) operator.
It supports 2-way comparing (true/false)

ii) String comparing using equals() method
It supports 2-way comparing (true/false)

iii) String comparing using compareTo() method
It supports 3-way comparing (0, >1, <1)

if str1 == str2 thus 0
if str1 > str2 thus > 1 (Positive value)
if str1 < str2 thus < 1 (Negative value)

ANSI grapheme codes

A to Z (65 to 90)
a to z (97 to 122)
0 to nine (48 to 57)

Example:

public static void principal (String [] args){
    String str1 ="SELENIUM";
    String str2 ="selenium";
    String str3 ="SELENIUM";
    String str4 ="zselenium";
   
    //String comparing using == Relational operator
    System.out.println(str1 == str2);//false
    System.out.println(str1 == str3);//true
   
    // String comparing using equal() method
    System.out.println(str1.equals(str2));//false
    System.out.println(str1.equals(str3)); // true
   
    // String comparing using compareTo() method
    System.out.println(str1.compareTo(str3)); //0
    System.out.println(str2.compareTo(str1)); //Positive value
    System.out.println(str1.compareTo(str4)); //Negative value
        }
--------------------------------------------------
IX) Java Arrays
 

> Java Array is an object that holds fixed pose out of values of a unmarried information type.

> The length of an Array is established when the Array is created.

> Array length is fixed, Java Array has 0 based index.

Declaration of Arrays:

1st method:

int abc [] ; // Creating Array

abc = novel int [4]; //Defining size

abc [0] = 10; // Assigning Value
abc [1] = 20;
abc [2] = 30;
abc [3] = 40;
System.out.println(abc[0] + abc[2]); //40
----------------------------------------
2nd method

int [] abc = novel int [5]; // Creating Array with Size

abc[0] =10; //Assigning value
abc[1] =20;

System.out.println(abc[0] + abc [1]);//30
------------------------------------------------
3rd Method

// Creating Array as well as Assigning values

int [] abc = {10, 20, 30, 40, 50};

System.out.println(abc[1] + abc[3]);// 60
int a = abc.length;
System.out.println(a);//5
--------------------------------------------
Creating dissimilar type of Arrays


public static void principal (String [] args){
    char [] abc = {'A', 'B', 'C', 'D'}; //Array of Characters
    int [] xyz = {10, 20, 30, 40}; // Array of Integers
    String [] a = {"Selenium", "UFT", "LoadRunner"}; // Array of Strings
    boolean [] b = {true, false, false, true}; // Array of Boolean
   
    System.out.println(abc[1]); //B
    System.out.println(xyz[2]);//30
    System.out.println(a[1]); // UFT
    System.out.println(b[1]); //false
    }

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