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 8034213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:56:22+00:00 2026-06-05T01:56:22+00:00

I know this is a very newbie C# question but I am implementing a

  • 0

I know this is a very newbie C# question but I am implementing a small program which does the following:

namespace ConsoleApplication
{
    class Program
{
static void Main(string[] args)
        {
    bool isRun = false;
                int number = 0;
                while (isRun = (true) && number < 3)
                {
                    ++number;
                    Console.WriteLine("Number = {0}", number.ToString());
                    Console.WriteLine();
                }
                Console.WriteLine(isRun.ToString());
                Console.ReadLine();
}
}
}

At the end of the while loop, I would have expected the bool value to be true, but is is printed to be false. Why is that? Is this different from C++ where I would have done something like and the same thing in C# is giving me false

while(number<3)
{
is = true;
}
if(is){
cout<<true;
}
  • 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-06-05T01:56:23+00:00Added an answer on June 5, 2026 at 1:56 am

    The reason you’re seeing this behavior is due to the operator precedence involved. Here the && binds more strongly than = so the code in the loop is actually bound as the following

    while (isRun = (true && number < 3)) {
      ...
    }
    

    Once number > 3 the && expression is false and is assigned into the isRun value and simultaneously terminates the loop. Hence once the loop exits you will see isRun as false

    To get the behavior you are looking for you will need to manually correct the precedence with parens.

    while ((isRun = (true)) && number < 3) {
      ...
    }
    

    Note: In general, as @Servey pointed out, the assignment of locals to expressions inside the loop predicate is considered bad practice. Many C# users would actually be surprised that code compiles at all because they’ve been conditioned to only use == in loops.

    It’s more idiomatic to simply set isRun to true on the first line of the loop for this pattern.

    while (number < 3) {
      isRun = true;
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, I know this is another newbie question but I'm very frustrated and I'm
This is a very newbie question. I want to know how to create a
I know this is very stupid question, but I wanted to clarify this. Let's
I know this question is very similar to this one, but I feel its
I know this is a very broadly scoped question, but what do I need
guys I know this question is very basic but I've met in few publications
Ok, I know this is a very basic question, but my head is swimming
This is a very standard newbie question, but I am looking for an expert
This must be a very stupid newbie question, but I have spent my whole
This is a very newbie kind of ASP.NET question: I simply don't know and

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.