computational expression - arrays, do while loop, for loop...computational expression arrays, do...

25
Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational Expression 18-20 November, 2019 1 / 17

Upload: others

Post on 22-May-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Computational ExpressionArrays, Do While Loop, For Loop

Janyl Jumadinova

18-20 November, 2019

Janyl Jumadinova Computational Expression 18-20 November, 2019 1 / 17

Page 2: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: ArrayList class

To use the methods of ArrayList, we need to create an instance ofan ArrayList.

ArrayList has methods that allow us to add, remove, changeelements in the list.

ArrayList supports dynamic arrays that can grow as needed.

Iterator

Can use an Iterator class’s hasNext() and next() methods to processArrayList

Janyl Jumadinova Computational Expression 18-20 November, 2019 2 / 17

Page 3: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: ArrayList class

To use the methods of ArrayList, we need to create an instance ofan ArrayList.

ArrayList has methods that allow us to add, remove, changeelements in the list.

ArrayList supports dynamic arrays that can grow as needed.

Iterator

Can use an Iterator class’s hasNext() and next() methods to processArrayList

Janyl Jumadinova Computational Expression 18-20 November, 2019 2 / 17

Page 4: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Standard Java arrays

An array is a logical, homogenous collection of data elements,grouped together under a common variable name and referencedindividually by position number.

An array in Java is a reference type since it is an object.

To refer to a particular location or element in the array, we specify thename of the array and the position number of the particular elementin the array.

Janyl Jumadinova Computational Expression 18-20 November, 2019 3 / 17

Page 5: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Standard Java arrays

An array is a logical, homogenous collection of data elements,grouped together under a common variable name and referencedindividually by position number.

An array in Java is a reference type since it is an object.

To refer to a particular location or element in the array, we specify thename of the array and the position number of the particular elementin the array.

Janyl Jumadinova Computational Expression 18-20 November, 2019 3 / 17

Page 6: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Standard Java arrays

An array is a logical, homogenous collection of data elements,grouped together under a common variable name and referencedindividually by position number.

An array in Java is a reference type since it is an object.

To refer to a particular location or element in the array, we specify thename of the array and the position number of the particular elementin the array.

Janyl Jumadinova Computational Expression 18-20 November, 2019 3 / 17

Page 7: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Declaring Arrays

Arrays occupy a fixed amount of space in memory.

The programmer specifies the type of each element and the numberof elements required by each array so that the compiler may reservethe appropriate amount of space in memory for the array.

Janyl Jumadinova Computational Expression 18-20 November, 2019 4 / 17

Page 8: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Arrays

Janyl Jumadinova Computational Expression 18-20 November, 2019 5 / 17

Page 9: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Arrays

Janyl Jumadinova Computational Expression 18-20 November, 2019 6 / 17

Page 10: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Arrays

Janyl Jumadinova Computational Expression 18-20 November, 2019 7 / 17

Page 11: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Arrays

Janyl Jumadinova Computational Expression 18-20 November, 2019 8 / 17

Page 12: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Arrays

Janyl Jumadinova Computational Expression 18-20 November, 2019 9 / 17

Page 13: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Declaring Arrays

Arrays can be initialized using a comma-separated initializer list.

int [] n = {32, 27, 64, 19, 95, 14, 90, 70, 37}.

The size of the array is inferred from the number of elements in theinitializer list.

Janyl Jumadinova Computational Expression 18-20 November, 2019 10 / 17

Page 14: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Declaring Arrays

Arrays can be initialized using a comma-separated initializer list.

int [] n = {32, 27, 64, 19, 95, 14, 90, 70, 37}.

The size of the array is inferred from the number of elements in theinitializer list.

Janyl Jumadinova Computational Expression 18-20 November, 2019 10 / 17

Page 15: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Processing Arrays

To iterate through an array, we use a loop.

To get the number of elements (size) in the array:arrayName.length;

int i = 0;

int [] n = new int [10];

while(i < n.length) {n = i*10;

i++;

}

Janyl Jumadinova Computational Expression 18-20 November, 2019 11 / 17

Page 16: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: Repetition structures

while() loop

while (condition) { action(s) }

Janyl Jumadinova Computational Expression 18-20 November, 2019 12 / 17

Page 17: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: Repetition structures

while() loop

while (condition) { action(s) }

- Example:

int num = 10;

while(num < 10) {

System.out.print(num + " ");

num++;

}

Output:

Janyl Jumadinova Computational Expression 18-20 November, 2019 13 / 17

Page 18: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: Repetition structures

while() loop

while (condition) { action(s) }- Example:

int num = 10;

while(num < 10) {

System.out.print(num + " ");

num++;

}

Output:

Janyl Jumadinova Computational Expression 18-20 November, 2019 13 / 17

Page 19: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Review: Repetition structures

while() loop

while (condition) { action(s) }- Example:

int num = 10;

while(num < 10) {

System.out.print(num + " ");

num++;

}

Output:

Janyl Jumadinova Computational Expression 18-20 November, 2019 13 / 17

Page 20: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Repetition structures

do{ } while() loop

do {action(s) } while(condition);

Janyl Jumadinova Computational Expression 18-20 November, 2019 14 / 17

Page 21: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Repetition structures

do{ } while() loop

do {action(s) } while(condition);

- Example:

int num = 10;

do {

System.out.print(num + " ");

num++;

}

while(num < 10);

Output: 10

Janyl Jumadinova Computational Expression 18-20 November, 2019 15 / 17

Page 22: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Repetition structures

do{ } while() loop

do {action(s) } while(condition);

- Example:

int num = 10;

do {

System.out.print(num + " ");

num++;

}

while(num < 10);

Output: 10

Janyl Jumadinova Computational Expression 18-20 November, 2019 15 / 17

Page 23: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

Repetition structures

do{ } while() loop

do {action(s) } while(condition);

- Example:

int num = 10;

do {

System.out.print(num + " ");

num++;

}

while(num < 10);

Output: 10

Janyl Jumadinova Computational Expression 18-20 November, 2019 15 / 17

Page 24: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

For repetition structure

for ( init; testForFinal; modification ) { actionStatement(s);}The init section should create and initialize your loop controlvariable.

The testForFinal section should be a Boolean expression to test forthe final value.

The modification section should be an arithmetic statement(usually increment ++ or decrement - - action) to modify the loopcontrol variable.

Janyl Jumadinova Computational Expression 18-20 November, 2019 16 / 17

Page 25: Computational Expression - Arrays, Do While Loop, For Loop...Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational

For loop

public class ForLoop {

public static void main ( String args[] ) {

for ( int counter = 1; counter <= 10; counter++)

System.out.println ( counter );

}

}

Janyl Jumadinova Computational Expression 18-20 November, 2019 17 / 17