Most voted "loop" questions
Loops are a type of flow control structure in programming in which a series of statements can be run repeatedly until some condition is satisfied.
Learn more…671 questions
Sort by count of
-
54
votes5
answers7178
viewsWhat is the advantage of using recursive functions?
Recently discovered the famous (or not so famous) Recursive Functions and I found the concept very interesting. However, during my reading I had some doubts regarding the use of such. What is the…
php loop software-engineering recursion encoding-styleasked 8 years, 11 months ago João Paulo Vieira da Silva 1,933 -
43
votes3
answers8167
viewsWhen to use recursion and when to use loops?
A problem can be solved and get the same result used a loop or through recursive calls to a function. Whereas the programming language being used has both features, how to know when one is better…
function loop software-engineering encoding-style recursionasked 10 years, 6 months ago Maniero 444,682 -
35
votes3
answers881
viewsIs there a difference between reporting the size in the loop condition or outside it?
If I have an array or collection in a Arraylist and need to go through its elements, occasionally need to make use of loop repetition. Ex.: for(int i = 0; i < arrayList.size(); i++){…
-
25
votes3
answers2778
viewsWhat is the purpose of "continue" in C?
int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf("value of a: %d\n", a); a++; }while( a…
-
24
votes1
answer933
viewsWhy is the use of "break" considered bad?
In many places I have heard several comments about this, such as: "Professional programmer does not use break" "The code sucks when you see a break" Because the use of break is so frowned upon by…
-
21
votes3
answers1786
viewsWhat is the usefulness and importance of "do... while"?
The command while is a repeat structure with a condition at the beginning of the declaration. Example: while (condição) { sentenças a executar } Already do...while has verification at the end, ie…
algorithm loop characteristic-language while do-whileasked 9 years, 4 months ago Denis Caixeta 3,427 -
20
votes4
answers3187
viewsShould one use break in going?
I have a question about the loops of repetitions. I had a class in which my teacher said that one should never use the for if you don’t execute it from start to finish. Only I’ve seen many, many…
-
18
votes3
answers713
viewsforeach is a loop or iterator? Or can it be both?
In a video tutorial the instructor stated not to fall for the nonsense of thinking that the foreach is a loop, and it was vehement that he was an iterator. There are cases where we can go through…
-
16
votes2
answers596
viewsWhy is there such a difference in performance between stream and normal loops?
I was reading a article related to the performance of streams/loops, and was startled by the difference in performance between the use of loops for large quantities of data. I decided to perform a…
-
14
votes3
answers19255
viewsWhat is the difference between while, for, while and foreach?
What is the difference between the while , do while, for and foreach in PHP, because they give the impression that same functionality. All of them can be used to create a loop or has other purposes?…
-
13
votes3
answers689
viewsConvert nested loops to recursive function to compute combinations
I did a job for compute all combinations of an array. The problem with my approach is that it only "generates" three-digit combinations (array length past 3), since I have 3 nested loops. If I…
-
12
votes1
answer2292
viewsHow to create objects (variables) with different names within a loop?
I want to generate different databases in a loop. In the example below would be 3 distinct databases with the following names: "data1", "data2", "data3". for (n in 1:3){ dados<-paste0("dados",n)…
-
12
votes2
answers11353
viewsWhat is the correct way to stop a "for" loop?
Let’s say I have this loop for that works based on my array name: var nomes = ["Nome 1", "Nome 2", "Nome 3"]; for(i = 0; i <= 2; i++) { if(nomes[i] == "Nome 2") { console.log(nomes[i]);…
-
12
votes4
answers299
viewsWhat does the "do" function do in Javascript?
I searched a lot and I can’t find the functionality of do in Javascript. Example: function b(a, b) { do a = a[b]; while (a && 1 !== a.nodeType); return a }…
-
12
votes3
answers5507
viewsCount how many elements are duplicated in a string
The purpose is to account for the number of elements that are repeated in a string and not just check for duplicates or count how many times these elements appear. For example: "aabbca1m" // 2 (a,b)…
-
11
votes4
answers584
viewsHow to verify the efficiency of these 2 functions in C++?
How to determine the best choice between these two functions for implementation? 1: int X(int x){ if(x<=0) return 0; return(x + X(x-1)); } 2: int Y(int x){ int soma=0; for(int i=0;i<=x;++i)…
-
11
votes3
answers15464
viewsFor increment in Python
I learned that in the Python, to make a loop with for, from 1 to 10, we use the range. Sort of like this: for i in range(1, 10): print(i) Generally, in other languages, when we require a simple…
-
11
votes1
answer27744
viewsConcatenate Strings into Java Loops - Stringbuilder or '+'?
Java allows us to concatenate Strings in Java using only the operator '+' String str = "a" + "b" + "c"; It’s a simple way to do the job, and much less verbose than with Stringbuilder. But in cases…
-
11
votes3
answers2738
viewsHow does the browser handle infinite loop in Javascript?
I was doing an exercise on loop repetition for in Javascript on Codecademy, however, in the exercise I came across the following warning: codecadeny Be very careful with your syntax - if you write a…
-
11
votes1
answer112
viewsDoes creating local variables all the time generate extra cost for the software?
I made a program of which he writes variables all the time, and with that generated me a doubt, instantiating a variable all the time generates more cost than just assigning the value to an existing…
-
10
votes1
answer165
viewsWhat is the difference between `for x in y` and `Enumerable#each`?
We can iterate an array/list in ruby in two ways: Using the syntax for x in y: > for x in [1,2,3] do > puts x > end 1 2 3 => [1, 2, 3] Using the method .each > [1,2,3].each do |x|…
-
10
votes1
answer164
viewsGood Practices (Refactoring) - Best way to treat one method with for within another
Personal using good Java practice how should I convert this method? I must break into various methods and within each of them do the go? public void salvarObjetos(Objeto objeto){ for(Objeto1 obj :…
-
10
votes3
answers2257
viewsWhich loop is faster in C: while or for?
Being a bond while and a for that run the same number of times, which is faster? Example: while: int i = 0; int max = 10; while(i<max){ funcao(); i++; } for: int i; int max = 10; for(i=0;…
-
10
votes2
answers171
viewsBesides structures like "for", "while", "goto" or recursion, is there any other way to repeat something in the programming?
In structured programming, we have structures as links for, while and similar structures, in addition to goto, that allow us to realize repetitions. In functional programming, recursion is used to…
-
9
votes2
answers1839
viewsWhich one performs better? For or Foreach+Range?
Of the two forms below, which has a better performance? For: for( $x=1; $x < 31; $x++ ) echo $x . PHP_EOL; Foreach + range: foreach( range(1,30) as $x ) echo $x . PHP_EOL; I know the difference…
-
9
votes1
answer189
viewsWhy is the incrementer "lost" in the loop?
Making a simple loop FOR, I found a strange behavior that I couldn’t understand. The count is "lost" in the AJAX request, keeping the value of the incrementer with the last one. The request to the…
-
9
votes2
answers750
viewsHow to improve the performance of my code with "for"?
I have the following code: for ($i=0; $i < 10; $i++) { for ($j=0; $j < 20; $j++) { for ($p=0; $p < 40; $p++) { echo $vaar[$i][$j][$p]; } } } I believe a code that contains a for inside…
-
9
votes2
answers1709
viewsThere is an "Else while"
Is there any way I can do that? As long as it’s one thing to do that, then when it’s another to do that? For example: var i = 0; while(i < 5){ //faça isso i++; } else { //faça aquilo } It’s…
-
8
votes3
answers2685
viewsAlgorithm C language - Multiplication
Translate to the language C: take a number on the keyboard and repeat the operation of multiplying it by three (printing the new value) until it is greater than 100. Ex.: if the user type 5, we…
-
8
votes2
answers214
viewsWhy isn’t this "for" loop infinite?
public class Loop { public static void main(String[] a) { int cont=0; for (int i=0; i>=0; i+=2, cont++); System.out.println("cont:"+cont); }} Caught my attention the condition of the loop is…
-
8
votes4
answers763
viewsWhat would be a good way to apply events: onMouseOver and onMouseOut, for all img tags?
I need this function to be automated so that it applies to all images built on the HTML document without any exception Code function aumenta(obj) { obj.height = obj.height * 2; obj.width = obj.width…
-
8
votes2
answers337
viewsHow do "for in, for of, foreach" loops traverse the array?
I’ve always used the loop for in which it differentiates a little from the mentioned loops, but the question is how these loops travel the array as, for example, the loop for usually follows these…
-
7
votes1
answer390
viewsHow to create a loop to send an event in socket.io?
I have an architectural problem. I have a code using socket. in nodejs: socket.on('images',function (aData){ ... socket.sockets.emit('show', JSON.stringify({imagens : json})) }); I’m trying to…
-
7
votes4
answers28233
viewsHow to know the highest value of an Array?
I am making 1 calculator that adds values typed by the user and when the values of sums arrive in 1000 or exceed, the loop ends. So far so good, the problem is to be able to identify the largest…
-
7
votes1
answer339
viewsWork with errors within R loop
I entered the non-linear adjustment function gnls within a loop for so that it could test a series of start values automatically. The point is that eventually some of these start values create a…
-
7
votes3
answers1561
viewsIncrement letters in PHP?
Recently I needed to increment letters in PHP within a loop. For each iteration, instead of numerical indexes, it needed letters of the alphabet. So, as I already know that PHP does letter…
-
7
votes2
answers260
viewsIs there any difference between an infinite loop with for and while?
In PHP, it is possible to generate a loop infinity with while simply passing the parameter true. Example: while (true) { echo "Ao infinito e além"; } It is also possible to generate this through…
-
7
votes1
answer230
views -
7
votes2
answers664
viewsWhat are the advantages of Parallel.Foreach in C#?
Working with C# I saw that we have the option to work with parallel.ForEach(). What is the advantage of working with her and not the foreach?
-
7
votes2
answers1573
viewsShow real-time loop result
I’m using a loop to send emails from an array. Every time the loop runs the function sleep(4); is executed. The problem is that php output only happens at the end, ie the lines echo…
-
7
votes3
answers1360
viewsFaster way to access properties in a C#list
I have a project that works with a large volume of data, and I need to optimize it to bring results of some calculations in a considerably small time. I know that I have several aspects to take into…
-
6
votes6
answers2089
viewsHow to change the bottom lines of a table alternately? With support for older browsers
My table is created with a PHP loop. I do this by PHP even adding a condition or has some better shape? if ($nomSenha == 'xxxxxxxx') { echo '<table class="pesquisaClientes">'; echo…
-
6
votes2
answers804
viewsWhy does this code loop infinity?
Because if I put an invalid entry like asdf the code below enters infinite loop? After capturing the exception and writing the message he should not ask for another entry again? import…
-
6
votes3
answers502
viewsINSERT query does not work inside a loop
So guys, I’m not getting to solve this query problem within a foreach loop not run. At first I thought it was something wrong with Pdo->beginTransaction so I commented on that part and left only…
-
6
votes4
answers1206
viewsJava Class Scanner Loop Error
When using the nextLine() method instead of next() in the code below, code interactions are skipped and some fields are empty. import java.util.Scanner; public class turma { public static void main…
-
6
votes5
answers17941
viewsHow to use the loop for(for) in Portugol?
I am learning algorithm, but I was in doubt about the use of Loop para (for). I know it’s a silly question, but unfortunately I have little internet access. I have the book but the doubt is this!…
-
6
votes5
answers2441
viewsHow to concatenate String within a repeat loop?
I need to put some values together string. How do I concatenate values into one string with a loop of repetition. Example: for($c=0; $c < $tam; $c++){ //concatenar sempre o valor $minhastring =…
-
6
votes1
answer173
viewsWhat is the difference between the two array structures and their repetition loops in Javascript?
I would like to know which is the best way to work with arrays? var meu_array = new Array(1, 2, 3); var meu_array = [1, 2, 3]; There is some difference in performance between the 6 cases presented…
-
6
votes1
answer2747
viewsRaw Strength Algorithm for Sudoku Game Resolution in C
I have the following code written in C: #include <stdio.h> #include <stdlib.h> #include <time.h> // Variáveis globais int jogo_tabuleiro[9][9] = {0}; int func_quadrante(int…
-
6
votes2
answers396
viewsWhy are loops slow in R? How to avoid them?
It is very common to hear (or read) that loops are not efficient in Rand should be avoided (at this link or another link or even in this). And proving this statement is simple: numeros <-…