Questions tagged [loops]

A loop is a sequence of statements which is specified once but which may be carried out several times in succession.

Taken from Wikipedia:

Most programming languages have constructions for repeating a loop a certain number of times. Note that if N is less than 1 in these examples then the language may specify that the body is skipped completely, or that the body is executed just once with N = 1. In most cases counting can go downwards instead of upwards and step sizes other than 1 can be used.

154 questions
15
votes
7 answers

Should <= and >= be avoided when using integers, such as in a For loop?

I have explained to my students that equal-to testing is not reliable for float variables, but is fine for integers. The textbook I am using said that it is easier to read > and < than >= and <=. I agree to some extent, but in a For loop? Isn't it…
user186205
8
votes
7 answers

provability of while loop vs for loop

I have this teacher, he's quite smart (sometimes, haha) he said good programmers try to use while loops instead of for loops. the reason he gave for this is because while loops can be proven, as in, one can completely explain what happens in a while…
Vincent
  • 375
5
votes
2 answers

What are the differences between a while loop and a for loop?

What are the differences between a while loop and a for loop? It seems to me that they are the same.
3
votes
2 answers

While and do-while loop in the white box method of loop testing

I'm trying to understand how this method works and everywhere I check it I find there's something faulty or maybe it's that I'm not understanding something from the method. Here there's an explanation of the method for reference. Some things of the…
1
vote
1 answer

Efficient way to skip ticks/frames in a loop?

So: main does a loop, and every iteration it increments a tick by one For example, say it were in python because that's easy to write: def main(): tick = 0 while True: tick += 1 I want to trigger many different events every n-in-x…
1
vote
1 answer

Most efficient way to calculate number of iterations to run

Let's say I have a batch process that executes 4 times per loop, and needs to execute a total of 9 items. Example: Iteration 1 executes 4 items of 9, leaving 5 left Iteration 2 executes 4 times of 9, leaving 1 left Iteration 3 only executes 1 item…
Super1337
  • 113
1
vote
4 answers

How to prove that this while loop calculates n^2

I'm studying for my exam of logic later this week and I have to prove that this while loop: i := 0 s := 0 while i < n do i := i + 1 s := s + (2*i -1) calculates n^2. This question is made up of 2 subquestions. We have to prove that 0 ≤ i ≤…
0
votes
1 answer

Explicit expression of a counter

Consider the following pseudo-code: cont = 0 for g = 1,...,m for h = g,...,m cont = cont + 1 end for end for I'm searching for the explicit map that returns cont in function of g and h. I've tried with cont = m*(g -…
Gost91
  • 111
0
votes
2 answers

Main Loops and listeners in live coding systems

My question is about the construction of main loops running in the background listening to commands and signals and how are they constructed to be efficient. For instance in live music synthesis programming languages like SuperCollider or PureData,…
user230703
-3
votes
3 answers

Real life scenario of why we would use loops

I am going to school to become a programmer, and I am doing some homework and I have a question. It wants me to give an example of why we would use loops in the real world. And I was thinking about smart phones, I have an app on here that tells me…
-4
votes
4 answers

Why are for loops needed?

It seems to me that a while loop with the appropriate break statements can replace them entirely. I understand that a break statement may not feel as "smooth" as a for loop that does something a specified number of times, but I also feel that the…