Loops - Reading Assignment

Hi @Matgas,

Q1, 2 & 4 :ok_hand:

No…both types of loop are limited to the number of iterations they can perform by their conditions. The difference between them is when their conditions are evaluated and how this effects when each loop can be exited:
While loops start by evaluating the condition before executing their code block for the first time. This means that, if the condition is not met from the outset, the loop’s code block will be omitted completely.
In contrast, the conditional expression in do loops is placed after the code block. This means that the code block will always be executed at least once before the loop is exited.

I hope that makes the difference clear.

Keep on learning! :muscle:

1. A looping control flow allows us to go back to some point in the program where we were before and repeat it with our current program state.

2. “Loops” will keep entering that statement as long as the expression keeps producing a value that gives true in the boolean condition. When the expression produces a false value the loop stops or alternatively until there is a break in the statement that is executed.

3. The difference between while and do-loops is that the do-loops always executes its body at least once then it starts testing whether it should stop after that first execution. The while keyword only executes if the boolean value is true.

4. An indentation is the space in front of the statements although it is optional and not necessarily required. It has an important role, the indentation inside the blocks is to make the structure of the code stand out. (Luckily for us most code editing programs do it for us :stuck_out_tongue:)

1 Like

Hi @Palmeguppsi,

Nice answers :+1:

I’ve had a look at your short program Multiples of Three and Five. This is a good start :ok_hand: but there is quite a bit of superflous code that you can cut out to make it more succinct:

  • You define count as your iterator variable in your for loop header. So there’s no need to also declare  var count = 0  with the other variables at the beginning, outside of the loop.
  • You can also completely remove the variable result because it doesn’t add anything. Instead, you can just add count directly to sum whenever the if statement condition is met.
  • In addition, you have already declared the variable sum before the for loop, so you don’t need to keep declaring it during iteration, only reassign it by adding count where necessary.
  • As you already have a condition based on the value of count in your for loop header, you don’t need the second if statement. In fact, at the moment, the second if statement condition is more restrictive than your for loop condition, because it excludes the last number in your range (represented by number). Is that what you intended? If so, then it would be more logical to change the condition in your for loop header from
count <= number;
// to
count < number

// and still remove:
// if(count < number) {...}

So, essentially, what I’m saying is you just want one of the above two conditions (with either <= or <) and only in your for loop header.

Here is my suggested version after making the above modifications:

var number = 10;
var sum = 0;
for (let count = 1; count <= number; count = count + 1) {
   if (count % 3 == 0 || count % 5 == 0) {
      sum = sum + count;     // 3 + 5 + 6 + 9 + 10
   }
}
console.log(sum);     // logs 33 to the console

What do you think?

Take a look at this post which describes how to correctly format the code you are posting here in the forum.

1 Like

Hi @yestome,

Nice answers :ok_hand:

Just one comment…

Yes :+1:

No… like loops, these are types of conditional execution, but unlike loops, they do not involve iteration. Instead they involve “branching” of the control flow.

Yes :+1:

I hope that’s now clearer.

1 Like

Understood - and thanks for explaining this !

1 Like
  1. Looping Control Flow allows the program to ‘go back to a previous point . . and repeat it with the current program state’.

  2. Loops allow pieces of code to be run repeatedly, producing values, and this can be constrained by certain conditions being met. This allows for a huge number of possibilities in the program, when combined with conditional execution, binding and other processes like the Boolean functions.

  3. A ‘ do-loop ’ executes itself, and only then looks for the conditional input as to whether it should execute again. A ‘ while loop ’ will look for a conditional ‘go ahead’ from the very first instance.

  4. Indentation is formatting the javaScript in a script editor in a certain way that is easier to read. It makes no difference to the way the code is processed, but is easier to see what’s what.

1 Like
  1. What is looping control flow allowing us to do?
    allow us to run the program multiple times.

  2. Describe what “loops” do in your own words.
    Execute the same function/command multiple times when the condition meets certain criteria;

  3. What is the difference between while and do-loops?
    Do-loops always execute its body at least once, and they start testing whether they should stop only after that first execution.

  4. What is indentation?
    Indentation is to add spaces in front of statements that are part of some larger statement.

1 Like
  1. What is looping control flow allowing us to do?
    It allows us to go back to some point in the program where we were before and repeat it with our current program state.
  2. Describe what “loops” do in your own words.
    It enable the program to do repeating tasks until condition is met.
  3. What is the difference between while and do-loops?
    A while loop will run indefinitely until the condition is changed.The do- loops execute its body at least once and continue to check if condition is met.
  4. What is indentation?
    It’s a way to organized the code structure for better reading.
1 Like

1. What is looping control flow allowing us to do?

Looping control flow allows us to go back to a previous point in a program, and repeat that within our current program state.

2. Describe what “loops” do in your own words.

Loops repeat certain functions within the program until we’ve achieved a desired outcome, or indefinitely. That way you can have the program do the work for you, rather than the other way around. For example, using loops to instruct a program to add from 1+2+3… to 10000 is infinitely more efficient than writing out each individual number yourself.

3. What is the difference between while and do-loops?

(Correct me if I’m wrong) From my understanding, the main difference between a while and do loop is that a while loop will check the condition before executing the loop, but a do loop will verify the condition after the execution of the loop

4. What is indentation?

Indentation is simply the space at the beginning of each line of code. It’s important to use consistent indentation in your programs to make them easy to read and understand.

2 Likes

Yes, that’s a chapter lag. Dealing html in JS or Statements in Loops. Nothing controversial, yet

