Tip:
Highlight text to annotate it
X
Hello, I'm Eli for edu4java and this is tutorial number 8;
"instruction or sentence 'for'"
and we are also going to take a look at "arrays"
Let's start with this example
which we have already seen
A for which iterates
from 0 to 2
Printing the numbers; 0,1,2
If someone has any doubt please take a look at the last tutorial about "while"
This same "while"
can be done in another way
It can be done with a "for"
Let's use this "for" by default
not this because it is used for arrays
Let's write 3
and we write Syso
Run
This does exactly the same
thing as what we saw before
Why?
for is an iteration instruction just as the "while"
It has it's block which is similar
What is different is what there is inside the parenthesis
Inside the condition of a "while" there is a condition and inside a "for" there are 3 parts separated by a ;
the first part is the initialization
the second is the condition just as the "while" and the third one which is the part of incrementation
These 3 parts are going to be executed in different moments
This part
(initialization) will be executed before cycling. If we compare it with the while is very similar
This is executed just once before it starts cycling. Here it is just the same. What we write here is going to be executed just once.
Here we can put more instructions but basically
this is executed once
This second one is similar to the other one. It is a condition. While it is true this will be executed. If it's false it goes out.
This last one is as if we wrote it at the end
of the block
It will be executed if it goes into the block and as the last instruction of the block
Just as it's executed here
This is why it does the same
Let's run it once again so you can see
To see a more real example
we are going to add an array
int [] array=
between braces
2,3,5,7,1
Ok, this is the declaration of an array
With this we create a variable called array which belongs to the type array of int
An array of int is like a chest of drawers
Imagine that every drawer is an int type variable
We could imagine the int array as a chest of drawers of ints
Automatically here we set
what it has inside
This is a way of writing
an array constant
of int type
The elements are written in between braces
and separated by commas
Here we have an array with 2,3,5,7,1
The "for" is specially
good for this
With just one click I have an iterator that goes from i=0 till
the length of the array
The object array has a variable "length"
With this we can go through the array. Let's print it on the console
So you can see it
To enter the positions of the array we have to write the name of the variable of the array and the position inside the chest of drawers
If we wrote 0 we would enter the position 0
Let's do it this way. Let's comment this
If we print the position 0
a 2 is going to be printed, why? because there is a 2
in the position 4 we have a 1. 0, 1,2,3, 4
In the position 3
we have a 7
0,1,2,3,
In the position 5 we have an error
We had said that the chest of drawers started in 0 and finished in the length of the array minus 1.It goes from 0 to 4
When we try to enter the position 5 it's a drawer that doesn't exist
that why it gives me an error
This is very nice but we don't win a lot with this
better with this
Notice that if we change the position 0 to i
and we go round the numbers between 0
and the array minus 1.Because when "i" is the same as the array it is not going to execute it
With just one instruction we are going to run it several times
The first time it goes into the cycle "i"=0 and it enters the position 0 which is 2 so it prints "2". The second time
it equals 1
So the array in the position 1 is equal to 3
It prints 3 and the same with 5,7 and 1
What else can I do to show you the use of this?
This has a big advantage if there are a lot of numbers.Here there are just 5 but imagine that there are hundreds of them
Let's write some more
so that it's more clear
We run it
If we had to do this by hand we would have to write System.out for each of them. If there were hundreds
we would have to write hundreds of lines. We are saving a lot of lines. With only three lines
we are doing what we would have to do with hundreds of them. This is the functionality of the array and the for
Let's write something more interesting
Instead of printing in this way, we are taking out the ln
and we are going to put a ","
See you can see that there are printed one after the other
Now we are going to increment in one all the elements of the array
"for"
We write another for to iterate
and we can put the same print to see how it looks
Here the first ones are printed
then the second ones are printed; but there are the same. We should increment them in the middle
Let's do another for
and we are going to
for each of the elements
array[i] we are going to assign
Let's use the operator incremental so that we get use to it. Let's get each of the elements and we are going to add them one
Let's run it again
Here you can see the first printing which was generated by this for
After we wrote that we were going to increment in one
We do a for that for each element we are going to add one. First the position 1
which is 2 and we are going
to increment 2 in one
3+1 and 5+1 and we do the same and print it
This is very simple but it is so that you get the concepts of array
and "for" which are used a lot in java
In the next tutorial we are going to see for and array too but with a more complicated example
Play with this exercise until you feel comfortable with an array
Play with the for too. You can change this for example for a 2
and you are going to see that
that all the work is done
from here onwards
Ok, practice a bit that the next one is a bit more complicated. See you in the next tutorial!