Java Programming- Arrays & Control Statements
Array
2 november
Types of Array in java
There are two types of array.
Single Dimensional Array in Java
2 november
Syntax to Assign value to an Array
Syntax to Declare an Array
Example
int myNum[ ] = {10, 20, 30, 40};
String cars[ ] = {"Volvo", "BMW", "Ford", "Mazda"};
Example
int[ ] myNum ;
One Dimensional Array
2 november
Single Dimensional Array
2 november
Instantiation of an Array in Java
Example
int myNum[];
myNum = new int[4];
String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
Output:
Volvo
Print - Single Dimensional Array
Change an Array Element
2 november
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
Output:
Opel
Array Length
2 november
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
Output: 4
Multidimensional Arrays
2 november
A multidimensional array is an array containing one or more arrays.
To create a two-dimensional array, add each array within its own set of curly braces:
Example
int myNumbers[][] = { {1, 2, 3, 4}, {5, 6, 7} };
Syntax to Declare Multidimensional Array in Java
Multidimensional Arrays
2 november
A multidimensional array is an array containing one or more arrays.
To create a two-dimensional array, add each array within its own set of curly braces:
Syntax to instantiate Multidimensional Array in Java
int arr[][]=new int[4][5];
Example to instantiate Multidimensional Array in Java
int twoD[][] = new int[4][5];
twoD[0][0] = 1;
twoD[0][1] = 5;
Print- Multidimensional Arrays
2 november
myNumbers is now an array with two arrays as its elements.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2];
System.out.println(x);
Multidimensional Array Length
2 november
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7}};
System.out.println(myNumbers.length);
Output:
2
Control Statements
Selection Statements
Java supports two selection statements: if and switch.
if (condition) statement1;
else statement2;
Nested ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else
int a, b;
//...
if(a < b) a = 0;
else b = 0;
Sequence of nested ifs is the if-else-if ladder
Output
April is in the Spring.
The switch statement is Java’s multiway branch statement. It often provides a better alternative than a large series of if-else-if statements.
switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
Break;
…
case valueN :
// statement sequence
break;
default:
// default statement sequence
}
i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.
Output
Iteration Statements
Java’s iteration statements are for, while, and do-while.
while(condition) {
// body of loop
}
int n = 10;
while(n > 0)
{
System.out.println(“n is” +n);
n=n-1;
}
Iteration Statements
do {
// body of loop
}
while (condition);
int n = 10;
do
{
System.out.println(“n is” +n);
n=n-1;
}
while(n > 0)
Iteration Statements
int n;
for(n=10; n>0; n--)
{
System.out.println(“n is” +n);
}
Jump Statements
Java supports three jump statements: break, continue, and return.
The general form of the labeled break statement is shown here: break label;
break;
if(a =10)
break;
Jump Statements
The return statement is used to explicitly
return from a method
Thank You