Iteration (Loops) Flashcards
(20 cards)
1) Iteration
Iteration is the form of p_______ c________ that allows us to instruct the computer
to carry out a task several times by repeating a section of code.
program control
2) How many types of loops are there in Java?
3
3) The three types of loops are:
1) f___ loop
2) w_____ loop
3 d__ w____ loop
for
while
do…while
4) When would we use a ‘for loop’?
If we wanted to repeat a loop a set number of times.
e.g.
for (int i = 1; i <= 5; i++) // loop to repeat 5 times
{
System.out.println(“*****”); // instruction to display one row
}
5) for loop
As you can see there are three bits of information in the header of the for loop,
each bit separated by a semi-colon:
for (int i = 1; i <= 5; i++)
What do we call the first part (int i = 1;)?
The counter
6) What is the counter?
e.g. i=1;
A counter is a v___________ that has to be created.
We use it to keep track of how many times we have been through the loop so far.
In this example we have i__________ the counter to 1.
variable
initialized
7) What do we call the second part of the ‘for’ header?
e.g. for (i=1; i <= 5; i++) so, i <= 5
A test
The second bit of information in the header is a test, much like a test when carrying out selection.
When the test returns a boolean value of true the loop repeats;
when it returns a boolean value of false the loop ends.
8) How many times do we want the loop to repeat in this code?
for(int i = 1; i <= 5; i++) // counter initialized to 1
{
System.out.println(“*****”);
}
5 times
9) What does the 3rd part of the ‘for’ header do?
e.g. for (i = 1; i <= 5; i++;) so, i++
The 3rd part increments the value of the counter i by 1 after every iteration of the loop.
The loop will stop once the counter has been incremented to 6, when the is it no longer <= to 5.
10) The body of the ‘for’ loop
The body of the loop can contain any number and type of instructions!
It can contain:
v_______ d________
i__ s____________
s_________ s_______
even other l_______
e.g.
public class DisplayEven
{
public static void main(String[] args)
{
System.out.println(“** Even numbers from 10 to 1 **”);
System.out.println();
for(int i=10; i >= 1; i–) // loop through the numbers 10 down to 1
{
// body of the loop contains an ‘if’ statement
if (i%2 == 0) // check if number is even
{
System.out.println(i); // number displayed only when it is checked to be even
}
}
}
}
variable declarations
if statements
switch statements
even other loops
11) The while loop
Sometimes, a repetition is required that is not fixed and a for loop is not the best one to use in such a case.
The w_____ loop offers one type of non-fixed iteration.
while
12) ‘While’ loops are s____________ simpler than ‘for’ loops’
e.g.
while ( /* test goes here */ )
{
// instruction(s) to be repeated go here
}
syntactically
13) While loops are useful for i________ v__________, or checking input data for errors.
input validation
14)
Imagine you are writing a program that requires a user to input their exam mark and the exam was out of 100.
The user would have to enter a number from 0-100, however, they might make a mistake and enter a higher number or even a lower number.
We could use a while loop to catch this error.
e.g.
while (input < 0 | | input > 100)
{
System.out.println(“Invalid grade, please try again”);
}
Q: What would happen if the user did not enter a number lower than 0 or higher than 100?
The while loop would be ignored.
15) The ‘do…while’ loop
How is the ‘do…while’ loop different from the ‘while’ loop?
The ‘do…while loop’ has its t_____ at the end of the loop, rather than at the beginning.
test
16) True or False
As we know, a ‘while’ loop may not even be run.
Because the ‘do…while’ loop has its test at the end of the loop, it will always iterate through at least once.
True
17) Guidelines - Choosing the right loop
a) if the number of repetitions required can be determined prior to entering the
loop—use a ___________ loop.
b) if the number of repetitions required cannot be determined prior to entering the
loop, and you wish to allow for the possibility of zero iterations—use a __________ loop.
c) if the number of repetitions required cannot be determined before the loop, and
you require at least one iteration of the loop—use a __________ loop
a) for loop
b) while loop
c) do…while
18) Can we use ‘break’ statements to terminate loops?
Yes
19) Why would we use a ‘break’ statement in a loop?
To end the loop early.
20) Why would we need to end a loop early? Here is an example:
Consider a program that allows the user a maximum of three attempts to guess a secret number. This is
an example of a non-fixed iteration—but the iteration does have a fixed upper limit of three.
a) What type of loop/s could we use for this example?
All of them. See Java in 2 semesters chp 5, pp.84-87