Weekly questions to consider when creating your posts. These questions are a jumping off point to asking your own questions, pose new ideas, and provide explanations and examples backed by careful analysis. Apply class concepts, terms, and theories too. Refer to Assignment 12 for additional details.
- Why do we use loops?
- What loop do we use when we want the code to run a specific number of times?
- What loop do we use when we don’t know the specific number of times? (consider the difference between a while loop and a for loop?)
- What are the three parts of a for loop?
Sophie Chen says
We use loops when we want code to run more than one time. For loops run for a certain number of times specified at the beginning. While loops run for as long as a condition is met (not a specific number). For example, “for” loops are great for running through a list of a certain number of objects, while “while” loops are great for performing a task while a certain condition is true. When we create a for loop, it must have three parts: the keyword that starts the loop, the condition being tested, and the end keyword that terminates the loop. An example of the syntax for a “for” loop would be:
for (let days = 1; days < 8; days++){
console.log(“Today is the “ + days + “ day”);
}
We are starting at days = 1 and printing a statement for each day. After the statement is printed, the number of days is incremented and the loop starts over. When we reach days = 8, the for loop stops and the condition is not executed since it only executes when days is less than 8. In total, the loop will run 7 times. We separate the variable ‘days’ with a “+” sign since we want it to print the value of the variable, not the word “days”. Anything printed between quotation marks will be printed as a statement and not a variable.
Jinzhou Situ says
Hi, Sophie,
I think your explanation of the “for” loop is very detailed, which makes me understand the specific principle of it and how it can be used in the program. This is a very useful programming knowledge.
Julia Grugan says
Hi Sophie, thanks for your response! I really appreciated your example of the for loop syntax. Breaking down the three parts and really understanding those was crucial for me in understanding how to construct these loops myself. It’s less intuitive when you’re assigning a variable like i to run through the code as opposed to a logical variable like days in your example. I liked how your example was common sense, like a loop running through 7 days in a week.
Justin Bonfiglio says
Hey Sophie,
I loved the way that you went so in depth for all of your responses. Having it be this heavily detailed really helps to understand the little parts that are usually more confusing that just giving an answer alone can’t help you with. Also I was wondering if you or anyone else knows if there is a different way or term to use instead of console.log that would work the same in the spot?
Assia Snineh says
Hey Sophie,
You explained loops very well and used great examples. This is very useful for anyone who doesn’t know about loops. Good job!
Cassidy Mantle says
Hi Sophie,
I enjoyed reading your response as it was extremely insightful on the usage of Loops. This definitely is an accurate representation and enhances one’s understanding of Loops.
Anthony Marquis says
Hi Sophie, I thought you described the coding parts in great detail and you shows how a for loop really works.
Jinzhou Situ says
In writing code, using loops can shorten hundreds of lines of code to a few lines to run. We use Nested loops when we want the code to run a specific number of times. We use “While” loops when we don’t know the exact number of times. The difference between a while loop and a for loop is that a for loop is used for a program with a known number of runs, while a while loop is used for a program with an infinite number of runs. The three parts of the for loop are initialization, condition and iteration.
Julia Grugan says
Hi Jinzhou, thanks for your response! What is a nested loop? Is a for loop a nested loop? Are there other nested loops that I don’t know about? It seems like you have a lot of knowledge on this subject, as your use of coding jargon is very apt.
Sophie Chen says
Hi Jinzhou,
I like how you mentioned nested loops, that one loop can run inside another loop. I think that is very useful when you want to iterate through a set of data in a more detailed way, or to respectively check something. I think it’s worth mentioning that too many loops can slow down a program’s run time, not that it’s noticeable to us as humans but the processor runs slower on its super fast level.
Assia Snineh says
Hey Jinzhou,
You explained nest loops very well, and explained the difference very well. This is very resourceful knowledge!!
Ryan Scharko says
Hi Jinzhou, I thought your explanation on loops and simplifying how they are used and what they entail for a function was spot on. Do you think you could give an example of a while loop?
Justin Bonfiglio says
We use loops to allow for less coding and repeating of the same lines over and over and being redundant, to help speed up the processing of the code, and most importantly to not piss off anyone else in that’s working on the same code lol. The specific loops that we use when we know the number of times we want it looped would be the for loop, and the other loop used for situations that we do not know the number of attempts we want would be the while loop. The main parts of the previously mention for loop would be Initialization, condition, and iteration. This is basically saying the loop starts, tests the “condition” or what you want it to test, and it ends the loop and gives you an answer.
Montajim Khan says
Hi Justin,
I like how you first begin with loops allowing us to code less to speed up the process during coding in Javascript. Also, when mentioning the “main parts” of for loops, “basically saying the loop starts, tests the “condition” or what you want it to test, and it ends the loop and gives you an answer.” It helped me understand what the textbook definition said when it says “initialization, condition, and iteration.”
Yi-Lun Ma says
Hi Justin,
you explain very easily and explain the way to use the loops. This is a useful explanation for me
Madeline Elia says
Hi Justin,
I liked your explanation of loops. They save lots of time and definitely lessen the chances of laptops being thrown out of windows, lol. The main parts of the loops (initialization, condition, and iteration) are important because they give the loop guidelines as far as the starting point, condition, and step.
Montajim Khan says
We use loops “sequence of instruction that is repeated until a certain condition is reached.” There are three types of loops you might need during coding. There is “for loop,” “while loop,” and “do-while loop.” Loops are common to run codes. It repeats until a condition evaluates to false. While loop executes its statement as long as the condition is true. Lastly, a do-while-loop is a statement repeated until the condition is false, meaning it stops execution and is passed to the do-while statement. I believe we use it for loop when we want to code a specific number of times because it’s always executed by initialization, expression, and increment statements. While loop is when you don’t know the specific number of times it executes as long as the expression is true.
Cassidy Mantle says
Hi Montajim,
I enjoyed reading your response. I agree with your interpretation of the usage of loops and its purpose. I definitely wrote something similar in a sense where Loops are used to determine an expression as long as its true and the specific number of executions are unknown.
Assia Snineh says
When programmers write code, loops allow them to condense hundreds of lines of code into a few. This allows them to write the code once and repeat it as many times as necessary, increasing the likelihood that the program will run as expected. When you have a block of code you want to run x number of times, then a block of code within that code which you want to run y number of times, you use what is known as a “nested loop”. A while loop is an iteration method that is best used when the number of iterations is unknown ahead of time. The loop’s contents are executed as long as the expression evaluates to true. A For loop, like a While loop, is made up of three parts: the keyword For, which initiates the loop, the condition being tested, and the EndFor keyword, which terminates the loop.
Robert DiBrino says
Hi Assia! Your explanation of loops was great. Having programmers use loops is a fantastic way to be more efficient and likely saves a significant amount of time. Additionally as you noted, once the code is written correctly and looped, it cuts down the potential for errors within the code. As someone who has made mistakes and had to fix them within other assignments, I can imagine that would be the largest benefit of loops even over efficiency!
Nicholas DiGiuseppe says
Hi Assia,
Your post contained a very precise explanation of how to use loops in JavaScript. I can definitely say that using loops is a huge time saver and I’d also like to add that having the diagrams drawn out in front of me really helped see the different functions of the while loop vs the do-while loop.
Yi-Lun Ma says
In programming, loops are used to repeatedly carry out a series of instructions up until a predetermined condition is satisfied. Loops enable us to avoid writing repeated code and improve the flexibility and efficiency of our applications.
The “for” loop, the “while” loop, and the “do-while” loop are the three most popular types of loops used in programming. Although each sort of loop has unique benefits and applications, they all have the same fundamental function of enabling us to repeat a piece of code repeatedly.
A “for” loop is often used when we want the code to execute a certain number of times. An initialization statement, a condition, and an update statement are the three primary parts of a “for” loop.
Madeline Elia says
Loops are a great tool because they’ll repeat a certain chunk of code without us having to retype it a bunch of times. Overall, it saves a lot of time and screen space. There are three types of loops: the for, while, and do-while loops. For every kind of loop, we have to establish what code we want to repeat (which is the loop) and then create a stopping point for when a certain condition is met. The for loop is commonly used and will keep going until the loop comes back as false. The while loop repeats until the looping condition comes back as false. A do while loop doesn’t execute unless the first set of conditions aren’t met from the start. Going back to the for loop, there are a few important things it needs to function. First, it needs a starting point. This is normally where the code is declared and initialized. Next, it needs a condition. This is basically what tells the loop when it’s going to stop. Finally, the loop needs to have a step. This determines what will happen to the starting point. For example, if it increases by 1, we type in i++. A for loop with all of its key components would look something like this:
for (let i = 0; i < 10; i++) {
saySomething();
}
function saySomething () {
document.writeIn(“hello!”);
}
Aidan Morgan says
Hi Madeline, I completely agree that loops are a great tool! Loops make the hassle of re-typing code almost non-existent as the loop will run until a certain condition is met. I also found your example and explanation of loops to be great and they described what loops were and how they are utilized well.
Isel Sainte says
We use loops to not have a bunch of repetitive lines in the codes. The loops are to better organize the code. The code is repeated in a neater way. There are 3 types of loops. For loop, while loop, and do-while loop. I believe that the for loop is the one, we would use when we run a specific number of times because it has a condition, and when it is reached it displays false and stops. I believe that the while loop is the one, we would use when we don’t know the specific number of times, so it continues to loop until it displays a false. In class, you described the three parts of a for loop as:
for (initial-action; loop-continuation-condition; action-after-each-iteration)
{
// loop body;
Statement(s);
}
Aidan Morgan says
If you were writing code and wanted to repeat a certain code without writing it out over and over again, what would help you with that? This is where the loop comes in. To simple it down, a loop is just a set of instructions repeating until a certain condition is reached. Loops are great in executing code over and over and saving time for the coder that now does not have to rewrite the code. There are multiple different versions of the loop, there is the for loop, while loop, and the do while loop. A for loop consists of 3 parts, the keyword starting the loop, the condition being tested, and the end keyword terminating the loop. When an user wants to run a loop that will continue to operate until a condition is true, a while loop would be perfect. A do while loop is similar to a while loop, instead here a condition will run first, then the condition will be checked if true. A while loop will always check if the condition is true before running but a do while loop will run the condition the first time, then it will check if the condition is true, Loops all around are a great way for programmers to save time and make their code look cleaner and run more efficiently.
Sheree Owens says
Hi Aidan! I think this was a great explanation of loops. I agree that loops make programmers save time, look cleaner and it is used to make their codes run more efficiently. Without the loop structure in a computer program, the tasks are almost impossible to be performed. Loops, on the one hand, execute the tasks faster saving time and energy, on the other hand, they are very helpful to accomplish tasks in an accurate manner.
Shivam Joshi says
Why do we use loops?
Loops are used in programming to execute a set of instructions repeatedly. They help automate repetitive tasks and simplify code, making it more concise and easier to read. Using loops can also help reduce the amount of code needed to achieve a specific task.
What loop do we use when we want the code to run a specific number of times?
We use a for loop when we want the code to run a specific number of times. For example, if we want to print the numbers 1 to
10, we can use a for loop that iterates from 1 to 10.
What loop do we use when we don’t know the specific number of times? (consider the difference between a while loop and a for loop?)
We use a while loop when we don’t know the specific number of times we need to run the code. A while loop will continue to execute the code block as long as the specified condition is true. In contrast, a for loop is used when we know the number of iterations required, and it is more suitable when we need to execute code for a specific number of times.
What are the three parts of a for loop?
The three parts of a for loop are:
Initialization: This is where we initialize the loop variable to a specific value, typically the starting value.
Condition: This is the condition that is checked at the beginning of each iteration. If the condition is true, the loop continues to execute. If the condition is false, the loop stops.
Increment/Decrement: This is the statement that is executed at the end of each iteration. It updates the loop variable so that the condition is eventually false, and the loop stops.
Sheree Owens says
JavaScript includes for loop like Java or C#. Use for loop to execute code repeatedly.
The for loop requires following three parts.
Initializer: Initialize a counter variable to start with
Condition: specify a condition that must evaluate to true for next iteration
Iteration: increase or decrease counter
Example
for(int i = 0; i This part of the for loop is known as initialization. Here we initialize the loop variable which will iterate through the loop.
PART 2 :- i This part is known as condition or you can say comparison, as it suits but condition is the word which suits here better. This part will set certain condition for loop variable regarding how much time it should iterate.
PART 3 :- i++ ==> This is increment / decrement part. Here the value of loop variable is either incremented or decremented. The value change is done after execution of certain statements in loop. The value keeps changing until the condition is fulfilled.
Sheree Owens says
For example^^
for(int i = 0; i<= 5; i++)
Nicholas DiGiuseppe says
No one ever wants to have to deal with repeating many, many lines of code over and over again which is why using loops is a very satisfying tool to have in your JavaScript toolbox. There are ‘while’ loops, which are used for an infinite number of runs within a program, and ‘for’ loops which are used for a known number of runs. The ‘for’ loops also contains three parts: Iteration, Initialization, and Condition. To put these terms in an easier way, the loop begins and then tests the ‘condition’ and it will eventually end and award you with a solution. There is also “do-while loop” which looks like: Start —> Statement —->Conditional Check and then if it’s true then we loop back to the Statement box and continue repeating in the cycle until the conditional check finally comes back as false, and that would be the end. An easy way to remember the difference between while and do-while is that the Statement and Conditional Check boxes are switched around.
Lamine Karamoko says
Loops are generally used as the key fundamentals for allowing a code to repeat. These loops can happen so many times until it is told to stop. Java script has many different types of loops but I can name a few. First, you have a “for” loop which comes in 3 sections such as initialization, condition and increment. Initialization is the part where you put your counter variable. The condition section is the part where you define the conditions you are putting for your variable. Lastly, the increment is used for the loops updates. In other words, you can use increment for updating the counter variable towards the end of each loop that occurs. Another loop is the “while” loop. This loop happens to be simpler than the “for” loop is. For this, you would need to put the condition of the variable. This is the same if either it is true or false.
Ishaan Joshi says
Hello Lamine, good job breaking down loops. I think loops are very convenient in being able to make things convenient and organized. It’s interesting to see the many different contexts in which “for” and “while” can be used and can come in very handy when trying to process different types of information.
Ishaan Joshi says
Loops are used as a shortcut to use a block of code repeatedly and are beneficial for repetitive data coding. “For” and “while” are the two types of loops, “for” is a loop that is used for a specific set of data, while “while” is used to generate code continuously until all conditions of a function are met. For this reason, “for” is the type of loop that would be used when dealing with a specific number of times. On the other hand, when there isn’t a particular set of times, we use the “while” loop so that this code can run continuously. Examples of the “for” loop can include calculating taxes on a specific income set. The “while” loop can be a homework assignment with unlimited attempts that keeps running until the answer is answered correctly. The three main parts of a for loop are initialization, condition, and iteration.
Anthony Marquis says
We use loops in order to make computers more interesting machines and with that said, without loops, computers would be way more complicated without adding these machines. Loops are the most powerful programming concept in order to help a computer program repeats until a specified condition is reached.
The function in order to run a specific number:
def length(self):
if self.value == None:
return 0
else:
return 1 + self.next.length()
The code we use if we don’t know the specific number of time:
@_current_user ||= {SOMETHING}
The three parts of a for loop include, a header, a body and a loop variable in order to show the body in order to know which iteration is being executed.
Preston Cain says
In Programming loops are very useful a loop can be used to tell the program itself that user wants to run the code more than once. This allows us as users to shorten code that could take days to write into a much easier process of a few lines. When the user wants to let the program know the exact amount of times the code should be ran for then, we use For Loops. Loops are so useful in programming because the loop can be used to tell you how many times it takes to reach a certain number or solution. For example a loop can tell you how many times it takes for a small number to go into a larger number by adding that number by itself over and over. Such as 2+2+2 and so on.