Expressions, Precedence, and Assignment, and Increment/Decrement Operators
AAHC

Click on A to make all fonts on the page smaller.

Click on A to make all fonts on the page larger.

Click on HC to toggle high contrast mode. When you move your mouse over some bold words in high contrast mode, related words are automatically highlighted. Text is shown in black and white.


In the previous lesson, we introduced the concept of a programming statement. In this lesson, we'll further deconstruct statements into programming expressions, as well as explore C# mathematical programming, mathematical expressions, and the increment and decrement operators.

Programming Expressions

If you've studied math, you're probably familiar with the concept of an expression. A C# programming expression consists of C# language elements that may be combined to create a meaningful computation. Programming expressions produce, or return, a result. These expressions may consist of other expressions. Below is a list of C# expressions. Note that an expression does not need to be a complete programming statement.

Assignments and Assignment Shortcuts
Simple Assignment Operator

Previously, we've used the assignment operator, the equals sign (=), in a number of different programming circumstances. The assignment operator is used to assign the result of an expression to a variable or other C# construct that accepts assignment. The equals sign is known as the simple assignment operator, as there are other, more complex assignment operators. The simple assignment operator, as with the other assignment operators, require that the left side of the equals sign, known as the left operand, consist of a C# element that accepts assignment, such as a variable.

Compound Assignment Operators

In addition to the simple assignment operator, C# also includes these compound assignment operators:

  • +=: Compound addition operator
  • -=: Compound subtraction operator
  • *=: Compound multiplication operator
  • /=: Compound division operator
  • %=: Compound modulus operator
  • &=: Compound bitwise AND operator
  • |=: Compound bitwise OR operator
  • ^=: Compound bitwise XOR operator
  • <<=: Compound shift left operator
  • >>=: Compound shift right operator

The compound assignment operators are shortcuts. For example, the += operator is a shortcut for the expression x = x + 5.

Let's explore these cool shortcuts!

Mathematical Compound Assignment Operators

We will add form controls and code that will allow us to display a grid of different sizes.

Create a new Visual Studio Windows Forms Application by selecting File | New | Project. Change the Name of the project to CompoundAssignments, leaving all other dropdowns and checkboxes with their default settings, and click OK:

Locate and click on the entry for Form1.cs and replace it with CompoundAssignments.cs.

Click on the Form1 titlebar, and find the Text property in the Properties Window. Change the Text property to Compound Assignments. Click to save your changes.

 

Drag a GroupBox control from the Toolbox onto your form, and resize the GroupBox to take up all of the horizontal space at the top of the form. Add a second GroupBox control from the Toolbox, and place it below the first GroupBox, also taking up all of the horizontal space on the form. Your form should look like this:

For each of the two GroupBoxes, in the Properties Window, clear the Text property. Your form should now look like this:

For the two GroupBoxes, in the Properties Window, change the Name properties to settingsGroupBox and drawGroupBox, respectively. Save your changes .

 

Next, we'll add Label and ComboBox controls. The ComboBox will be a DropDownList that will allow us to select the grid size, such as 4x4, 10x10, etc.

NoteA ComboBox can have one of three styles, set in the DropDownStyle property: Simple, DropDown, and DropDownList.

Drag a Label control onto the settingsGroupBox. In the Properties Window, change the Text property to Grid Size:. In the top menu, select Format | Center in Form | Vertically, to center the label vertically in the GroupBox. Save your changes .

   

Drag a ComboBox control onto the settingsGroupBox and reduce its width:

In the top menu, select Format | Center in Form | Vertically to center the combobox vertically in the GroupBox. In the Properties Window, change the Name property to gridSizeComboBox. Find and select the Items property and click on the ellipsis (...). In the String Collection Editor dialog box that appears, enter the numbers 4, 10, and 20, each on a separate line, and click OK. Remember to .

   

Now we'll add code to our project, so we'll need to view the Code Editor for the Form.

Right-click the CompoundAssignments.cs entry in the Solution Explorer, and select View Code. The default Windows Form code for your project appears in the Code Editor:

We'll change our code so that any time a user changes the selected item in the gridSizeComboBox, the grid is automatically drawn in the drawGroupBox.

First, let's add the code to respond to whenever the user changes the selected item in the gridSizeComboBox. This event is the SelectedIndexChanged.

Select the gridSizeComboBox, then at the top of the Properties Window, click the Events icon . Locate the SelectedIndexChanged event handler, and double-click on the row. You'll again see the code editor, with the new gridSizeComboBox_SelectedIndexChanged() event handler method added. Save your changes .