Loops - Reading Assignment.

What is looping control flow allowing us to do?

  1. When you need to run a programme or piece of code x100 or x1000, for a developer to save time you can create what is called a Looping Control flow. What this does is allow us to repeat lines of code or programme by using either a while keyword or do keyword.

Describe what “loops” do in your own words.

  1. Okay, a loop in a line of code that can be used in several ways. You can use a loop function for example if your doing complex task that require a lot of processing, where the computer would be better to equip to solve it and quickly. Like maths problems. You can also use loops with .html web page but creating a loop where every time they click a particular header or tag they would be redirected to the start. These examples are simple but to allow the programme to run a loop means that there is high complexity issues it can solve and handle.

What is the difference between while and do-loops?

  1. A do loop is something that requires an action to happen form the programme. When a do loop function is applied it kinda like the name its given it requires something to “do” meaning if a value or string is not returned then the action will keeping being asked until something is returned. An empty string can not be returned empty otherwise the do loop will keep invoking.

What is indentation?

  1. When writing code you can be writing complex lines that may contain brackets within brackets. It can be sometime difficult to tell apart were lines of code start and end. So making things more helpful you can create space to separate some lines of code, some software can take one step future and auto fill and start new lines or create space for you. JavaScript can read your programme with space or without it.
1 Like

It Allows to go back to some point in the program where it were before and repeat it with the current program state.

Loops are useful if you want to run the same code over and over again, without the need to have to write the same code many times and so waste time.

while loops = it loops through a block of code while a specified condition is true
do-loops= it loops through a block of code once, and then repeats the loop while a specified condition is true

The role of the indentation is to make more clear-readable the code, technically the code could be written in a single line but would not be good for a programmer to work on that kind of code.
It consist in adding a space in each new block.

1 Like

Nice answers @Changfa_Lin :ok_hand:

Just one comment…

… not the whole program!.. but a specific part of the program.

1 Like

Good answers @ArtCenter1 :ok_hand:

Just one comment…

Whether it’s a while or a do-loop, the loop’s condition isn’t changed — but there comes a point where it no longer evaluates to true, and at this point the loop is exited. This is the same for both while and do-loops. The difference between them is where the condition is located, and so when it is evaluated.

While loops start by evaluating the condition before executing their code block for the first time. This means that, if the condition is not met from the outset, the loop’s code block will be omitted completely.

Yes, this is correct :+1:
A do loop’s condition is placed after the code block. This means that the code block will always be executed at least once before the loop is exited. But a while loop will also continue to check if its condition is met, but before each loop, rather than afterwards.

I hope that clears up any confusion.

1 Like

Nice answers @Bunbo_Hue :ok_hand:

Yes, that’s correct :+1:
…and because of this difference in when the condition is checked, if a while loop’s condition is not met from the outset, the loop’s code block will be omitted completely. Whereas a do-loop’s code block will always be executed at least once before the loop is exited.

2 Likes

Thank you for making things even clearer.

1 Like
  1. What is looping control flow allowing us to do?
  • It is allowing us to go back on ourselves as much as many times as needed as a more efficient method for creating similar code as opposed to repeatedly typing the same code.
  1. Describe what “loops” do in your own words.
  • Loops allow code to be repeated until a confition is met
  1. What is the difference between while and do-loops?
  • a do-loop will run at least once while a while-loop may not run at all
  1. What is indentation?
  • It is a method writing in which blocks of code will stand out in order to make the code easily for humans to read.
1 Like
  1. What is looping control flow allowing us to do?
    Looping control flow allows us to go back to some point in the program where
    we were before and repeat it with our current program state.
  2. Describe what “loops” do in your own words.
    A loop allows to run a piece of code multiple times
  3. What is the difference between while and do-loops?
    The word while is followed by an expression in parentheses and then a statement, much like if. The loop keeps entering that statement as long as the expression produces a
    value that gives true when converted to Boolean.
    A do loop differs only on one point: a do loop always executes its body at least once, and it starts testing whether it should stop only after that first execution. To reflect this, the test appears after the body of the loop.
  4. What is indentation?
    The role of indentation inside blocks is to make the structure of the code stand out.
1 Like

Not sure what you mean here… the use of the terms expression and statements in @javedkhalil’s diagram of a while loop is perfectly fine.

While it is also correct that you could use the terms condition and body instead, I don’t agree that it is more accurate, although perhaps the term condition may be clearer in terms of what that particular expression does.

Is that what you mean?

2 Likes

Hi @Hamze_Dirir,

Q1 & 4 :ok_hand:

Question 2

…you’ve missed out one of the key aspects of loops: the fact that they contain a condition which is evaluated on each iteration. If the condition evaluates to true, then another loop is performed. If it evaluates to false, then the loop is exited. Loops also contain an iterator variable, which acts as a counter for the number of times the loop’s code block has been repeated. The condition (evaluated on each iteration) will involve comparing this counter with a certain threshold or limit representing the point at which the loop should be exited.

This example doesn’t involve a loop. It doesn’t require a repetition of a code block to achieve the output, but instead is a single instruction for the browser to reload the home page or the top of the current page, for example.

Question 3

No… you are confused about this. The difference between the two types of loops is as follows:

While loops start by evaluating the condition before executing their code block for the first time. This means that, if the condition is not met from the outset, the loop’s code block will be omitted completely.
In contrast, the condition in do loops is placed after the code block. This means that the code block will always be executed at least once before the loop is exited.

2 Likes