Skip to main content

Questions tagged [loops]

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

17 votes
6 answers
8k views

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

This is the most popular way (it seems to me) of checking if a value is in an array: for (int x : array) { if (x == value) return true; } return false; However, in a book I’ve ...
Danila Piatov's user avatar
0 votes
3 answers
300 views

Start and exit actions in while loops

Sometimes I find it useful to have loops that do an action at the beginning and/or the end of a while loop. The best way I can think of for representing this in C++ would be: if(condition) { // ...
Graham's user avatar
  • 105
13 votes
7 answers
12k views

Best practice to "continue" from inside a nested loop?

Here is a simplified sample. Basically, it does checks on a string from a string list. If the check passes, it will remove that string (filterStringOut(i);), and it is no longer ...
Anon's user avatar
  • 3,613
48 votes
16 answers
16k views

Inside a for-loop, should I move the break condition into the condition field if possible? [closed]

Sometimes I need for loops which needs a break like this: for(int i=0;i<array.length;i++){ //some other code if(condition){ break; } } I feel uncomfortable with writing if(...
ocomfd's user avatar
  • 5,712
-1 votes
2 answers
720 views

How do i create a logic for dynamic product list page like below using php and bootstrap?

The big image is catalog or ad blocks that comes from catalog table. And small images are the actual products block that comes from product table. There may be more products and more catalog. ...
monster's user avatar
3 votes
2 answers
542 views

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 ...
user2638180's user avatar
-5 votes
2 answers
229 views

Term for refactoring looping logic for an event loop

Constrained by a main loop outside of my control (eg: UI event loop, game loop, etc...), I have a slow (relative to the main loop's expectations) algorithm involving a loop. Thus, I have to ...
Peter K's user avatar
1 vote
2 answers
2k views

How to return boolean result from comparison loops to maintain better readability?

Let's consider I have an std::string instance filled with textual data and an std::set<std::string> instance with keywords. I would like to know whether the text stored inside the std::string ...
Akira's user avatar
  • 247
2 votes
1 answer
5k views

Does i in for loops means iteration or index? [duplicate]

Does i in for loops means iteration or index? How could I know the original meaning? Some programmers say it's iteration, some say it's index. It seems to me more of an index we start from --- we go ...
Arcticooling's user avatar
1 vote
1 answer
3k views

What is the benefits of a foreach-loop vs a body-less for-loop

In C# you can use a for-loop without declaring the body, what is the benefit of using this in production-code versus using a foreach loop? Example My Examples below use reflection to get the ...
Brownish Monster's user avatar
5 votes
6 answers
1k views

How to apply SRP in a loop where two things need to be done?

Is there a way to apply the Single Responsibility Principle to a function where two things need to occur in a loop in order to not need to iterate twice? For example, suppose I have a function like ...
charlieshades's user avatar
6 votes
7 answers
9k views

While loop without evaluating data twice

I often come across the following pattern. while(GetValue(i) != null) { DoSomethingWith(GetValue(i++)); } Here GetValue is executed twice. It would be much nicer to use a pattern where we can ...
Roy T.'s user avatar
  • 654
0 votes
2 answers
320 views

Permutations for time in JSON

Let's say that I have a JSON file like at example below. How would I go about finding all possible values of item combination time sums that exist between let's say 00:03:04 to 00:25:55 without ...
user2676326's user avatar
13 votes
2 answers
17k views

What exactly happens when a thread awaits a task inside a while loop?

After dealing with C#'s async/await pattern for a while now, I suddenly came to realization that I don't really know how to explain what happens in the following code: async void MyThread() { ...
aoven's user avatar
  • 309
7 votes
1 answer
357 views

Automatic Realtime Cycle Detection of function calls

I am running into a wall mentally when trying to think of a way to solve this problem. At my work we process customer data through some complex reasoning logic. Sometimes this logic will cause ...
Hangman4358's user avatar

15 30 50 per page
1 2
3
4 5
11