For and For Each Loops, Dot Products

Programming: For Loops and For Each Loops

From what I could find, the best way to describe a For Loop is where it’s used to repeat a specific block of code a known number of times. If the number of times isn’t known, then a While Loop is more appropriate. The hilarity is that sometimes the computer will know how many times the repetition will need to happen, but you don’t, so it’s still classified as known.

A couple of examples I found were:

  1. When you ask a player to guess a pre-determined number between 1 and 100. You don’t have a way to know how many guess it will take.
  2. Return all of the odd numbers from 1 to 1001.

The syntax for a For Loop looks like this:


The init is where you declare and initialise the loop control variables. Next, the condition of the For Loop is checked; if it’s true, the body of the loop runs, but if it’s false then the loop doesn’t execute and the script moves on. After the loop is run, it looks at the increment statement, which allows you to update any control variables. This means the loop can keep… looping until the condition becomes false.

The test I did for this to see if I understood all this was the following:

This seemed to work fine and printed out the numbers 10 to 19 in the console.

Meanwhile, as I understand it, the For Each Loop is similar, but it instead looks at each individual element one by one.

An older example I could find on this was:


Bringing this into Unity again to test, this seemed to work a treat:

I can see the uses behind these kinds of functions, so I’m looking forward to covering them more in class!

Maths: Dot Products

I don’t particularly want to pretend I understand all about this subject after looking through some resources online – it seems like it’s the most complicated mathematical topic we’ll have covered so far. However, I’ll write out what I know and I hope to bolster that with what we’ll learn next week in class.

According to the glorious Wikipedia page, a Dot Product or Scalar Product is “an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors) and returns a single number”.

Vectors can be multiplied by Dot Products, although to what end I couldn’t really discover. It then returns a number as an answer, which is a scalar, not a vector. To calculate the Dot Product of two vectors, the equation is…

The magnitude of the first vector multiplied by the magnitude of the second vector, multiplied again by the cos(Ø). This picture I found online probably explains that calculation better:


It was interesting to find out that this can work in three dimensions, not just two, although it’s likely for our examination we’ll only need to worry about the 2D calculations in Unity. However, when the two vectors are at right angles to one another, the Dot Product will be 0, which seems to be handy to find out if the two vectors are actually at right angles. Otherwise, from what I’ve read, the Dot Product can only be found if the two vectors are pointing the same way at an angle that’s less than 90º.

This seems to be falling into the realms of Calculus, something I didn’t even do in high school, but I feel like it’s more interesting now than it was back then. That’s probably a good thing, given my interests in programming. I really enjoyed reading through this website during my research, as I feel it helped me understand putting vectors into the equations and understanding the calculations in a game-related context. This has been one of the only times I’ve found the maths we’ve been asked to research explained even slightly using video games, so very good information there, especially considering the heavy subject matter!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s