So far we've only seen programs
that run each statements only once. Thus, using java loop let us execute
the same statements more than once. Java Loops are very common in writing programs to
get inputs from
users, do something with the input, then get more input from the user and do
something with it again.
Like if
statements loops rely on conditional expressions to tell them when to get out of
the loop. If we
don't have conditional expressions loops would run on forever. Infinite loops
are useful too, the windows
we are running, it's a very good example of infinite loop.
Lets start with the famous
java for loops
with our example.
The basic principle behind a java for loop
is that the loop itself maintains a
counter variable, a variable's value
increases each time the body of the loop is executed
The basic format of a java for loop
for ( initialization-expression;
testing-expression; counting-expression;)
{
statements;
}
The three expressions in the parentheses following the keyword for control how the for loop works.
-- The
initialization-expression
is executed before the loop begins. If we haven't initialize a
counter variable before the loop, we
can do so here.
-- The
testing-expression
is evaluated each time the loop is executed to determine whether the loop
should continue. In other word, if
this condition is still true, continue looping otherwise exit the loop.
-- The
counting-expression
is evaluated each time the loop executes. Its job is to increment or decrement
the counter variable.
Example) We will use a loop to print out the numbers from 0 to 5.
ClickHere to download Count.java
Now, look at the example above, notice we
have i < 6, the reason is because once i = 6 or higher
the loop will end, hence the highest we would be able to print out is 5.
Sometimes we want to get out of a
loop before executing some statements, thus we have a keyword "break,"
it can force out a loop.
Example) This program will only output 0
Notice, here the statement will only execute once because once we reach to break, the loop will end.
Example) This program prints out even and odd numbers
ClickHere to download SumEven.java
.
Now, lets look at one of the most basic loop " java while loop." The basic format for java while loops is as follow.
while (expression)
statement;
The while statement begins by evaluating the
expression. If the expression is true,
the statement is executed.
Then the expression is evaluated again, and the whole process repeats until the
expression is false.
Example) Using a while loop to print out numbers from 1 to 5
ClickHere to download WhileCount.java
Now, lets look at another loop, the
java do-while loop
( sometimes called a do loop
) is very similar to a java while loop.
In java do-while loops, we execute the statement first than check the condition
expression after. Here is the basic
form for a do while loop.
do
{
statement;
}while(expression);
Example) Using a do-while loop to print out numbers from 1 to 5.
ClickHere to download DoWhileCount.java
Now lets wrap up everything together and see some bigger examples.
ClickHere to download Forloop.java
ClickHere to download Whileloop.java
ClickHere to download Dowhileloop.java