A **loop** is a sequence of statements which is specified once but which may be carried out several times in succession.
0
votes
0answers
56 views
Finding plateau in Java Array [migrated]
I having a really hard time finding a plateau within an array. What I'm looking for is length and location of the longest sequence of equal values where the numbers on both sides are smaller. Now I ...
0
votes
0answers
53 views
Where should I declare variables used in for-loops in Java for maximum efficiency? [duplicate]
I'm talking hypothetically but I'll use an example of what I worked on in a programming class a while ago. There's a better way to actually perform the operation, but that's not the point.
An example ...
0
votes
0answers
3 views
Number Patterns using loops in Java [migrated]
I have been trying different variations of for loops and have no clue how to make these patterns:
Pattern 1
54321
5432
543
54
5
Pattern 2
1
12
123
1234
12345
Pattern 3
12345
2345
...
7
votes
5answers
307 views
Is doing an assignment inside a condition considered a code smell?
Many times I have to write a loop that requires initialization of a loop condition, and an update every time the loop executes. Here's one example:
List<String> currentStrings = ...
2
votes
2answers
127 views
Is there a difference between declaring variables outside or inside a loop? [closed]
Is there any difference if I were to write something like this:
int row,col;
for(row = 0; row < data.length; row++){
for(col = 0; col < data[row].length;col++){
//do ...
2
votes
2answers
144 views
'Game loop' in a non-game application?
In real-time games, there is always a game loop that runs every few milliseconds, updates the game with new data and repaints the entire screen.
Is this something that is seen in other types of ...
-2
votes
4answers
270 views
Are for loops supposed to be read inward or outward? [closed]
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
cout << arr2d[i][j] << "\t";
}
cout << endl;
.....
Like that for example. Do you read ...
2
votes
4answers
857 views
Complexity in nested loops
Foreword:
In this post, I will make the common confusion between O(n) and Theta(n) as complexity notations.
I will write pseudo-code to talk about algorithms, using whatever notation I find to my ...
6
votes
4answers
608 views
Is doing two things on one line always a bad practice?
int *itPins = pins;
for(int i = 0; i < count; i++)
{
ConfigureGpifPinAsGpioOutput(itPins);
itPins++;
}
I have the impression that doing two things in one line is bad practice. This is why ...
0
votes
2answers
211 views
Is there a special name for a condition which will break a loop if it increments a set number of times
Is there a name for including a limitation in a loop structure to prevent it from running if its primary condition becomes unwieldy.
For example
for (var i = 0; i < len; i++){
...
4
votes
1answer
317 views
Will loop constructs become obsolete? [closed]
By loop constructs I mean for… and while… constructs with nested statements.
Currently I am coding a tool in Python and decided out of curiosity not to use the regular loops - and rely on ...
1
vote
3answers
279 views
How do you avoid looping mistakes? Mistakes that are not detected by systems [closed]
I had this crazy initialisation --
documentList = new ArrayList<Map<String,Integer>>();
which I intended to store a new map everytime in a loop but unfortunately put itself inside the ...
1
vote
1answer
110 views
Randomly and uniquely iterating over a range
Say you have a range of values (or anything else) and you want to iterate over the range and stop at some indeterminate point.
Because the stopping value could be anywhere in the range, iterating ...
3
votes
5answers
542 views
What does the English word “for” exactly mean in “for” loops?
English is not my first language, but since the keywords in programming languages are English words, I usually find it easy to read source code as English sentences:
if (x > 10) f(); => "If ...
1
vote
8answers
770 views
Why does the instruction “do” require a “while”?
Since this statement is so common:
while (true) (Java)
or
while (1) (C)
or sometimes
for (;;)
Why is there not a single instruction for this? I could think that an instruction that could do it ...
0
votes
2answers
262 views
Best Practice - Loop Exit Via Iterator Modification
I had an interesting discussion with my boss today regarding exiting a loop before the terminal condition is met. He had a construct in some of his VB6 code that looked something like this:
Dim ...
0
votes
1answer
77 views
Code execution within delimiters
Is there any way I can execute a block of code in a loop, like so:
[ some code goes here ]
Where the delimiters are "[" and "]", and also allowing for nested blocks, i.e.:
[the user can create ...
0
votes
2answers
115 views
Loading chunks around center
I am making a voxel game like Minecraft. I am able to load chunks using multithreading with the code...
for (int x = 0; x < WorldSettings.WORLD_WIDTH_IN_CHUNKS; x++)
{
for (int y = 0; y < ...
16
votes
4answers
6k views
Why are nested loops considered bad practice?
My lecturer mentioned today that it was possible to "label" loops in Java so that you could refer to them when dealing with nested loops. So I went home and looked up the feature as I didn't know ...
6
votes
4answers
408 views
Is it a performance hit to create threads that loop a lot to check for things?
This is a generic question that I've always wondered.
In general, is it intensive for a CPU to create threads that perform "while not true ..." loops or similar?
For example, suppose I do:
// ...
0
votes
4answers
122 views
Use functions inside a loop declaration
What's the best practice?
This :
for ($i = 0; $i < count($array); $i++) {
//stuff
}
Or, what I usually do :
$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}
Is it the ...
9
votes
2answers
1k views
How to unit test method that returns a collection while avoiding logic in the test
I am test-driving a method that is to generate a collection of data objects. I want to verify that the properties of the objects are being set correctly. Some of the properties will be set to the same ...
2
votes
2answers
342 views
Repetitive Drawing in Javascript & Canvas
Creating an HTML5 page using canvas and javascript to draw a set number of musical staves on the page, spaced a pre-determined amount in between.
What I have is re-drawn on top of the canvas 10 ...
45
votes
7answers
3k views
Why are semicolons and commas interchanged in for loops?
In many languages (a wide list, from C to JavaScript):
commas , separate arguments (e.g. func(a, b, c)), while
semicolons ; separate sequential instructions (e.g. instruction1; instruction2; ...
0
votes
1answer
137 views
LOOP program only need inc and zero
I have 4 different commands in LOOP programming language:
y=Zero()
y=Val(x)=copy x and put it in register y
y=Inc(x)=x+1
y=Dec(x)=x-1
Finally I also have
loop n times {
...
...
4
votes
3answers
236 views
Is there an idiom for a loop that executes some block of instructions between iterations? (In Ruby in particular)
I often need to do some operations in a loop and some other operations between the iterations. A simple example would be collecting words from an array into a string, spelled backwards and separated ...
3
votes
5answers
984 views
Declaring functions in order to avoid explicit nested loops
My programming professor has told me that it is a good programming practice (at least in C/C++) to declare a function with the inner loop when nesting loops (not for loops, since when, i.e. looping ...
4
votes
1answer
933 views
Loop Invariants in Python
I have a bit of experience with loop invariants but I'm not really clear on them. I'm trying to learn them through an example in Python. Can someone point one out or help me understand?
I've ...
3
votes
1answer
365 views
Loop invariant vs Assertions
I have an exam on Programming Concepts this Saturday and I am struggling to find some information to understand some concepts better.
What is the difference between loop invariant and assertion? To ...
7
votes
1answer
471 views
Loop fusion example using aspect-oriented framework
I had recently read a paper 'Aspect-Oriented Programming' by Gregor Kiczales and others, and found there the loop fusion example.
Here is a definition of the loop fusion from the paper
…the loop ...
4
votes
4answers
790 views
Decrementing/Incrementing loop variable inside for loop. Is this code smell?
I have to read lines from a text file in sequential order. The file is a custom text format that contains sections. If some sections are out of order, I would like to look for the starting of the next ...
3
votes
3answers
498 views
Is there a way to add unique items to an array without doing a ton of comparisons?
Please bare with me, I want this to be as language agnostic as possible becuase of the languages I am working with (One of which is a language called PowerOn). However, most languanges support for ...
7
votes
3answers
2k views
Does it make a difference if I declare variables inside or outside a loop in Java? [duplicate]
Possible Duplicate:
Where do you declare variables? The top of a method or when you need them?
Does it make a difference if I declare variables inside or outside a loop in Java?
Is this
...
0
votes
4answers
12k views
2 Dimensional Arrays in C++
I started learning arrays in C++ and came over a little side note in the book talking about 2D arrays in breif.
I tested it out and i was amazed that it could give the programmer the ability to store ...
9
votes
7answers
3k views
C# foreach improvements?
I run into this often during programming where I want to have a loop count index inside of a foreach and have to create an integer, use it, increment, etc. Wouldn't it be a good idea if there was a ...
-1
votes
4answers
1k views
How to improve my loop logic in programming?
I know how to do simple loops but I don't know what's going on when many loops are working together.
For example:
for (i=0; i <= 9; i++){
document.write(linebreak);
for (m=0; m <= 9; ...
44
votes
6answers
9k views
Who created the idea(s) of the first loop constructs?
while (1) {
if (1+1==2) {
print "Yes, you paid attention in Preschool!";
} else {
print "Wait... I thought 1+1=2";
}
}
As a developer, we all have to use ...