Caution Repetitive

Caution Repetitive

OMG! Omg, omg, omg. NOW things have gotten exciting.

'Caution Repetitive' by Princess Chelsea is probably one of my favourite songs of all time in the world

It is probably the most healing, empowering song I know. There is all of this deep, deep power and deep, deep bitterness contained within it. I love it so much - it is amaaaaaaaazing - oh my godddd. It was my favourite song back when I was studying in Liverpool, and it helped me to get through a LOT of things.

So how perfect that it should come again - and help me again - to the rescue, when I am struggling with loops!

Because... loops are all about REPETITIONS! Loops are about... REPEATS! 

And in the song... the themes repeat, the melodies repeat, the lyrics repeat.. and yet they don't. They play on each other. They iterate... they loop. 

So, with renewed hope now, let me look at loops... AGAIN.

Loops

Direct from Codecademy:

"A loop is a programming tool that repeats a set of instructions until a specified condition, called a stopping condition is reached."


"As illustrated in the diagram, loops iterate or repeat an action until a specific condition is met. When the condition is met, the loop stops and the computer moves on to the next part of the program."

Repeating Tasks Manually

The course got me to console.log() lots of boring tasks manually to get an appreciation for the value of loops. Lol - it worked, lol. That was very boring.

The for loop 

OH NO. These are surely the bane of my existence. 

Just kidding, they aren't so bad really. Omg nooooooooooooooooooooooo...

The for loop is driving me insane

I'm running back to the safety of the arms of my hero, the MDN

"The for statement creates a loop that consists of THREE OPTIONAL EXPRESSIONS, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop. 

Syntax

for (initialization; condition; afterthought) {
    statement
}

Note the use of the semi-colons 

Okay so wait. What does each of these parameters do?

initialization

"An expression or variable declaration evaluated once, before the loop begins". 

condition

"An expressions to be EVALUATED before EACH LOOP ITERATION.

If the expression evaluates to true, statement is executed." 

afterthought

"An expression to be evaluated at the end of each loop iteration. This occurs before the next value of condition. Generally used to update or increment the counter variable".

statement

"A statement that is executed as long as the condition evaluates to true".

I dropped everything and went to my JavaScript book

Remember that book I bought last month? 

I was showing it to my colleague and I opened the book on a random page to show him AND CRAZILY IT WAS A LOOPS PAGE. That was the only sign I ever needed from the Universe.

Here is what the book has to say about for loops: 

They are "one of the most common type of loops". 

for (initialization; condition; update) {
  //do something
}

Better than the MDN, imo. 

"Three things get set inside the parentheses:"

  • The initialization code is run before the loop starts and is usually employed to initialize any variables that are used in the loop."
Great. I'm already lost. Maybe let me read up a bit more on loops first in general in this book.

But actually. 

This next bit looks clearer: "The condition has to be satisfied for the loop to continue". 

And then: "The update code is what to do after each iteration of the loop, and it is typically used to update any values". Huh? Eh? like i++ ? Or whatever... 

What the books just generally says about loops

It says two bullet points:
  • "Loops are a way of getting a program to repeat a piece of code according to certain conditions".
And:
  • "A loop is a piece of code that will continue to run until a certain condition is met".
Kind of useful.

Just generally drilling for loops into my head

initialization; condition; update

initialization; condition; update

The example from my course: 

for (let counter = 5counter < 11counter++) {
  console.log(counter);
}

Notice the use of the variable counter three times.

The counter variable gets used three times...

I should probably be practising for loops just a little bit...

... But I don't even know how to begin to practise them.

And on Monday it's on to... Looping in reverse! But I will probably watch a video on for loops over the weekend. Or maybe two...


Comments

  1. Looping is indeed very common however depending on the preference of the codebase you might not ever see for loops. The concept of looping is good to know but for is not the only way to loop/iterate.

    ReplyDelete
    Replies
    1. Thank you Adam! Wow how lovely to have you comment on here again! Never fear - still at least six or seven other types of loops to cover in the course. Thank you!!!

      Delete

Post a Comment

Popular posts from this blog

Hello World

Yosemite

Where To Hide A Star