Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.

learn more… | top users | synonyms

1
vote
0answers
19 views

Brute Force Password Cracker - Multi-threading and making code prettier

I am just coding some classic brute force password cracking program, just to improve myself. I've explained how my program works at the start of the code. Check some of those screenshots to ...
5
votes
1answer
311 views

Mini mind reader

Here is a code that guesses the number chosen by the user. I know that using goto is a bad practice, but it seems unavoidable here. that's because using do while doesn't work, for example: do{ ...
2
votes
1answer
33 views

How to shorten the JavaScript in Raphaël?

The Rapheal script seems to be too long. Is there any option to make it shorter ? I think loop can make this script smaller. Does anyone have good suggestions or advice? ...
23
votes
5answers
3k views

Is it OK to use while ((line = r.readLine()) != null) construct?

I want to refactor the following code because I don't feel comfortable about using assignment inside comparison operator. It looks like pretty idiomatic C, but do you think this is a good practice in ...
3
votes
3answers
96 views

Loop optimization for image processing

I have this piece of code that is running too slow. I was wondering if anyone can help me optimize it as I can't seem to find any more shortcuts. I'm not sure if using List<> is going to help me ...
15
votes
3answers
2k views

Is my coding technique progressing in terms of C# loops?

I have isolated a little bit of code that was causing a small debate between myself and another user. I have taken some of the things that he said and meshed it with the code that was being reviewed ...
-2
votes
2answers
91 views

Is this correct way to use loops or should it be in LINQ?

The results variable is coming from an API that have a arrays in arrays, etc. I have made a foreach with for loops inside, which looks like this: foreach (var s in results) { var ...
10
votes
9answers
2k views

Is there a more succinct way of writing this simple JavaScript loop?

This seems a bit redundant to me, but I'm not sure of how else I might be able to write this. Normally I'd use a switch statement, but I don't think that'll work here. I realize I could also do this ...
7
votes
2answers
365 views

Object Creation during loops

I'm trying to parse a CSV file into objects. I've already got a very efficient CSV parser (SuperCSV). The problem arises when trying to create the object with the data that is being read out. I've ...
6
votes
2answers
53 views

Collect and calculate average times from log, then display top 10 longest durations

Here's a novel-length summary of the issue: I'm trying to write a VB.net program to help me collect remote site statistics from system-generated logs, but I'm a little like a carpenter who only knows ...
9
votes
1answer
125 views

Making C++11 range-based for loops a bit more useful

C++11 is great. Probably one of the most beautiful features (in my opinion) is the so-called range-based-for-loop. Instead of for ( std::size_t i(0); i < range.size(); ++i ) { // do something ...
4
votes
1answer
123 views

Building <div>text</div> inside a while loop [closed]

I use the following code <div><?php echo $obj->text; ?></div> in while loop. Is this the best way? Is there a better way, either to optimize or replace this code? <?php ...
5
votes
1answer
57 views

Game Loop and FPS

Some time ago I made my simple game loop, so here's the code: public class Game { private final int TARGET_FPS = 60; /** optimal waiting time in milliseconds */ private final long ...
5
votes
1answer
26 views

Read line and combine duplicate entries based on one of the fields

The following Java code reads lines from an input text file and will output the entry that has the highest number in field-2 to an output file: INPUT: William J. Clinton 6 Q1124 42nd President ...
5
votes
2answers
219 views

QuickSort of Comparable[]

Here is the code for the QuickSort class that I created. public class QuickSort { public static void sort(Comparable[] a) { quicksort(a, 0, a.length-1); } ...
9
votes
2answers
118 views

Thread-safe prime number source

I am working on some multi-threaded code that requires a prime-number source. The following code keeps an array of primes in memory, extending it as needed. It works in the 'int' domain, being ...
3
votes
1answer
38 views

Display PHP Menu Stored in an Array and Looped

I'm creating a PHP website for a non-profit. They have some restrictions (no MySQL or pre-installed CMS) so I'm creating a CSS menu displayed by an unordered list where all of the elements are stored ...
5
votes
2answers
243 views

Relevant performance difference C#-VB.NET - Integer Division

C# code static int KEY_NOT_FOUND = -1; private void Form1_Load(object sender, EventArgs e) { int[] A = createArray(1, 100000); Stopwatch sw1 = performCalcs(1, A); Stopwatch sw2 = ...
6
votes
1answer
101 views

How to fill an ArrayList of ArrayLists with a Left Join?

I have a class Employee that contains an ArrayList of Projects. I'm storing the Employees in one table, and the Projects in another. I'm trying to find the best way to create an ArrayList of ...
0
votes
1answer
68 views

Time Limit Exceed Problem [closed]

When I submitted below code in codepad.org, it shows "Time limit exceed" problem. Please provide alternative solution : Problem Statement : Given a number K, find the smallest Fibonacci number that ...
6
votes
2answers
568 views

Read an input text file and store the tokens in 2 different arrays

I am very new to Java so please ignore if there are obvious mistakes. If my question seems redundant then please guide me towards the correct link. However, I have surfed enough in order to find the ...
7
votes
1answer
86 views

Creating a thread for file transfer

I am creating an application in Java that runs at scheduled intervals and it transfer files from one server to another server. For SFTP I'm using jSch, and my server and file details came from ...
10
votes
5answers
1k views

Printing star greater symbol in Java

I need to print this in Java. I have written code for this, but I feel that my code is too big. * ** *** **** *** ** * My Code: public static void main(String[] args) { for(int i=0; i<=3; ...
21
votes
5answers
2k views

Print an ASCII diamond

This takes a width specified by user and prints a diamond of that width. It uses only three for loops, but could I reduce that further? Is there a more elegant solution? public class Diamond { ...
2
votes
2answers
57 views

Optimizing Python code for Project Euler #5 (LCM of all numbers from 1 to 20)

This is my solution for the 5th problem of Project Euler, which asks for the least common multiples of the numbers from 1 to 20 (inclusive). Is there any way to improve the while loop conditional ...
37
votes
11answers
8k views

Nesting versus GOTO: which is better to avoid?

In Java they're not really known as GOTO statements and are rather referred to as Branching Statements, but I find that the former term is a bit more indicative of what they actually do. Regardless, ...
1
vote
2answers
44 views

Managing while loop in bash

Two difference scenario in bash program, First Here i am breaking loop in function testFunc. #!/bin/bash function testFunc { if [ some condition ] ; then break fi } while [ 1 ] do ...
17
votes
6answers
4k views

Guessing a unique 4 random digits number

I've created a simple game, in which the user needs to guess 4 digits number between 0-9, generated randomly using Random() Each of the 4 digits are different from each other, with no repeated digits. ...
8
votes
3answers
86 views

List iteration, creation, or comprehension

I suspect that this can be done in a much neater way using list comprehension in python: poses = [] for x,y in get_points(spline): pose = Pose2D() pose.theta = 0 pose.x=x pose.y=y ...
3
votes
2answers
57 views

Find end of $.each() loop

I need to find the end of a loop or the length of the object. Some context on the code: I obtain the txt var from the DOM (that's the only way I've got by now and can't change it) and it's a ...
10
votes
3answers
358 views

Creating all possible combinations of points

I want to create all possible combinations of points (0,0),(0,1)...(12345,6789) and then all segments from these points. The code I have written is simple and with no optimization. Is there any ...
3
votes
3answers
262 views

Decrease CPU usage in while loop

I've a application which runs with two threads. The main thread runs in one infinity-while-loop: while (true) { try { ArrayList<Info> infoList = zabbixHandler.APIRequest(); ...
3
votes
1answer
59 views

Math Skills Game Advice

I think my code works pretty well, although I'm biased: http://jsfiddle.net/AHKb4/2/ Basic Overview: I'm working on building a math skill game, where the objective is to drag and drop div's to a ...
1
vote
2answers
93 views

How can I remove this for-loop in this competitive FizzBuzz code?

I'v written this code snippet that's part of a code competition. I want to do the trick without the for-loop, or simply find a way to optimize this code for speed. for ( i=1; i<=N; i++ ) ...
4
votes
3answers
197 views

Do-While loop in Python, with limit on the amount of 'do's

I'm in a location with poor Internet access. In order to ensure that a call to an external service succeeds, I am wrapping the call in a pseudo-do-while in Python. Additionally, I am limiting the call ...
4
votes
1answer
52 views

Locating empty field groups for saving data

The aim of the code below is to see if selected fields are empty so data can be saved in that field. Since I'm making a library system, I wanted to record the basic info on one record (all the books ...
4
votes
1answer
185 views

Optimize MySQL double select inside for-loop

I want to get the number if items with state 4 and 1 from my database for each day between a certain date. Is there a smarter and more performative way than this one? I am aware that I should use ...
3
votes
2answers
101 views

7 for loops, 9 variables, 7 if statements, and one function

I have a large js function to calculate the number of chips, and which denomination of chips to show as the players chip stack. It's done on the base of 10. I have chip denominations worth 1, 10, ...
5
votes
1answer
87 views

Optimising Funny Marbles

Problem statement is at http://www.codechef.com/DEC13/problems/MARBLEGF Lira is given array A, which contains elements between 1000 and 2000. Three types of queries can be performed on this ...
4
votes
2answers
72 views

Removing redundant lines from script

Context: I've been learning Python for a few months and I'm able to write codes which do work, but they normally look very ugly and contain a lot of unnecessary code. But I normally struggle to find ...
1
vote
2answers
36 views

Better way to display list of categories

I need to display a list of category links, like so: Category 1, Category 2, Category 3 I've got it working already but the code seems pretty repetitive and a bit of a mess, I was wondering if there ...
31
votes
10answers
2k views

Perform instruction in loop every time except the last time?

The specific issue is this: I am writing to a file and want to output a new line after each line written. If I use a normal loop without any further checks, this will create a blank line at the very ...
2
votes
3answers
91 views

Make code more memory efficient, or shorter

//rename files if(!empty($_FILES[$desktop_fieldname_1280x800]['name'])){ file_exists($desktop_filename_1280x800 = mb_strtolower($desktop_dir_1280x800 . 'wall_desktop_1280x800_' ...
3
votes
1answer
48 views

Simplifying loop for Incidence Matrix

I had to work in the Transport Problem and we're asked to try to make the fastest code for big matrices, so we're advised to try to avoid loops. And the only one I've got and I cannot imagine how to ...
7
votes
4answers
159 views

Looping Strategy: Change behaviour after first iteration

I have often encountered the following situation. I want to call a method in a Loop, but want to skip the call at the first run. It do not have to be a method, it also can be some lines of code that I ...
19
votes
7answers
1k views

Is it bad practice to increment more than one variable in a for loop declaration?

While coding today, I came across the following loop in C#: for(int x = 0; x < max_x_index; x++){ for(int y = 0; y < max_y_index; y++, count++){ some2DarrayRepresentedBy1D[count] = ...
5
votes
1answer
84 views

Running a block of code on every interval except the first iteration

I am working with an encryption algorithm that looks like this The key is a number stored in 4 bytes. For each byte in the message, XOR it with the corresponding byte in the key When you reach the ...
1
vote
1answer
113 views

foreach loop too slow?

I have a nested array that returns objects which are then traversed to retrirve the ids, which are then used to retrieve another array, which is traversed and the values returned are echoed out. The ...
1
vote
1answer
84 views

Getting limited user input, with echo

I've written a code snippet that will get a (max.) 14 character string from the user input, and while the user is typing, simultaneously echo it out to the screen. It seems a bit long and ugly, so I ...
18
votes
6answers
1k views

How can I make this mess of Java for-loops faster?

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words called sortedDictionary. I have ...