I have to create a button which counts from 0 to 9, and after 9 come 0 again. One of my friends said that this code is not good enough. Can you explain to me why it's a problem to use -1?int szm = 0;private void counterButton_Click(object sender, EventArgs e){    szm++;    this.Text = szm.ToString();    if (szm == 9)    {        szm = -1;    }}
Button incrementing from 0 to 9 and repeating
c#
The only problem with the -1 is that it's a bit harder to follow what the code is doing.You are doing half of an operation by setting the variable to -1, and the operation is only completed when the event handler is called again and the value is increased to 0. By splitting the operation over two executions of the event handler, you have to think two steps further to understand what the code actually does.One way to write the code in clear steps is to increase the variable, then make sure that it's in the range 0..9, and finally display the value. That makes it easy to follow what's happening to the value:int szm = 0;private void counterButton_Click(object sender, EventArgs e){    szm++;    szm %= 10;    this.Text = szm.ToString();}You can of course write that as an if statement if you like:int szm = 0;private void counterButton_Click(object sender, EventArgs e){    szm++;    if (szm == 10) {      szm = 0;    }    this.Text = szm.ToString();}