+ 9

Why for loop methods allows semicolons inside the parenthesis?

Just curious why we cant do the same. :)

30th Mar 2021, 3:49 PM
D_Stark
D_Stark - avatar
13 Answers
+ 5
I think only the creators of the different programming languages can answer that.
30th Mar 2021, 3:59 PM
Jan
Jan - avatar
+ 3
Martin Taylor I'm guessing we cant recreate them? It also fascinates me that the body of the forloop is executed before the increment operation is made, there like while loops in disguise 😁
30th Mar 2021, 7:17 PM
D_Stark
D_Stark - avatar
+ 3
D_Stark in a while loop, the incrementing can be done anywhere inside the body. Why does it remind you of a while loop?
31st Mar 2021, 9:51 AM
Sonic
Sonic - avatar
+ 2
in a lot of languages, for loops allows semicolons(for eg-: JS, C, C#, C++, Java, not sure about lua) so those who think the answer that it's the developers taste is not the correct answer unfortunately.
30th Mar 2021, 4:44 PM
Rellot's screwdriver
Rellot's screwdriver - avatar
+ 2
D_Stark, Sonic, Martin Taylor Some more secrets... Use a for(;;;) { ... } or without body for(;;;); and you will get an infinite loop seamless to while(true) { ... } Also, as you can see, neither three parts are required, even the body..., but can contain multiple sentences too int count = 0; var sc = new Scanner("Hello learners") .useDelimiter("\\s+"); // iterate consuming for(;sc.hasNext(); sc.next(), count++); System.out.println(count); // 2
4th Apr 2021, 3:04 AM
David OrdĂĄs
David OrdĂĄs - avatar
+ 2
David OrdĂĄs thanks. Yes I knew that.
4th Apr 2021, 6:30 PM
Sonic
Sonic - avatar
+ 2
David OrdĂĄs Sonic also this is ok, if you want to increment n from the start; for(int n=0;n<10;){ ++n; System.out.print(n);};
4th Apr 2021, 6:56 PM
D_Stark
D_Stark - avatar
+ 1
Fill in the data types of the data shown below in the comments field
31st Mar 2021, 3:21 AM
Nozimjon Ubaydullayev
+ 1
Other languages borrowed that style from C.
31st Mar 2021, 9:48 AM
Sonic
Sonic - avatar
+ 1
Sonic just because the body of the method is executed before the increment. Initalizing variable Check condition Execute body Increment Because incrementing the variable using ++i or i++ makes no difference
1st Apr 2021, 8:27 AM
D_Stark
D_Stark - avatar