Iteration Continued

Download Report

Transcript Iteration Continued

Iteration
Iteration
 What is iteration known as?
 There are different types of loop
– Repeat-until loops
– For loop
 When do loops terminate?
For Loop
 Syntax of the For loop…
for (Initialisation; Test; Increment){
Statements...
}
Initialisation: what is to be done once before the loop is started.
This is executed once at the start of the loop.
Test: what is tested prior to any execution of the loop. The test
determines whether the loop is executed or completed. If it is
true, the loop continues, if it is false, the loop ends (statements in
the body of the loop are not executed). This expression is
evaluated at the top of each iteration of the loop
example:
i<maxNumber;
Increment: The statement that is carried out at the end of each
repetition
example: i++
For Loop
 For loops are usually used when the number
of iterations is known in advance (this number
can be a variable)
 All three components (initialisation, test,
increment) of the for loop are optional.
 It is possible to declare the loop variable in
the “for” statement (int i=0; i<100;
i++)…remember the “scope” of this
variable will be limited to the loop.
For loop example
// Calculate the sum of all numbers between 0 and 99
public class CountAll{
public static void main (String args[]){
int count = 0;
int sum=0;
for (count = 0; count<=99; count++){
sum + = count;
}
System.out.println (“the sum of all the numbers between 1 and 99
is” +sum);
}
}
For Loop Example
//For Loop example
public class forLoop{
public static void main (String args[]){
for (int i = 0, j = 10; i < j; i++, j--) {
System.out.println ("i = " + i);
System.out.println ("j = " + j);
}
}
}
while Loop
 Syntax of the While Loop…
while (expression) {
statement
}
 A Java programming language keyword used to declare a loop
that iterates a block of statements. The loop`s exit condition is
specified as part of the while statement.
 This type of loop is for iterations that take place zero times or
more.
 While is the equivalent of “as long as XYZ is true”
 In a while loop the condition is tested before the loop is executed
and again before the repetition of the loop.
While Example
…
while (age <= 65)
{
balance = (balance + payment);
}
While Loop Example
public class DisplayResult
{
public static void main(String [] args)
{
int mark = 109;
System.out.print("Whats is the exam result?");
while (mark < 0 || mark > 100)
{
// display error message
System.out.println ("Invalid mark: please re-enter");
}
}
}
While loop Example
/*
Java application which counts from 0 - 9
*/
class whileExample {
public static void main(String[] args)
{
int i = 0;
while (i < 10)
{
System.out.print(i);
System.out.print(" ");
i = i + 1;
}//end while
}//end main
}//end class While example
do...while
 Syntax of the do-while loop…
do {
statement(s)
}
while (expression);
 A Java keyword used to declare a loop that will iterate a block of
statements. The loop`s exit condition can be specified with the
"while" keyword
 In the do-while loop, the decision is made at the end of the loop
 The loop is guaranteed to be done at least once.
 Used in situations where something must be executed but the
number of repetitions is not known.
do…while example
/*
Java application which counts from 0 - 9
*/
class DoWhileDemo {
public static void main(String[] args)
{
int i = 0;
do
{
System.out.print(i);
System.out.print(" ");
i = i + 1;
}
while (i < 10);
}//end main
}//end class While Demo