+ 9
Why for loop methods allows semicolons inside the parenthesis?
Just curious why we cant do the same. :)
10 Réponses
+ 5
I think only the creators of the different programming languages can answer that.
+ 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 😁
+ 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?
+ 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.
+ 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
+ 2
David Ordás thanks. Yes I knew that.
+ 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);};
+ 1
Fill in the data types of the data shown below in the comments field
+ 1
Other languages borrowed that style from C.
+ 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