Tip:
Highlight text to annotate it
X
Hello, I'm Eli for edu4java
and today we are going to talk about the "while" instruction
sentence or instruction "while"
Let's start
We are going to create an example
We create an Integer variable and we initialize it zero
after we are going to create a "while" sentence
Let's take a look at the format. It has a format similar to the "if" instruction
It has a condition
and a block
If the condition
is true the block is executed. All this is just like the if
The only difference is that once the block was executed the "if" continued it's normal execution
With the "while" if the condition is true it executes the block
and after it executes the block it repeats
the evaluation of the condition and if it's true it executes the block again
and this can be undefined
It could go on going around for ever
Let's write an example. Let's write a condition "contador" (meter)
while (contador0)
then we accumulate it
if it isn't we don't
As you can see here
Let's run it first
2,2,3,3
-1
10
Ok!
Let's run it again
2+3+4
+5
-1 to stop
14
Perfect!
What happened before? we wanted to use the -1 to stop so that the while didn't go on for ever
bur we didn't want to add the -1
to the sum
So what we do now
if nuevoNumero>0 we add it if it isn't we don't
We have just finished an example for the while
We have already two examples with while
This is the common meter which goes on adding one inside a cycle
And this one is a bit more specific
It cycles while a condition is true
we change this condition with the insertion of external data
This is enough for "while"
The next tutorial is "for"
See you there!