Iteration (Loops) Flashcards

(20 cards)

1
Q

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.

A

program control

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

2) How many types of loops are there in Java?

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3) The three types of loops are:

1) f___ loop
2) w_____ loop
3 d__ w____ loop

A

for
while
do…while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

4) When would we use a ‘for loop’?

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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;)?

A

The counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

variable
initialized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

7) What do we call the second part of the ‘for’ header?

e.g. for (i=1; i <= 5; i++) so, i <= 5

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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(“*****”);
}

A

5 times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

9) What does the 3rd part of the ‘for’ header do?

e.g. for (i = 1; i <= 5; i++;) so, i++

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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
}
}
}
}

A

variable declarations
if statements
switch statements
even other loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

12) ‘While’ loops are s____________ simpler than ‘for’ loops’

e.g.
while ( /* test goes here */ )
{
// instruction(s) to be repeated go here
}

A

syntactically

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

13) While loops are useful for i________ v__________, or checking input data for errors.

A

input validation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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?

A

The while loop would be ignored.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

17
Q

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

a) for loop
b) while loop
c) do…while

18
Q

18) Can we use ‘break’ statements to terminate loops?

19
Q

19) Why would we use a ‘break’ statement in a loop?

A

To end the loop early.

20
Q

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?

A

All of them. See Java in 2 semesters chp 5, pp.84-87