Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I mean... Couldnt i just put the
Int i = 0;
And
I++;
Inside the while, as done in the for.
And that would be same length of code with no variable issues in future code like the for ?
He just said to always use for instead of while, kinda sceptical about that -
ddephor45116yIf you have a defined number of iterations (the condition fits in one or two statements), it' s better to use a for, it's jut more readable.
Use while if the number of iterations is unknown and defined by the control flow inside the loop. -
Marl3x27796yI only use "while" when I do not have to create a variable, because as @ddephor mentioned, it's more readable.
-
to put it simple...
while expects a boolean expression - and while iterates as long as the expression is true (break / continue etc omitted)
for is made (in most languages) of 3 components, where in some languages components can be omitted.
- Initialize
- Test (boolean expression)
- Continuation
Stating the obvious here, I know.
Point is that I think it's good to use the appropriate loop based on what you want to achieve, like everybody said.
If u feel unsure while coding look at the design and intention of what u want to use.
In this case, I think that for does the job as it was clearly intended to do what you've written in your example. -
Great answers by @irene and @IntrusionCM
Can't add anything afte that but I can offer some cool tips for for loops in C#:
Infinite for loop:
for ( ; ;) { //code here}
Execute some code inside the for loop initializer statement:
int i;
int j = 10;
for (i = 0, Console.WriteLine($"Start: i={i}, j={j}"); i < j; i++, j--, Console.WriteLine($"Step: i={i}, j={j}")) { //Code here }
Enjoy 🤘 -
@zotigapo haha thats a creative way to put it, understandable :D
You are all dope people helping out a C# rookie -
int i = -1;
while(++i < 100) {
Console.WriteLine($"i = {i}");
}
In C#, a while loop expressed as above is generally faster than a for loop. Also, you can use string interpolation like above. If you don't use interpolation, no need for i.ToString(), because when you concatenate a string with a non-string the ToString() is implicit. -
Reusing i is not a very strong argument. In general, an accepted convention is to use i, j,m, etc. as loop increment/decrement variables, scoped to that loop. However, that principle of scope is not applicable to every widely used language. If you reuse it in a different loop 'by mistake' you're not paying attention. There are cases in which you might define i named as index, which expresses your intention to used index's value after the loop.
Use the loop construct that suits your case best! I hope that dude also pointed out that in general, reverse for loops (i--, i >= 0) perform better. -
maybe take a look at Cambria.
It's Barock Antiqua, but the spacing is wider which improves readibility imho. -
Related Rants
Following this c# course on the web.
The dude says to always use the for exampoe instead of the while.
To produce fewer lines of code, and to ensure not to use the variable "i" wrongly later on.
Thoughts on this from you C# masters?
question
learning to code
c#
coding
help
learning