Loop
• In programming languages, loops are used to execute a set of
instructions/functions repeatedly when some conditions
become true. There are three types of loops in java.
• for loop
• while loop
• do-while loop
Java For Loop
The simple for loop is same as C/C++. We can initialize variable,
check condition and increment/decrement value.
Syntax:
for(initialization;condition;incr/decr)
{
//code to be executed
}
class ForExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
• If you use two semicolons ;; in the for loop, it will be
infinitive for loop.
for(;;)
{
//code to be executed
}
Java While Loop
• The Java while loop is used to iterate a part of the program
several times. If the number of iteration is not fixed, it is
recommended to use while loop.
while(condition)
{
//code to be executed
}
class WhileExample
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
If you pass true in the while loop, it will be infinitive while loop.
while(true)
{
//code to be executed
}
Java do-while Loop
do
{
//code to be executed
}while(condition);
class DoWhileExample
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}
Java Break Statement
• The Java break is used to break loop or switch statement. It breaks the current flow of
the program at specified condition. In case of inner loop, it breaks only inner loop.
class BreakExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
break;
System.out.println(i);
}
}
}
Java Continue Statement
• The Java continue statement is used to continue loop. It continues the current flow of the
program and skips the remaining code at specified condition. In case of inner loop, it
continues only inner loop.
class ContinueExample
{
public static void main(String[] args)
{
for(int i=1;i<=10;i++)
{
if(i==5)
continue;
System.out.println(i);
}
}
}
Java Array
• Normally, array is a collection of similar type of elements that
have contiguous memory location.
• Java array is an object the contains elements of similar data
type. It is a data structure where we store similar elements.
• Array in java is index based, first element of the array is stored
at 0 index.
• Types of Array in java
• There are two types of array.
• Single Dimensional Array
• Multidimensional Array
Single Dimensional Array in java
Syntax to Declare an Array in java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
• Instantiation of an Array in java
arrayRefVar=new datatype[size];
class Testarray{
public static void main(String args[])
{
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
class Testarray1
{
public static void main(String args[])
{
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
Multidimensional array in java
• Syntax to Declare Multidimensional Array in java
dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];
• int[][] arr=new int[3][3];//3 row and 3 column
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
int[][] a = new int[3][4];
class Testarray
{
public static void main(String args[])
{
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];
//adding and printing addition of 2 matrices
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}
}
}
Irregular arrays or Jagged arrays
• A jagged array in Java is a collection of arrays where each array may
contain a varied number of elements.
• Jagged arrays are also known as "ragged arrays" or "irregular arrays".
• A jagged array is sometimes called an "array of arrays."
Declaration and Initialization of Jagged array
Datatype arrayName[][] = new datatype[numRows][];
arrayName[0] = new datatype[numColumns1];
arrayName[1] = new datatype[numColumns2];
...
Example:
#syntax
int[][] jaggedArray = new int[3][];
#initialize column elements
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
#initialize the values
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
1 3 5 7 9
0 2 4 6
11 12
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d jagged array directly
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
// calculate the length of each row
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}
}
Assigning Array References
• When you assign one array reference variable to another, you are simply changing what object that variable
refers to.
class Arrayref
{
public static void main(String[] args)
{
int a[]={1,2,3,4,5};
int b[]={3,4,5,6,7};
a=b; //assigning array reference
b[3]=20;
for(int i=0;i<=4;i++)
System.out.print(i);
Output: 3 4 5 20 7
Java For-each Loop | Enhanced For Loop
• It is mainly used to traverse the array or collection elements.
class Foreachloop
{
public static void main(String[] args)
{
int a[]={1,2,3,4,5};
for(int i:a)
System.out.print(i);