The Java programming language

(ε-faster than slow motion)

Arrays

Arrays

Arrays provide a way of holding more than one value with the same variable name

Declaring Arrays

If we know the size but not the content of the array


Declaring Arrays

If we know the content of the array


Loops

The while loop

initialize_index;
while ( logical_expression ){
do something;
change_index_value;
}

The while loop


The indexed for loop

for (initialize_index; logical_expression; change_index_value){
do something;
}

The indexed for loop


The for each loop

for (TYPE element_name: array_name ){
do something with element_name;
}

The for each loop


More on Arrays

and Strings

Strings can be viewed as char Arrays

Strings as Arrays


Accesing the character at position i :

month_array[i]

Strings as Arrays


Accesing the character at position i :

month_array.charAt(i)

To try by yourself


Use binary_number.charAt(i) in a loop to check if the character at position i is a 0 or a 1

More on loops

We can use loops for counting

Counting with loops

Count the numbers from 1 to 100 using a while loop


Using a for loop


Counting with loops

Let's use a loop to check if a number n is prime


Test: n divisible ONLY by n and 1


Procedure: Check if any number from 0 to n-1 divides n

Counting with loops

Checking if a number is prime with a while loop

Counting with loops

Checking if a number is prime with a for loop

Nested loops

How we do determine the prime numbers between n= 1 and n=100

Methods

Methods

An essential programming skill

Realising when you can break up a task into sub-tasks

Methods

Think of methods as machines that take some input and produce some output

Methods are defined by their input and their output

Methods

Think of methods as machines that take some input and produce some output

You can also think of them as mathematical functions

Methods are defined by their input and their output

Methods

Think of methods as machines that take some input and produce some output

You can also think of them as mathematical functions

Methods are defined by their input and their output

Methods in Java

Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

Methods in Java


Methods in Java are defined by:

Try something by yourself


Methods in Java are defined by:

Resources

/