Wednesday, 29 August 2018

Java Concepts -4

Loop Statements/Iterative statements:
-------------------------------------
Loop Statements/Iterative statements are used to execute some portion of the script multiple times/iterations
a. While Loop:
b. Do while Loop:
c. For loop:

a. While Loop:
It will allow script to execute multiple iterations until given condition is true

Syntax:
while (condition){
----------
----------//Repeatable statements
}
Exp:
int n=1;
while (n <= 5){
System.out.println(n);
n++;
}

b. Do While Loop:
It will allow atleast one time to execute the block of statements irrespective of condition true/false

Syntax:
do{
    ----------
    ----------//statements
[break]
}


NOTE: [break] is used to terminate the iterations

Exp:
int n=1;

do {
System.out.println(n);
n++;
    }while (n <= 5);


Ex:
int n=1;

do {
System.out.println(n);
if(n==3){
break;
}
n++;
}while (n <= 5);

ex:
int n=6;
do {
System.out.println(n);
n++;
}while (n <= 5);

c. For Loop:
For Loop is used to execute a block of statement fixed number times/itersations

Syntax:
for(initialization; condition; increment/decrement){
------------
-----------//Repeatable statements
[break]
}

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


Array Declaration:
----------------------
Array is used to store multiple values with same data types

Syntax:
datatype[] array_name= new datatype[size];

size--> number of values to store
array index starts with zero
to find number of values in an Array we can use array.length

Exp: Create an array to store 3 values

//Array Declaration
String[] myCars= new String[3];
//to assign values
myCars[0]="Audi";
myCars[1]="Benz";
myCars[2]="BMW";

//to find size of an array
System.out.println("Number of values are: "+myCars.length);

//to read values from array using index
System.out.println(myCars[0]);
System.out.println(myCars[1]);
System.out.println(myCars[2]);

System.out.println("*******************");
//to read values from array using for loop
for(int i=0; i <= myCars.length-1; i++){
System.out.println(myCars[i]);

For-each:
Used to read each value from an array/collections

Syntax:
for( datatype variableName: array/collectionname)

//to read values from array using For -each
for(String myCar: myCars)
{
System.out.println(myCar);

}


ArrayList/Dynamic Array:
--------------------------------
When we don't know how many values to store in an Array we can use an ArrayList
Syntax:
ArrayList arrayname= new ArrayList();

//to assign values
Syntax:
arrayname.add(value);

//to find size of an ArrayList we can use ArrayList.size();

Exp: Create ArrayList to assign 3 string values

// ArrayList Declaration
ArrayList myCars= new ArrayList();

//to assign values
myCars.add("Audi");
myCars.add("Benz");
myCars.add("BMW");

//to find number of values
System.out.println("Number of values are: "+myCars.size());

//to read values from an arrayList using For each
for(String myCar: myCars){
System.out.println(myCar);
}


No comments:

Post a Comment