C# loop more efficient in while leasson | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

C# loop more efficient in while leasson

On the while loop lesson where it shows you about while loop I would make the code shorter to make sure the same mission Int num = 1; While (num < 6){ Console.WriteLine(num++); } Done :)

23rd Mar 2020, 10:52 AM
Brandon EBox Ramirez
Brandon EBox Ramirez - avatar
4 Réponses
+ 2
But why? shorter doesn't necessarily mean more readable. Even as it is now, people still find some issues in dealing. IMO shorter code is no good idea when it will only discourage new learners.
23rd Mar 2020, 11:04 AM
Ipang
+ 1
There is not much difference between for and while loops. the for loops can be called a enhanced form of while loops. The for loop structure: for(declearation; condition; postLoopCounter) { //Code } The while loop structure: while(condition) { //Code } In terms of effeciency, there doesnt seem to be much difference between for and while loops. But, obviously, the for loops make our code shorter, and easily debug-able.
23rd Mar 2020, 10:58 AM
Sarthak
Sarthak - avatar
+ 1
Ipang It completely depends on code. On basic counter loops, while loops are not suitable. On non-numerical and multiple conditional loops, for loops work, but not considered as good as while loops.
23rd Mar 2020, 11:07 AM
Sarthak
Sarthak - avatar
+ 1
Sarthak 🇳🇵 I wrote that for the OP buddy, not to your response. I have to disagree on the idea to "compress" snippets especially those used as introductions and/or learning materials. And more specifically, when the most of code readers are new learners, like here in SL. What did you mean by "non-numerical" and "multiple conditional" loops? how come `while` loop excels over `for` loop for these types of loop? Also, why "On basic counters, while loops are not suitable."
23rd Mar 2020, 11:29 AM
Ipang