Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 188513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:05:08+00:00 2026-05-11T16:05:08+00:00

The question field is a bit too short to pose my real question. If

  • 0

The question field is a bit too short to pose my real question. If anyone can recapitulate it better, please feel free.

My real question is this: I’m reading a lot of other people’s code in C# these days, and I have noticed that one particular form of iteration is widely spread, (see in code).
My first question is:

Are all these iterations equivalent?

And my second is: why prefer the first? Has it something to do with readibility? Now I don’t believe the first form is more readable then the for-form once you get used to it, and readibility is far too much a subjective item in these constructs, of course, what you use the most will seem more readable, but I can assure everyone that the for-form is at least as readable, since it has all in one line, and you can even read the initializing in the construct.

Thus the second question: why is the 3rd form seen much less in code?

        // the 'widespread' construct
        int nr = getNumber();
        while (NotZero(nr))
        { 
            Console.Write(1/nr);
            nr = getNumber();
        }

        // the somewhat shorter form
        int nr;
        while (NotZero(nr = getNumber()))           
            Console.Write(1 / nr);            

        // the for - form
        for (int nr = getNumber(); NotZero(nr); nr = getNumber())
            Console.Write(1 / nr);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:05:08+00:00Added an answer on May 11, 2026 at 4:05 pm

    The first and third forms you’ve shown repeat the call to GetNumber. I prefer the second form, although it has the disadvantage of using a side-effect within a condition of course. However I pretty much only do that with a while loop. Usually I don’t end up passing the result as an argument though – the common situations I find myself in are:

    string line;
    while ( (line = reader.ReadLine()) != null)
    ...
    

    and

    int bytesRead;
    while ( (bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
    ...
    

    Both of these are now so idiomatic to me that they don’t cause me any problems – and as I say, they allow me to only state each piece of logic once.

    If you don’t like the variable having too much scope, you can just introduce an extra block:

    {
      int bytesRead;
      while ( (bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
      {
         // Body
      }
    }
    

    Personally I don’t tend to do this – the “too-wide” scope doesn’t bother me that much.

    I suspect it wouldn’t be too hard to write a method to encapsulate all of this. Something like:

    ForEach(() => reader.ReadLine(), // Way to obtain a value
            line => line != null,    // Condition
            line =>
    {
        // body
    };
    

    Mind you, for line reading I have a class which helps:

    foreach (string line in new LineReader(file))
    {
        // body
    }
    

    (It doesn’t just work with files – it’s pretty flexible.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Bit of an unusual question, but I have setup a field inside a MySQL
My question is how can i clear the editText field after i have saved
In this question: If a form field has multi validators, how to let play
I came across this question on transcender: What should you apply to a field
I saw this question Inject into private, package or public field or provide a
This question is a bit rhetorical. At some point i got a feeling that
This question was inspired a bit by this question , in which the most
according to this question , I can't assign the result of a boolean logic
This is a broad question, so let me narrow it a bit. I am
This may be a bit of an odd question, and what I have in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.