Loops - Reading Assignment

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

To go back to an earlier point in the program.

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

They simplify the repeating of a procedure. Code is reiterated either for multiple instances or as long as a condition is met.

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

Do-loops are repeated at least once even if the condition fails. Its test appears after the body. While-loops are only repeated when the condition is True and this is tested before the body or at the beginning of the loop.

  1. What is indentation?

The way the programmer structures the code for visual and readability purposes.

1 Like

Hi @Jaysun,

Nice answers :ok_hand:

Yes, that’s essentially correct :+1: …it keeps our code more concise, easier to read, manage, debug and develop further, and using loops means we don’t have to waste our time writing out the same sequence of statements many times over!

That’s correct :+1:
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.

1 Like

Hi @Brett_Schor,

Q1, 2 & 4 :ok_hand:

Yes… and the key here is that while loops start by evaluating the Boolean (conditional) expression 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… in contrast to while loops, 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.

No … a do loop keeps looping until the condition evaluates to false — exactly in the same way as while loops, except that with do loops the condition is evaluated after each loop has finished executing, rather than before.

1 Like

Hi @Alexliljen,

You seem to be a bit confused about loops. Let’s see if we can make things clearer for you :slightly_smiling_face:

No…
Looping control flow allows specific blocks of code to be repeated as many times as necessary. It enables the program to return to a previous point, while, at the same time, maintaining its current state (i.e. it starts the next iteration with the values assigned to its variables in the preceding iteration).

You are right that it means we don’t have to waste our time writing out the same sequence of statements many times over. But it doesn’t necessarily make things any quicker, because a computer would normally perform the same computation just as quickly whether a repeated block of code is written as a loop, or is written out many times over and executed from top to bottom without any iteration.
Instead, the increased efficiency that you mention is due to the fact that loops make our program more succinct, and easier to read, manage, debug and develop further.

No…
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.

Yes :+1: … and more specifically, indentation is where a nested statement not only starts on a new line, but is also preceded by more whitespace than its parent.

I hope that makes things clearer for you. Let us know if anything still doesn’t make sense, or if you have any questions, and we’ll do our best to help.

2 Likes
  1. Looping Control Flow allows to return to a previous point in the Program & repeat it

  2. Loops allow a program to execute by entering a statement, until a condition is met

  3. A ‘While Loop’ will execute as True only if a condition is met, where as a ‘Do Loop’ will execute at least once until a ‘True value’ is found

  4. Indentation (or spacing) is basically used to provide an easy way for a human programmer to read the code. Any spaces used are dis-regarded by a CPU when reading / executing code

1 Like
  1. What is looping control flow allowing us to do?
    It allows us to go back to the start of a program so the program can be repeated.

  2. Describe what “loops” do in your own words.
    Loops continue the iteration of a program as long as the expression continues to produce a true outcome when converted to Boolean.

  3. What is the difference between while and do-loops?
    While loops will loop continuously on a true boolean, and do loops begin testing after the first execution of the program. Do loops always run at least one loop and conduct testing to determine whether or not to end the program.

  4. What is indentation?
    This keeps the structure of your code easy to read. It keeps the spacing between code blocks evenly spaced by using standard spacing on each line of code.

1 Like

Thank you so much! You are correct that i was confused haha :wink:

1 Like

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

Quite close sit, but could you explain the difference? how the while loop function compared to the do-loop

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like
  1. It allows us to go back to a point in the program and repeat it .
  2. Loops repeat the same formula as many times as necessary as long as the values remain true .
  3. A while loops only executed if the value gives true but a do loop automatically executed at least one time .
  4. Indentation Is when the next line in the code is moved a few spaces inward making it a little easier to read .
1 Like
  1. What is looping control flow allowing us to do?
    Looping control flow allows us to go back(loop) to a certain point in a program where we once were and repeat with the current state of conditions. This process will repeat over and over.

  2. Describe what “loops” do in your own words.
    Loops are code that will repeat a process over and over again. This will continue until the loop satisfies a certain determined condition.

  3. What is the difference between while and do-loops?
    A while loop will continue the operation “while” a certain condition exists. When that condition no longer exists or is true the loop ends.
    A do-loop will continue until an action is performed that satisfies the parameter of the program.

  4. The purpose of indenting code is to keep it readable and to provide a structure.

  5. What is indentation?

1 Like
  1. What is looping control flow allowing us to do?
    Looping control flow is conditional execution that allows for a particular part of the program to continue looping back on itself until it achieves a desired result.

  2. Describe what “loops” do in your own words.
    Loops allow us to change the straight forward flow of the ‘story line’, so to speak, into a conditional story line that allows us to continue on with the story, once a particular event has occurred.

  3. What is the difference between while and do-loops?
    While loops are only executed if the parameters have not been met whereas the do loop will execute at least once and then test to see if the desired outcome has been achieved, before continuing to loop if it has not been met.

  4. What is indentation?
    Indentation improves readability. The program does not require indentation or line breaks, but readability is greatly increased for humans and the way we read. It is important to have a consistent structure if you are using indentation, otherwise the readability is not overly improved.

1 Like
  1. Going back to some point in the program previously and repeating a piece of code multiple times in the current program state.
  2. A feature that is used to run a piece of code repeatedly until a certain condition is met.
  3. While loop loops through a piece of code for as long as specified condition is true, do loop will execute the code block once before checking if the condition is true.
  4. A way to make the structure of the code easily readable.