Next, we need to handle the Paint event for the drawComboBox control. The Paint event is called every time a request is made to draw (paint) the contents of a control.

Select the drawGroupBox, then in the Properties Window, click the Events icon . Locate the Paint event handler, and double-click on the row. You'll again see the code editor with the new drawGroupBox_Paint() event handler method handler added. Save your changes .

Modify CompoundAssignments.cs as shown below to add code that uses the event handlers and selects the first entry in the gridSizeComboBox. (Again, we've hidden the using section by clicking the minus (-) icon to its left.)

CompoundAssignments.cs
using ...

namespace CompoundAssignments
{
    public partial class CompoundAssignments : Form
    {
        public CompoundAssignments()
        {
            InitializeComponent();
            
            // Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
        }
        
        private void DrawGrid(Graphics drawingArea)
        {
        }
        
        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }
            
        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            // Call the base class OnPaint event to ensure any other controls that need to be redrawn are also redrawn.
            base.OnPaint(e);
            // Call our draw grid method.
            DrawGrid(e.Graphics);
        }
    }
}

This program will not yet draw the grid because we haven't added code in the DrawGrid method. Instead, the code we've added prepares everything we need to have the grid be drawn:

OBSERVE: CompoundAssignments.cs
.
.
.
        public CompoundAssignments()
        {
            InitializeComponent();
            
            // Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
        }
        
        private void DrawGrid(Graphics drawingArea)
        {
        }
        
        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }
        
        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            // Call our draw grid method.
            DrawGrid(e.Graphics);
        }
.
.
.

The gridSizeComboBox.SelectedIndex = 0; code selects the first value in the gridSizeComboBox. The drawGroupBox.Refresh(); code in the gridSizeComboBox_SelectedIndexChanged event handler will force the drawGroupBox to redraw itself, which will call the drawGroupBox_Paint method. The code in the drawGroupBox_Paint method will then call DrawGrid. From previous lessons, you may remember that to draw, we need to get a Graphics object. The drawGroupBox_Paint method includes a PaintEventArgs parameter, e. The PaintEventArgs parameter includes a reference to the drawing surface or area of the drawGroupBox control, and we use that reference, e.Graphics, as a parameter to our DrawGrid method.

Now, modify CompoundAssignments.cs as shown below to draw the grid.

NoteAs you type the new code, note the extensive use of comments. Think about the comments to help you understand the process and implementation.

CompoundAssignments.cs
using ...

namespace CompoundAssignments
{
    public partial class CompoundAssignments : Form
    {
        public CompoundAssignments()
        {
            InitializeComponent();
            
            // Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
        }
        
        private void DrawGrid(Graphics drawingArea)
        {
            float gridMargin = 10.0F;       // Margin to keep grid away from group box edge.
            int gridSize = 0;               // Grid size, i.e. 4x4, 10x10, etc.
            
            // Try to convert the grid size in the combo box to an integer.
            if (Int32.TryParse(gridSizeComboBox.Text, out gridSize))
            {
                // Make sure we have a grid size > 0 before we attempt to draw grid.
                if (gridSize > 0)
                {
                    // Create a pen object for drawing.
                    Pen pen = new Pen(Color.Red, 2);
                    
                    // Use the smaller of the drawing area width or height to ensure a uniform grid spacing.
                    float gridWidth = drawingArea.VisibleClipBounds.Width;
                    if (drawingArea.VisibleClipBounds.Height < gridWidth)
                        gridWidth = drawingArea.VisibleClipBounds.Height;
                    
                    // Subtract the margin value.
                    gridWidth -= gridMargin * 2;
                    
                    // Determine grid cell width.
                    float cellWidth = gridWidth / gridSize;
                    
                    // Set up starting x and y coordinates for both horizontal and vertical lines.
                    float horizontalX = gridMargin;
                    float horizontalY = 0;
                    float verticalX = 0;
                    float verticalY = gridMargin;
                    
                    // The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    // drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        // Draw horizontal lines.
                        Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        
                        // Draw vertical lines.
                        Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                    }
                }
            }
        }
        
        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }
        
        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            // Call the base class OnPaint event to ensure any other controls that need to be redrawn are also redrawn.
            base.OnPaint(e);
            // Call our draw grid method.
            DrawGrid(e.Graphics);
        }
    }
}

Whew, that was a lot of typing! Trust me, it's worth it. and to run the program. You should see something like this:

