1 of 23

Java Programming- Arrays & Control Statements

2 of 23

Array

2 november

  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

3 of 23

Single Dimensional Array in Java

2 november

Syntax to Assign value to an Array

  1. dataType arr[ ] = { val1,val2..};

Syntax to Declare an Array

  1. dataType[ ] arr; (or)
  2. dataType arr[ ];

Example

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

String cars[ ] = {"Volvo", "BMW", "Ford", "Mazda"};

Example

int[ ] myNum ;

4 of 23

One Dimensional Array

2 november

5 of 23

Single Dimensional Array

2 november

Instantiation of an Array in Java

  1. type array-varname[ ];
  2. array-varname = new type [size];

Example

int myNum[];

myNum = new int[4];

6 of 23

String[] cars;

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars[0]);

Output:

Volvo

Print - Single Dimensional Array

7 of 23

Change an Array Element

2 november

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

cars[0] = "Opel";

System.out.println(cars[0]);

Output:

Opel

8 of 23

Array Length

2 november

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

System.out.println(cars.length);

Output: 4

9 of 23

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

  1. dataType[][] arrayRefVar; (or)
  2. dataType arrayRefVar[][];

10 of 23

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;

11 of 23

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);

12 of 23

Multidimensional Array Length

2 november

int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7}};

System.out.println(myNumbers.length);

Output:

2

13 of 23

Control Statements

  • Java’s program control statements can be put into the following categories: selection, iteration, and jump.
  • Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.
  • Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops).
  • Jump statements allow your program to execute in a nonlinear fashion.

14 of 23

Selection Statements

Java supports two selection statements: if and switch.

  • if

if (condition) statement1;

else statement2;

  • Nested ifs

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;

15 of 23

  • The if-else-if Ladder

Sequence of nested ifs is the if-else-if ladder

Output

April is in the Spring.

16 of 23

  • switch

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

}

17 of 23

  • switch

i is zero.

i is one.

i is two.

i is three.

i is greater than 3.

i is greater than 3.

Output

18 of 23

Iteration Statements

Java’s iteration statements are for, while, and do-while.

  • while

while(condition) {

// body of loop

}

int n = 10;

while(n > 0)

{

System.out.println(“n is” +n);

n=n-1;

}

19 of 23

Iteration Statements

  • do-while

do {

// body of loop

}

while (condition);

int n = 10;

do

{

System.out.println(“n is” +n);

n=n-1;

}

while(n > 0)

20 of 23

Iteration Statements

  • for

int n;

for(n=10; n>0; n--)

{

System.out.println(“n is” +n);

}

21 of 23

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

break;

if(a =10)

break;

22 of 23

Jump Statements

  • continue

  • return

The return statement is used to explicitly

return from a method

23 of 23

Thank You