Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 4.56 KB

File metadata and controls

81 lines (58 loc) · 4.56 KB

Core elements of programs - Iteration

Introduction

Often we want to repeat certain things. For example, let's say we had an algorithim for eating breakfeast cereal. It could like something like:

1. Put cereal in bowl
2. Add milk to cereal
3. Spoon cereal and milk into mouth
4. repeat step 3 until all cereal and milk is eaten
5. Rinse bowl and spoon

Using iteration makes this algorithim possible - repeating step 3 untill it is time to move on. The two main types of loops are while and for loops. They're named quite sensibly, with while loops doing repeating while something is true, and for loops repeating for a certain amount of time.

While loops

While loops let you repeat something while a condition is true. Here's the above alogrithim... questionably rewritten in dodgy java code.

Bowl bowl = new Bowl();
bowl.add(new Cereal());
bowl.add(new Milk());

while (!bowl.isEmpty()) {
	bowl.eat();
}

bowl.rinse();

Note that the statement inside the while loop is kinda exactly like an if. It evaluates to go true or false. We even use a not (the exclemation mark) as well.

Activity 1 - make a while loop that will count to the number 4613 (or any arbitrarily large number)

It is possible to make a loop that runs forever. This is generally bad, and you should try to avoid it.

For loops

Now loops are also useful for counting things. You can use while loops to count up things, as you did with Activity 1. But there is in fact a better way. That better way is called a for loop!

They're quite a bit more complicated than while loops syntax wise. Look at the example, try to figure out what's happening, and then I'll deconstruct it.

for (int counter = 0; counter<=4613; counter++) {
	System.out.println(counter);
}

This remakes the program you wrote for activity 1. Let's deconstruct it!

  • for ( - starts the for loop. Pretty easy
  • int counter = 0; creates a new variable, to count the iterations, and starts it at 0. This variable creation bit always goes first.
  • counter<=4613; This dictates when the loop should run. This always comes second.
  • counter++) { This dictates what should be done to counter at the end of every loop. This always comes last. It also ends the for and opens the code loop block.

do while

While loops only ever run if their condition is true. So for example if you had an empty bowl, it would have never eaten anything. But sometimes you do want to run at least once, and then loop if a condition is true. For this we have the do while loop!

do {
	///Code to loop over...
} while (condition);

It might be a bit odd seeing the while on the same line as the closing parenthesis. In this case it makes more sense to do this, but in actual fact Java ignores whitespace - any spaces, new lines or tabs, Java dosen't care about (well it does care about having spaces between words, but other than that). Of course it is important to have neatly formatted code, otherwise you can't work well, as you won't just not understand your code, but you will barely be able to read it.

*Activity 2 - make a program that prints out the bestest number. BUT only people who know the password get to see the bestest number! It should ask for the password, and keep on asking intill they get it right.

Activitites

  1. If we list all the (positive integer) numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000
  2. The fibonanci sequence is generated by adding the previous two terms to create the next term. Starting with 1, it goes 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. Find the sum of numbers in the fibonacci sequence, that are less than four million, and are divisible by 8.
  3. Find the 4613th prime number.

These activities are a big step up from previous ones. You definitely should do at least the first one. Doing the 2nd one is also really good, and shouldn't be too hard. The 3rd problem is quite hard, but can be done. If you can, try to solve it; it will really extend you and have you solve problems where you really have to think about how you're going to solve it. BUT if you're busy, or don't have much time, don't worry about it. As a guide, you definitely shouldn't spend more than 20 mins on each If you find yourself spending that much time, just stop/move on. You should be able to get (at least the first two) easily within 10 minutes.

Source: I stole took inspiration from many of the problems on projecteuler.net . Activity 1 is problem 1, activity 2 is problem 2.