Let's discuss how this code works.

OBSERVE: CompoundAssignments.cs
.
.
.        
        private void DrawGrid(Graphics drawingArea)
        {
            float gridMargin = 10.0F;       // Margin to keep grid away from group box edge.
            int gridSize = 0;               // Grid size, i.e. 4x4, 10x10, etc.
            
            // Try to convert the grid size in the combo box to an integer.
            if (Int32.TryParse(gridSizeComboBox.Text, out gridSize))
            {
                // Make sure we have a grid size > 0 before we attempt to draw grid.
                if (gridSize > 0)
                {
                    // Create a pen object for drawing.
                    Pen pen = new Pen(Color.Red, 2);
                    
                    // Use the smaller of the drawing area width or height to ensure a uniform grid spacing.
                    float gridWidth = drawingArea.VisibleClipBounds.Width;
                    if (drawingArea.VisibleClipBounds.Height < gridWidth)
                        gridWidth = drawingArea.VisibleClipBounds.Height;
                    
                    // Subtract the margin value.
                    gridWidth -= gridMargin * 2;
                    
                    // Determine grid cell width.
                    float cellWidth = gridWidth / gridSize;
                    
                    // Set up starting x and y coordinates for both horizontal and vertical lines.
                    float horizontalX = gridMargin;
                    float horizontalY = 0;
                    float verticalX = 0;
                    float verticalY = gridMargin;
                    
                    // The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    // drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        // Draw horizontal lines.
                        Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        
                        // Draw vertical lines.
                        Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                    }
                }
            }
        }
.
.
.       

We'll split the explanation of the DrawGrid method into two sections: first describing overall how the code works, and then explaining the compound assignments we're learning in this section of the lessons. First, the overall explanation:

To draw the grid of lines, we use a for loop to iterate over each line we'll need to draw. Because we're drawing a square grid, the number of lines to be drawn is the same both vertically and horizontally. All we need to do is set the initial starting point for each line, vertical and horizontal, then add the width of each cell, cellWidth, to the starting point as we increment through each grid line. We use the VisibleClipBounds method of the drawingArea object to determine our drawing surface dimensions and the best width for the grid, gridWidth, and the DrawLine method to perform the actual line drawing. We also ensure that we can see the edge grid lines by creating a margin, gridMargin, and adding that value to the starting coordinates of our line, and subtracting it from our ending coordinates.

Within the DrawGrid method, we use compound assignments three times. The first use is:

gridWidth -= gridMargin * 2

This subtracts two margin values from the available space for the grid, and is equivalent to:

gridWidth = gridWidth - gridMargin * 2

The second compound assignment is:

horizontalY += cellWidth

This increments the horizontal starting coordinates by the width of each grid cell, and is equivalent to:

horizontalY = horizontalY + cellWidth

The last compound expression is:

verticalX += cellWidth

This increments the vertical grid line starting coordinates by the width of each cell, and is equivalent to:

verticalX = verticalX + cellWidth

Before moving on, let's also modify our code to use the modulus operator (%). Remember, the modulus operator returns the remainder of a division operation. The modulus operator is very useful when you want to perform a specific action at a specific frequency, such as every even number, every odd number, or every number divisible by ten. We'll modify our code to show a different grid line color for every odd grid line.

Modify CompoundAssignments.cs as shown below to display a different color for each odd-numbered grid line.

CompoundAssignments.cs
using ...

namespace CompoundAssignments
{
    public partial class CompoundAssignments : Form
    {
        public CompoundAssignments()
        {
            InitializeComponent();
            
            // Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
        }
        
