C# problem i dont understand but its easy

Jannis1366

New member
Joined
Aug 18, 2023
Messages
3
Programming Experience
Beginner
C#:
using System;

namespace JumpStatements
{
  class Program
  {
    static void Main(string[] args)
    {
      bool buttonClick = false;
      int alarm = 0;
      do
      {
        Console.WriteLine("BLARRRRR");
        alarm++;
        if(alarm == 3){
          break;
        }
      } while(!buttonClick);
    }
  }
}

i dont understand why the !buttonClick is always False. I learned that the ! means negation and if the buttonclick is false, the !buttonclick is true. But the code only works when the buttonclick is false and the while(!buttonclick) is false eather. I tried it with buttonclick==false and is always the same answer. Nobody can really explain it to me at work, pls can you?
 
Last edited by a moderator:
@Jannis1366 : Welcome to the forum. In the future, please post your code in CODE tags. You can use the </> button on the toolbar if you are not familiar with how to format in BBCODE.
 
Describe what you consider to be a working state vs. a non-working state.

!buttonClick is always False

How do you know it is always false?

As I read the code buttonClick will always be false, but !buttonClick will always true, so your code boils down to:
C#:
do
{
    :
} while (true);
 
Describe what you consider to be a working state vs. a non-working state.



How do you know it is always false?

As I read the code buttonClick will always be false, but !buttonClick will always true, so your code boils down to:
C#:
do
{
    :
} while (true);

Hey, yeah my understanding of this code is like this: I create a bool buttonclick=false; and than i will create the do while and create the while(!buttonclick). In my understanding the (!buttonclick) is now true, but the code will run like the (!buttonclick) is = false; i dont understand why that is.
 
Describe what you consider to be a working state vs. a non-working state.



How do you know it is always false?

As I read the code buttonClick will always be false, but !buttonClick will always true, so your code boils down to:
C#:
do
{
    :
} while (true);

so my understanding is more Like your explanation, but my collegues say that the while loop cant know if a bool is true or false, so its false if i use the !. My head goes crazy haha
 
Yes buttonClick is false because you initialized it to false and then never change the value anywhere in your code after you have initialized it.

Since buttonClick is false, then ! buttonClick will always evaluate to true while the code is running.

I think your colleagues are very mistaken about a while loop can't know if a bool is true or false. The whole point of a while loop is it to evaluate if the bool is true or false and only continue if it evaluates to true.

A while loop can't know if the bool is true or false at compile time. The compiler can usually infer this at compile time and optimize the loop, but the while itself is executed at runtime.
 
Sometimes operations do change variables. Sometimes they don't. Generally operations that involve an = symbol do change a variable, but not always

You can get C# to help you out by putting the operation on a line on its own. If you get an error saying "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement" then the operation didn't change the variable

For example:

C#:
int x = 0;
x++;  //operation, doesn't involve =, does change variable

x is now 1

C#:
bool b = false;
!b;  //not a statement; gives an error

Calling !b doesn't flip the value of b from false to true. It takes the false that b is and converts that false to true but that in itself doesn't affect the value of b.

If an operation did affect variable values that it touched, things would go crazy, and C# would be fairly unusable. Just imagine if doing an add changed a number variable:

C#:
  int age = 20;
  Console.WriteLine("Next year you will be " + (age+1));

If doing (age+1) caused age to increment by 1, things would be very difficult to use because every time we did something like that print out we would have to remember to subtract to counteract the add.. and some things can't be undone - squaring a negative number for example

Side note, because assignment operations in C# usually return the value that was assigned to the variable, this is actually legal C# that would print your age next year and would actually also change the age variable:

C#:
  int age = 20;
  Console.WriteLine("Next year you will be " + (age+=1));

Try to avoid this when you're newbie

---


More examples, this time that do involve equals signs:

C#:
int x = 0;
x+=1;

This is same as before, x is 1

C#:
bool b;
b != true; //also not a statement, gives an error
b ^= true; //this one does work

b ^= true does work to flip b from false to true because it is short hand for b = b ^ true which is an exclusive-or op (returns false when both operands are true)

Not wishing to get too deep into this but there are other situations where you can do something but you have to capture the returned value to see a permanent change. Here are two examples, one that will and one that won't work:

C#:
  int a = new int[]{3,1,2};
  Array.Sort(a);
  //a is now changed to be 1,2,3


  string s = "Hello world";
  s.Replace("Hello", "Goodbye");
  //s still says "Hello world"; Replace() returned us a new string saying "Goodbye world" but we didn't capture it

  s = s.Replace("Hello", "Goodbye");
  //s now says Goodbye world

Remember what I said about involving an =...
..and if your operation doesn't involve an =, put it on a line on its own and see if there is an error. If there is then that statement will not change the variable on its own
 
Last edited:
Well, there are some logical errors with your code, try this code to fix this error.

C#:
using System;

namespace JumpStatements
{
    class Program
    {
        static void Main(string[] args)
        {
            bool buttonClick = false;
            int alarm = 0;
            
            do
            {
                Console.WriteLine("BLARRRRR");
                alarm++;
                if (alarm == 3)
                {
                    break;
                }
            } while (!buttonClick);
        }
    }
}

Thanks
 
@gulshan212 : What's the logic error that was in the original post that has been fixed by the code in post #8? I must be missing it because I'm trying to read on my phone and have to keep scrolling up and down
 
Well, there are some logical errors with your code, try this code to fix this error.

C#:
            bool buttonClick = false;
            ...
           
            do
            {
                ...
            } while (!buttonClick);
        }
    }
}

Thanks
If you're going to do that you might as well just do 'while(true)'
 
@gulshan212 : These are the only differences between the original code, and your code:
1693064270276.png


Where are the logical errors that you claimed that you fixed? It looks like all you did was apply some better formatting of the code.
 
Hi, all
There are not logical nor compiler errors in @Jannis1366 's code. I think nobody here noticed that when the variable alarm reaches the value 3 the program abandons the do while loop with the break statement. This can make it seem that !buttonClick is false, and that's why buttonClick is true, but this is not real. If the break statement was not into the loop, the loop would iterate eternally.
I think that if you tell us what exactly you want to do, we might help you better.
Pablo
 

Latest posts

Back
Top Bottom