Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
1 vote
1 answer
200 views

Should I separate special cases from for loop deliberately, or let a for loop handles special cases naturally?

Suppose I need to find the last selected index of options according to last selected contents, eg: options : A,B,C,D,E ,last selected option: C, so the last selected index is 2. There is a special ...
wcminipgasker2023's user avatar
-5 votes
1 answer
1k views

How do I manage multiple nested for-loops without using multiple variables?

If I have code that looks like this: int i; void functionA (){ for (i=0; i<10; i++){ functionB(); } } void functionB (){ for (i=0; i<20; i++){ doSomething(); } } ...
Noah Smith's user avatar
4 votes
2 answers
511 views

How to avoid duplication in a for loop when "initialization step" is identical to "update step"?

I often find a situation where I need to write duplicate codes in a for loop, where the "init step" to identical to the "update step": // duplicate `next()` for (let x = next(); p(x); x = next()) { ...
golopot's user avatar
  • 266
4 votes
5 answers
747 views

Is copy and paste the head of for-loop (e.g.:for(let i=0;i<something.length;i++)) violating DRY principle?

For example, in my project, I often found some head of for-loop appears many times, eg: for(let i=0;i<SharedData.students.length;i++){ SharedData.students[i].something=..... } if(isReset){ ...
ocomfd's user avatar
  • 5,722
2 votes
2 answers
181 views

Should special case be inside or outside the for loop here?

For example, suppose I have 2 arrays: let arr1=[5,2,1]; let arr2=["abcde","ab","a"]; my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index ...
ocomfd's user avatar
  • 5,722
3 votes
2 answers
126 views

Should I move tasks which is just for a specific element only out of for loop?

For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be: 1.place inside for loop for(int i=0;i&...
ocomfd's user avatar
  • 5,722
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
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,722
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
4 votes
2 answers
703 views

Common loop variable names for indexes in 4D and above

I am curious if there are common loop index variables (of single character or not) for dealing with 4 dimensions and above? I was helping another student working through CS50x who was just learning ...
MathFromScratch's user avatar
22 votes
8 answers
5k views

At what point is it taboo to have loops within loops?

Just curious. The most I have ever had was a for loop within a for loop, because after reading this from Linus Torvalds: Tabs are 8 characters, and thus indentations are also 8 characters. There ...
Anon's user avatar
  • 3,613
1 vote
1 answer
1k views

How to name variables without plural in a for-each loop? [closed]

How do you name loop variables when the list item is named after something without a plural? For instance (in python): [x for x in sheep]. x is not a great name, but sheep have/has no plural that ...
AncientSwordRage's user avatar
2 votes
3 answers
521 views

The recommended Way to exit a Loop

Occasionally - but recurringly - I face the following loop pattern problem: CodeSnippet1 DO WHILE LoopCondition //LoopCondition depends on some pre-calculation from CodeSnippet1 CodeSnippet2 ...
fjf2002's user avatar
  • 123
8 votes
5 answers
55k views

How to structure a loop that repeats until success and handles failures

I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another 1/...
wefwefa3's user avatar
  • 1,007