        private void DrawGrid(Graphics drawingArea)
        {
            float gridMargin = 10.0F;       // Margin to keep grid away from group box edge.
            int gridSize = 0;               // Grid size, i.e. 4x4, 10x10, etc.
            
            // Try to convert the grid size in the combo box to an integer.
            if (Int32.TryParse(gridSizeComboBox.Text, out gridSize))
            {
                // Make sure we have a grid size > 0 before we attempt to draw grid.
                if (gridSize > 0)
                {
                    // Create a pen objects for drawing.
                    Pen penredPen = new Pen(Color.Red, 2);
                    Pen bluePen = new Pen(Color.Blue, 2);
                
                    // Use the smaller of the drawing area width or height to ensure a uniform grid spacing.
                    float gridWidth = drawingArea.VisibleClipBounds.Width;
                    if (drawingArea.VisibleClipBounds.Height < gridWidth)
                        gridWidth = drawingArea.VisibleClipBounds.Height;
                    
                    // Subtract the margin value.
                    gridWidth -= gridMargin * 2;
                    
                    // Determine grid cell width.
                    float cellWidth = gridWidth / gridSize;
                    
                    // Set up starting x and y coordinates for both horizontal and vertical lines.
                    float horizontalX = gridMargin;
                    float horizontalY = 0;
                    float verticalX = 0;
                    float verticalY = gridMargin;
                    
                    // The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    // drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        // Select pen, red for even, blue for odd.
                        Pen pen = bluePen;
                        if (i % 2 == 0)
                            pen = redPen;
                    
                        // Draw horizontal lines.
                        Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        
                        // Draw vertical lines.
                        Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                    }
                }
            }
        }
        
        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }
        
        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            // Call the base class OnPaint event to ensure any other controls that need to be redrawn are also redrawn.
            base.OnPaint(e);
            // Call our draw grid method.
            DrawGrid(e.Graphics);
        }
    }
}

and to run the program.

Let's discuss it.

OBSERVE: CompoundAssignments.cs
.
.
.
                // Make sure we have a grid size > 0 before we attempt to draw grid.
                if (gridSize > 0)
                {
                    // Create pen objects for drawing.
                    Pen redPen = new Pen(Color.Red, 2);
                    Pen bluePen = new Pen(Color.Blue, 2);
                    
.
.
.
                    // The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    // drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        // Select pen, red for even, blue for odd.
                        Pen pen = bluePen;
                        if (i % 2 == 0)
                            pen = redPen;
                        
                        // Draw horizontal lines.
                        Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        
                        // Draw vertical lines.
                        Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                    }
                }
.
.
.

First, in place of the single definition we previously had for one red pen, we define our redPen and bluePen. Then we define pen (an instance of Pen), as our bluePen—unless i % 2 == 0, which means we are in an even-numbered iteration of our for loop. This provides for the alternation of colors of pen when we draw the lines.

Bitwise and Shift Compound Assignment Operators

These operators are used to access and manipulate individual bits. What is a bit? A bit is a binary digit, consisting of either 0 or 1. A bit is useful for (among other things) storing the equivalent of false (0) or true (1). The concept of a bit derives from electrical components, where a device could detect the presence of a charge (1), or no charge (0). From this fundamental concept of a bit derives a vast array of computer concepts. For compound assignments, we can use the fact that we can access and manipulate individual bits of a variable.

We will explore using bitwise and shift operators in a later lesson.

C# Operators and Precedence

You may have noted in the previous code, when we expanded one of the compound statements, we had both a subtraction and a multiplication operator within the same statement. How do you know which operator to apply first? From mathematics, you might know that the multiplication operator is used first. The term we use to indicate the correct order to follow is precedence. C# has a very large collection of operators, and thus a large precedence table. You can view it here:

Visual Studio 2010 Precedence Table

From this table, you'll notice that compound assignment operators have less precedence than all of the mathematic operators, and that multiplication has a higher precedence than subtraction, so we would process multiplication expressions first, then subtraction, then the compound assignment operators.

Note You can always enforce a different precedence order using parentheses in your C# expressions.

Increment and Decrement Operators

We've seen the increment and decrement operators before, including in the coding we've just done in this lesson. Where? In the for loop code, the last section is the counting expression, and our code included i++, which increments i by one (i--, of course, would decrement i by one).

The increment and decrement operators we've seen so far represent the postfix versions. The operators follow the expression ("i") they modify. These operators can also be prefix, indicating that the operators precede the expression they modify. Is there a difference between prefix and postfix? Yes. In the case of prefix, the expression is first incremented or decremented, and then the expression is used, whereas with postfix, the expression is used, and then the value is incremented or decremented. Essentially, if you want the value incremented or decremented before you use it, use prefix. If you want to get the current value, then increment following the statement, use postfix. Let's modify our code to explore the difference.

Modify CompoundAssignments.cs as shown below to show the results of using prefix and postfix. Note that the Console.WriteLine statements in DrawGrid have been commented out.

CompoundAssignments.cs
using ...