1 Like
  1. What is looping control flow allowing us to do?
    It allows us to repeat the code until set value is reached, for example counting all integers until it reaches 10.
  2. Describe what “loops” do in your own words.
    It allows us to repeat a function or request until desired output is reached. It basically helps with reaching desired goal without having to write multiple strings of code to achieve that.
  3. What is the difference between while and do-loops?
    While loop test once and then stops. Do-loops keep testing until desired action is achieved.
  4. What is indentation?
    It is a way to make the code cleaner to read and understand what is happening and what actions are being asked of the computer. It basically indents the code on the line to make things stand out and easier to read.
1 Like
  1. Looping control permits the repetition of a segment of code if certain conditions are met.

  2. Loops efficiently conserve programming code by permitting the repetition of code under defined circumstances.

.3. The do loop always executes once whereas the while loops execute based on condition.

  1. indentation is an optional structure of code to assist the programmer in writing and others reading a given program
1 Like

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

Quite close sir, you have both loops backward, the do-loop is the one that test once and then stops, the While loop keep testing until desired action is achieved (while this condition not true).

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

  1. What is looping control flow allowing us to do?
    execute the same statement multiple times

  2. Describe what “loops” do in your own words.
    execute the same piece of code multiple times as long as a certain condition is met.

  3. What is the difference between while and do-loops?
    do-loops always execute its body at least once, while-loops don’t.

  4. What is indentation?
    use spaces (or tabs) to make the code more readable.

1 Like
  1. 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. makes a program useful to work with because it repeats a certain function until it reaches a certain threshold predefined by the programmer. Instead of writing 100s of code lines a loop checks a certain status until it has reached that certain status.
  3. a do loop will ask again and again and again until it got what it wants. It is not limited to a certain number of loops like a while.
  4. Gives the code the structure so every developer can fast read your code.
1 Like
  1. Program loops facilitate the repeated execution of certain blocks of code and thereby greatly increase programming efficiency. Instead of writing one block of code for each iteration, a programmer who uses a looping control flow needs to write only one block and specify only one iteration-ending break condition.
  2. Loops repeatedly execute a given block of code until a certain break condition is satisfied.
  3. In a while-loop, the break condition is tested before each execution of the block of code that the loop encloses, and in a do-loop it is tested after each execution of that block.
  4. Indentations are used to make visually apparent the logical nestedness of program code. In other words and for example,.by means of indentation a program can be written in such a way that loops within loops or conditions within conditions become easily identifiable.
1 Like

1. Looping control flow allows us to go back to some point in the program and repeat a snippet of code until an exit condition is met. I actually used this in my first program where i solved “multiples of three and five” in project euler.

var number = 10
var result = 0;
var count = 0;
var sum = 0;
for (let count = 1; count <= number; count = count + 1) {
if (count % 3 == 0 || count % 5 == 0) {
if (count < number) {
var result = + count
var sum = sum + result
}
}
}

(I copy pasted my code here, its supposed to be properly indented but it wouldn’t work)

So basically, i use the for keyword to continiously add 1 to “count” as long as count is less then or equal to the number that you give to the program. It then proceeds to test if the value that count has is evenly divisible by 3 or 5 with the modulus operator, if this gets converted to boolean true, the value is then tested if it is less than number given to the program in the beginning. If this also is true, the value held by “count” is then stored in the variable “result”. Then “result” is then added to variable “sum”. This behavior loops until all numbers from 1 up until the number you specify have been tested. Once the entire program has run, “The sum of all the multiples of three and five” between 0 and the number the user has given to the program is now stored in the variable “sum”.

I initially had console.log(sum) at the end of the program but i decided to try to integrate the code in a html file and to try to make it into a fully interactive webpage but i haven’t figured out how to do that yet…

2. I think i described “what loops do” in my answer to the first question. But i would say it repeats a block of code until an exit condition is met.

3. Using the while-function creates a loop that runs a statement that follows it long as the expression that immediately follows the “while”-keyword gets converted to true.

let number = 0;
while (number <= 12) {
console.log(number);
number = number + 2;
}
// → 0
// → 2
// … etcetera

The do-loop on the other hand, always runs its code block at least once, and only starts testing whether it should stop after that first execution.

let yourName;
do {
yourName = prompt(“Who are you?”);
} while (!yourName);
console.log(yourName);

This do loop forces the user to enter a name and will ask again and again until the user has provided something that is not an empty string.

Both examples in this answer is copy-pasted from eloquent javascript.

4. Indentation is adding spaces in a way that makes a program stand out visually in a way that corresponds to the shape of the code blocks inside it. This is to make life easier for the programmer(s) working on the code, because otherwise it can be hard to clearly see where one block of code ends and another begins, as well as where code blocks inside of other code blocks begin and ends.

1 Like
  1. What is looping control flow allowing us to do?
    To repeat a piece of code until a condition is met. It includes: if, else and switch; as well as: while, do, and for

  2. Describe what “loops” do in your own words.
    Loops repeatedly execute an operation until it is stopped either by

  • a Boolean that equals “false”
  • by the command “break”
  1. What is the difference between while and do-loops?
  • a while-loop reiterates until the test returns false. The test is placed before the body
  • a do-loop executes at least once and test only after this first execution to decide if it shall continue or not
  1. What is indentation?
  • it is used to better visually recognize the code structure
1 Like