Computational Expression Arrays, Do While Loop, For Loop Janyl - - PowerPoint PPT Presentation

computational expression
SMART_READER_LITE
LIVE PREVIEW

Computational Expression Arrays, Do While Loop, For Loop Janyl - - PowerPoint PPT Presentation

Computational Expression Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019 Janyl Jumadinova Computational Expression 18-20 November, 2019 1 / 17 Review: ArrayList class To use the methods of ArrayList , we need to create


slide-1
SLIDE 1

Computational Expression

Arrays, Do While Loop, For Loop Janyl Jumadinova 18-20 November, 2019

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

slide-2
SLIDE 2

Review: ArrayList class

To use the methods of ArrayList, we need to create an instance of an ArrayList. ArrayList has methods that allow us to add, remove, change elements in the list. ArrayList supports dynamic arrays that can grow as needed.

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

slide-3
SLIDE 3

Review: ArrayList class

To use the methods of ArrayList, we need to create an instance of an ArrayList. ArrayList has methods that allow us to add, remove, change elements in the list. ArrayList supports dynamic arrays that can grow as needed. Iterator Can use an Iterator class’s hasNext() and next() methods to process ArrayList

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

slide-4
SLIDE 4

Standard Java arrays

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

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

slide-5
SLIDE 5

Standard Java arrays

An array is a logical, homogenous collection of data elements, grouped together under a common variable name and referenced individually by position number. An array in Java is a reference type since it is an object.

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

slide-6
SLIDE 6

Standard Java arrays

An array is a logical, homogenous collection of data elements, grouped together under a common variable name and referenced individually 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 the name of the array and the position number of the particular element in the array.

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

slide-7
SLIDE 7

Declaring Arrays

Arrays occupy a fixed amount of space in memory. The programmer specifies the type of each element and the number

  • f elements required by each array so that the compiler may reserve

the appropriate amount of space in memory for the array.

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

slide-8
SLIDE 8

Arrays

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

slide-9
SLIDE 9

Arrays

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

slide-10
SLIDE 10

Arrays

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

slide-11
SLIDE 11

Arrays

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

slide-12
SLIDE 12

Arrays

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

slide-13
SLIDE 13

Declaring Arrays

Arrays can be initialized using a comma-separated initializer list. int [] n = {32, 27, 64, 19, 95, 14, 90, 70, 37}.

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

slide-14
SLIDE 14

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 the initializer list.

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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Review: Repetition structures

while() loop while (condition) { action(s) }

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

slide-17
SLIDE 17

Review: Repetition structures

while() loop while (condition) { action(s) }

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

slide-18
SLIDE 18

Review: Repetition structures

while() loop while (condition) { action(s) }

  • Example:

int num = 10; while(num < 10) { System.out.print(num + " "); num++; }

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

Repetition structures

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

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

slide-21
SLIDE 21

Repetition structures

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

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

slide-22
SLIDE 22

Repetition structures

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

  • Example:

int num = 10; do { System.out.print(num + " "); num++; } while(num < 10);

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

For repetition structure

for ( init; testForFinal; modification ) { actionStatement(s);} The init section should create and initialize your loop control variable. The testForFinal section should be a Boolean expression to test for the final value. The modification section should be an arithmetic statement (usually increment ++ or decrement - - action) to modify the loop control variable.

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

slide-25
SLIDE 25

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