namespace CompoundAssignments
{
    public partial class CompoundAssignments : Form
    {
        public CompoundAssignments()
        {
            InitializeComponent();
            
            // Select the first item in the combo box.
            gridSizeComboBox.SelectedIndex = 0;
            
            // Prefix
            string prefixValues = "0 ";
            int result = 0;
            int value = 1;
            for (int i = 1; i <= 3; i++)
            {
                result += ++value;
                prefixValues += result + " ";
            }
                
            // Postfix
            string postfixValues = "0 ";
            result = 0;
            value = 1;
            for (int i = 1; i <= 3; i++)
            {
                result += value++;
                postfixValues += result + " ";
            }
            
            Console.WriteLine("Prefix: " + prefixValues);
            Console.WriteLine("Postfix: " + postfixValues);
        }
        
        private void DrawGrid(Graphics drawingArea)
        {
            float gridMargin = 10.0F;       // Margin to keep grid away from group box edge.
            int gridSize = 0;               // Grid size, i.e. 4x4, 10x10, etc.
            
            // Try to convert the grid size in the combo box to an integer.
            if (Int32.TryParse(gridSizeComboBox.Text, out gridSize))
            {
                // Make sure we have a grid size > 0 before we attempt to draw grid.
                if (gridSize > 0)
                {
                    // Create pen objects for drawing.
                    Pen redPen = new Pen(Color.Red, 2);
                    Pen bluePen = new Pen(Color.Blue, 2);
                    
                    // Use the smaller of the drawing area width or height to ensure a uniform grid spacing.
                    float gridWidth = drawingArea.VisibleClipBounds.Width;
                    if (drawingArea.VisibleClipBounds.Height < gridWidth)
                        gridWidth = drawingArea.VisibleClipBounds.Height;
                    
                    // Subtract the margin value.
                    gridWidth -= gridMargin * 2;
                    
                    // Determine grid cell width.
                    float cellWidth = gridWidth / gridSize;
                    
                    // Set up starting x and y coordinates for both horizontal and vertical lines.
                    float horizontalX = gridMargin;
                    float horizontalY = 0;
                    float verticalX = 0;
                    float verticalY = gridMargin;
                    
                    // The number of lines in grid is equal to the size of the grid plus 1, so loop over size of grid + 1,
                    // drawing horizontal and vertical lines.
                    for (int i = 0; i < gridSize + 1; i++)
                    {
                        // Select pen, red for even, blue for odd.
                        Pen pen = bluePen;
                        if (i % 2 == 0)
                            pen = redPen;
                        
                        // Draw horizontal lines.
                        // Console.WriteLine("Horizontal: ({0},{1}) to ({2},{3})", horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        drawingArea.DrawLine(pen, horizontalX, horizontalY + gridMargin, horizontalX + gridWidth, horizontalY + gridMargin);
                        horizontalY += cellWidth;
                        
                        // Draw vertical lines.
                        // Console.WriteLine("Vertical: ({0},{1}) to ({2},{3})", verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        drawingArea.DrawLine(pen, verticalX + gridMargin, verticalY, verticalX + gridMargin, verticalY + gridWidth);
                        verticalX += cellWidth;
                    }
                }
            }
        }
        
        private void gridSizeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Notify the group box control that it should redraw itself, which will call the Paint event below.
            drawGroupBox.Refresh();
        }
        
        private void drawGroupBox_Paint(object sender, PaintEventArgs e)
        {
            // Call the base class OnPaint event to ensure any other controls that need to be redrawn are also redrawn.
            base.OnPaint(e);
            // Call our draw grid method.
            DrawGrid(e.Graphics);
        }
    }
}

and the program. You'll see this:

Let's discuss how this code works.

OBSERVE: CompoundAssignments.cs
.
.
.
// Prefix
string prefixValues = "0 ";
int result = 0;
int value = 1;
for (int i = 1; i <= 3; i++)
{
    result += ++value;
    prefixValues += result + " ";
}

// Postfix
string postfixValues = "0 ";
result = 0;
value = 1;
for (int i = 1; i <= 3; i++)
{
    result += value++;
    postfixValues += result + " ";
}

Console.WriteLine("Prefix: " + prefixValues);
Console.WriteLine("Postfix: " + postfixValues);
.
.
.

As you can see from the output, the prefix ++value increments the value before it is added to result, as illustrated by the number sequence 0 2 5 9, whereas the postfix value++ increments the value after it is added to result, as illustrated by the number sequence 0 1 3 6.

Before you move on to the next lesson, do your homework! Right-click in the window where this lesson text appears and select Back. Then, select Quiz for this lesson in the syllabus and answer the quiz questions. When you finish the quiz questions, click Hand it in at the bottom of that window. Then do the same with the Project(s) for the lesson. Your instructor will grade your quiz(zes) and project(s) and provide guidance if needed.