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?

  • 9
    The variable is shadowed anyway inside the block.

    For loop is not always an appropriate solution, for example when step is not uniform and depends on the inside logic. If you are concerned about variable being in the scope you could narrow the scope by using blocks.
  • 1
    I mean... Couldnt i just put the
    Int i = 0;



    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
  • 2
    @BitByteBoolean updated my comment
  • 1
    @irene got ya, thanks 📚🤓
  • 12
    If 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.
  • 1
    I only use "while" when I do not have to create a variable, because as @ddephor mentioned, it's more readable.
  • 1
    @ddephor I wish people understood this very simple distinction between the two.
  • 1
    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.
  • 8
    Totally off topic, but...
    That font tho
  • 8
    Is.. is that Times New Roman?
  • 4
    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 🤘
  • 4
    I am gonna take a "while"
    If you know everthing its "for" you.
  • 4
    @MrCSharp while(true){...} IMO is easier to understand at a glance than for(;;){...}.
  • 2
    @zotigapo haha thats a creative way to put it, understandable :D
    You are all dope people helping out a C# rookie
  • 1
    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.
  • 1
    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.
  • 2
    @CodeMasterAlex *i, j, k. >__>
  • 1
    @irene that god damn S9 keyboard... I always tap the wrong fucking keys haha
  • 2
    For is While++
  • 2
    @Gregozor2121 it's actually a syntactic sugar to while.
  • 2
    Actually I prefer “for loop” for easier reading
  • 2
    What is that font?
    It's gorgeous.
  • 0
    Time New Roman?
  • 0
    maybe take a look at Cambria.

    It's Barock Antiqua, but the spacing is wider which improves readibility imho.
  • 0
    @IntrusionCM do code in Helvetica. Let it look SOVIET 😄
  • 0

    And Wikipedia is immensely helpful:

  • 0
    Or even such...
  • 0
    @Irene I use Fira Mono Code. :)

    But I know a bit about fonts due to the use of TCPDF / PDF design.
  • 0
    @IntrusionCM I use Hack font (It's actually named Hack)
Your Job Suck?
Get a Better Job
Add Comment