Reputation: 65
bool paint = false;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
paint = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
paint = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (paint) //part i am confused about
{
SolidBrush solidBrush = new SolidBrush(Color.LightBlue);
graphics.FillEllipse(solidBrush, e.X, e.Y, Convert.ToInt32(toolStripTextBox1.Text), Convert.ToInt32(toolStripTextBox1.Text));
}
}
I was watching a tutorial and a guy just did something like this. What does an if statement with only one thing mean?
Upvotes: 1
Views: 82
Reputation: 415665
Let's go back to the basics. When looking at a code file (and skipping past any header section or comments for this purpose), there are several layers of structure.
At the outer layer we have blocks. These are the sections of code between braces ({
and }
characters). You can also have implicit single-statement blocks.
Inside of blocks we have statements. These are the things which usually end with a semi-colon (;
), but can also include control statements/block headers like if
, while
, return
, etc. Think of a statement as a complete "line" of code, even though occasionally a statement might span multiple lines of text in the file, or even multiple statements in the same line of text.
We can strip off another layer and look inside statements. Here we find expressions. An expression is a part of a statement that can be evaluated. For example, take this statement:
a = b + (c - d);
Here we can say c - d
is an expression. We can also say b
by itself is an expression. But b +
is not a complete expression, because the +
operator is incomplete. Neither is b + (c
, because we've broken the parentheses.
There are a few different types of expression:
a == b
, a > b
, etc)a + b
)a()
).Here's the thing: I lied. Conditional expressions don't really exist as a separate category. All conditional expressions are actually value expressions, where the result value in question is a boolean. All that if()
, while()
, etc really about is getting a boolean expression. Therefore, every time you see code along these lines:
if (a == b)
You can re-write it like this:
bool c = (a == b);
if (c)
Because c
is still a boolean expression on it's own. a == b
is really just an expression with a boolean result, and you can put any boolean expression in it's place.
For completeness I need to mention assignment. A few languages treat assignment as void expressions. Others treat assignment as a value expression, where the result is value is which was assigned (C# does this). A small number treat assignment as a value expression where the result is a boolean true
if the assignment succeeds and false
otherwise.
Upvotes: 0
Reputation: 881243
It means that paint
should be treated as something that's already a boolean value (true or false).
That is, after all, the type of value that you get from an expression line age < 18
.
As an aside, for better code readability, I actually prefer English-style names for variables so would opt for something like
shouldPaint
orisPainting
in this case.
In this particular snippet, pressing the mouse button down sets paint
to true while releasing the button sets it to false. When you move the mouse, it uses this value to decide whether or not to draw something. In other words, it draws only when the mouse button is being held down.
Upvotes: 3
Reputation: 37472
From the documentation:
An
if
statement identifies which statement to run based on the value of a Boolean expression. (...)
The Boolean expression is the expression in the parenthesis after if
. And a Boolean (bool
) variable (or member, etc.) well is a Boolean expression. It evaluates to just the value of the variable (or member, etc.). So if and only if the value of it is true
, the if
branch is followed.
Upvotes: 1