Most voted "for" questions
Use this tag when the question primarily involves logic and/or command structure. Effectively, the for is a loop of repetition used to iterate matrices, vectors, or to another end that basically has three components: start, stop condition, and step, where it is usually possible to omit some of these components (although not very recommended).
Learn more…446 questions
Sort by count of
-
27
votes12
answers28737
viewsWhat are the ways to iterate an array in PHP (without foreach)?
Before I am censured by the question, notice beforehand that the purpose of it is simply to level curiosity. I know that the foreach is the most suitable means for this. But I would like to know the…
-
20
votes8
answers17275
viewsDifference between while and for
What’s the difference between while and for, if the two are loops of repetition, if with the two I can do the same things either conditioning a halt or iterations of variable, because there are…
-
15
votes2
answers310
viewsGo with a ";" at the beginning of the loop, which means
I picked up a code with a code like this: for (; indField > 0 && indMask > 0; ) What does that mean ";" at the beginning and end of the command?…
-
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?…
-
12
votes2
answers3744
viewsHow does the "for" inline command work?
I made a question about a Python algorithm, but reply of user Anderson Carlos Woss he used a kind of for inline that I had never seen and that left me confused. Follow the code section corresponding…
-
12
votes5
answers3081
viewsHow to decide between using for or foreach?
When should I choose between using the for or foreach? They both do pretty much the same thing, and I always wonder if I’m using the "correct"... My doubt increases when people say to use the for…
-
12
votes4
answers242
viewsWhat’s behind the "go"?
Day I came across a question from a user who wanted to print a string, but with time interval between each character, so I suggested that it use the following code: from time import sleep frase =…
-
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
votes3
answers3029
viewsHow to calculate perfect numbers quickly?
I am trying to perform an exercise to show the perfect numbers present within a certain range, but I can only accomplish such a feat until the perfect fourth number. If I raise the range, it takes…
-
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;…
-
9
votes3
answers318
viewsIndentation in statement "Else"
What is the difference in the indentation of else "out of" the if. In this case it is to return the prime numbers up to the nth 'n' value. First case: for i in range(2, n): for j in range(2, i): if…
-
8
votes3
answers156
viewsDifference in behavior between for and for.. in
I have the following code that works exactly as expected (which is to go through the array, showing ONLY each element, in this case "Miguel" and "Celeste"): var runners = ['Miguel', 'Celeste'];…
-
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
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
votes1
answer460
viewsWhat is " : " (two points) in C#?
I still don’t understand what the two points represent and why this code isn’t running. int soma = 0; int[] lista = { 3, 7, -6, 10, -1, 0, -1, 4}; for (int i : lista) { if (i > 0) { soma = soma +…
-
8
votes2
answers118
viewsWhat’s the difference between executing code inside or outside the "for" keys?
In Javascript or C, there is some difference between using for in the normal way: for(var i = 0; i < 10; i++){ console.log(i); } or execute the codes within the parentheses? Example: for(var i =…
-
8
votes2
answers391
viewsC++ (basic): for, references and syntax
My teacher presented this function to us: void escala(std::vector<double> &v, double fator) { for (auto &vi:v){ vi *= fator; } } It serves to multiply all the elements of a vector by a…
-
8
votes2
answers144
viewsPrint the result of a for JS inside a <p>
I’m doing a college paper that consists of making a tabuada generator. It’s working perfectly when I use it console.log to show the result, but when I use .innerHTML = resultado to display in a tag…
-
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
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
votes2
answers2134
viewsWhich loop is faster for or foreach in C#?
I’ve read articles from some programming languages that loop for is faster than the foreach, and wanted to know if C# has performance differences?
-
7
votes1
answer386
viewsWhat is and how does the repetition of the for in C#?
for (int i = 0; i < palavra.Length; i++) What does each word mean? And how each part of that code works?
-
7
votes2
answers2928
viewsWhat’s the difference between For, Foreach and Find in Javascript?
What is the difference between the 3? For, ForEach and the Find
-
7
votes3
answers1292
viewsHow to loop 'for' in 1 line?
The following code did not work: rank = [1, 2, 3, 4] print(rank[c] for c in range(4)) If you can make use of for in a row would like to know.
-
7
votes3
answers162
viewsAnother alternative to not repeat this "function" three times?
Make a program that reads three vectors with 10 elements each. Generate a fourth vector of 30 elements, whose values should be composed of the interspersed elements of the three other vectors.…
-
6
votes3
answers12748
viewsGenerate random numbers in an Array from 10 to 50
Since I can generate an array of random numbers with a limit, they should be numbers from 10 to 50. To generate random numbers from 0 to 50 I use: Random random = new Random(); int array[] = new…
-
6
votes1
answer226
viewsWhy a loop FOR faster than 10 FOR together
I made a for alone count up to 1000000 (sending 1 message each loop) and it took 14 seconds. public class main { public static void main(String[] args) throws InterruptedException { long init =…
-
6
votes4
answers325
viewsLoop 20 to 20 lines
I would like every click on the button to count, and the same would invoke the variavel to change the initialization and the condition of noose for, for a numerical value of 20 on 20. It would be…
-
6
votes1
answer347
viewsSplit base with "for" in R
As it is my first for in the R I found it difficult to apply this function. I have a base with a base date of different years and would like to divide the base by base dates. The variable "date" has…
-
6
votes4
answers324
viewsInterpretation of loop of repetition
I’m having trouble understanding the following code that is presented as a book exercise. $a = true; $b = -2; $c = 7; for ( ; $b < $c || $a; $b++){ if ($c + $b * 2 > 20) $a = false; echo $b."…
-
6
votes2
answers1649
viewsWhat is the underscore (or underline _ ) for in the repeating structure?
What this _ ago? Why it is being used in the code snippet below? print('3 numeros') data = [] for _ in range(3): data.append(input()) numbers = list(map(int, data)) print(numbers)…
-
6
votes3
answers820
viewsHow to exit the FOR loop within the Switch structure?
I’m in a noose FOR and I want to lock him inside the Switch, but when I give break within the Switch he comes out of Switch and continues the loop FOR, someone can tell me how to get out of the loop…
-
5
votes1
answer2438
viewsA break within two for actually works as a continue?
If I happen to have one break within two for, as in the example below: for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { break; } { The break won’t make the show come out of both for,…
-
5
votes1
answer605
viewsClose loop when typing specific character
Hello! I need to resolve the following question, but I’m not getting it. Write an algorithm that calculates the average arithmetic of the students' 3 grades (number undetermined of pupils) of a…
-
5
votes2
answers612
viewsAccess items from a stack using for
I’m trying to access a forward position in the pile to make a comparison, but it’s not working and I couldn’t understand this for. Code for ( String UmToken : PilhaTokens) {…
-
5
votes1
answer64
views -
5
votes2
answers1182
viewsUsing For in Python
Python can only work with for' (loop) using a list? It is not possible only with an integer as in other languages?
-
5
votes1
answer121
viewsPerformance difference from for simple and for iterator
I have a list of customers and on a certain screen the user can do a search and edit the clients of the list that is quite extensive, comparing these two examples which would be the best to work?…
-
5
votes1
answer70
viewsHow do I exchange a apply inside a for for for a double apply?
I have a vector origem and a vector destino with different locations in latitude and longitude. For each location in origem, I want to count how many places in destino are located in a radius of up…
-
5
votes1
answer1089
viewsHow does the for(;;)work?
It’s probably duplicated, but I didn’t find it here on Sopt, and I don’t know how to Google. How the loop works for in that syntax? for(;;) { //... }…
-
5
votes1
answer215
viewsComparison of value within data frame
Hello, I have a database, with about 50000 remarks, as follows, only figurative values: nome<-c("joão","pedro", "joãoo") identificador<-c(123456,124578,123456) valor<-c(2145,350,23)…
-
5
votes2
answers1750
viewsHow to create a for in R with the indexes of a data frame
If I have a date.: > dato<-as.data.frame(matrix(1:64,8,8,T))[-3,] > dato V1 V2 V3 V4 V5 V6 V7 V8 1 1 2 3 4 5 6 7 8 2 9 10 11 12 13 14 15 16 4 25 26 27 28 29 30 31 32 5 33 34 35 36 37 38 39…
-
5
votes1
answer415
viewsTemporary variable performance within loop
During college a professor commented with me that declare a variable before a loop and reuse it was an economic and more interesting way to do it. For example (in Java): String str; for(Pessoa p :…
-
5
votes3
answers97
viewsWhy does the first element of the vector come out as zero without having stored this value there?
Why the following code prints a 0 at the beginning of the output? #include <stdio.h> #include <limits.h> // Escreva um programa que leia do teclado 8 valores, e em seguida os // imprima…
-
5
votes3
answers969
viewsDifferences and similarities between apply and for loop functions
I have this list: dataset<-data.frame(matrix(runif(6*30,20,100),ncol=6)) cluster<-kmeans(dataset,centers=3) cluster dataset$kmeans<-as.factor(cluster[['cluster']])…
-
4
votes3
answers633
viewsLoop variable declaration error
I made the following command: for(int i = 1 ; i <= 3 ; i++) {etc } Then it made the following mistake when I went to compile: game.c:11:2: error: "for" loop initial declarations are only allowed…
-
4
votes3
answers6684
viewsHow to create a dynamic variable
I have a field(input text), and would like to get the value, and pass as variable. In other words, pass the value of this text field into the: var valor do campo aqui ; See the example I leave:…
-
4
votes2
answers85
viewsFunction Optimization (probably with for loop)
I’m having trouble developing when we talk about for... The basics I can understand now in practice... People have developed this code and I believe it can be reduced to a few lines with for. Notice…
-
4
votes1
answer216
viewsProblem with the sum of 10 numbers with for
I have a doubt in an algorithm that sums 10 numbers with the repetition command for. soma_numero := soma_numero + idade; When I declare the soma_numero as 0 before the for the algorithm runs…
-
4
votes4
answers89
viewsRepetition is not equivalent to one second
How much would be the limit value for a repeat to be compared to a second? For example: for x in range(0, 1000): print("1 segundo")