C++. Demonstration of the use of the “for” loop in programs. Example of calculating the sum of the series

Demonstration of the use of the “for” loop in programs. Example of calculating the sum of the series

In this work is demonstrated the use of an iterative process for finding the sum of a series. The program is implemented in C++ programming language using Microsoft Visual Studio tools.


The task

Using the loop with the parameter “for” find the sum of the series:


1. Create the project

Run MS Visual Studio. Create the project on template “Visual C++ – Windows Forms Application“. Example of creating the program based on this template is described in detail here.


2. Placement the controls

In accordance with the condition of the problem, the program uses the n variable, which must be entered from the keyboard. Therefore, the form of the program will look like as shown in Figure 1.

Figure 1. Main form of the program

The following controls are placed on the form:

  • two controls of type Label named label1 and label2;
  • control of Button type named button1;
  • control of TextBox type with name textBox1.


3. Setting properties of controls

Example of creating form and setting the properties of the controls is described in detail here.

Set the following properties in controls:

  • in control label1 property Text = “n = “;
  • in button1 property Text = “Calculate“;
  • in Form1 property Text = ” The sum of a series “;
  • in label2 property Text = “The sum of a series = “.

After setting, the shape of the program will take the form as shown in Figure 2.

Figure 2. Main form of application


4. Programming of event clicking on the button “Calculate

When you click on the button “Calculate“, then will be calculated the sum of the series.

In this case, you need to program the event Click of button1 control. A detailed example of event programming is described here.

The code listing the Click event handler is as follows:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
    float summa;
    int i, n;

        if (textBox1->Text == "") throw -1;

        // get the value n
        n = n.Parse(textBox1->Text);

        // the sum calculation
        summa = 0;
        for (i=1; i<=n; i++)
            summa = summa + (float)(1.0/i);

        // display the result
        label2->Text = "The sum of a series = " + summa.ToString();
    catch (int e)
        if (e == -1)
            label2->Text = "Error!";

To calculate the sum of a series is used the “for” loop. General view of the loop:

for (initialization; expression; increment)
    sequence of instructions


  • initialization – assignment statement that sets a loop control variable to an initial value. The loop control variable is a counter. In our case the loop control variable is a variable named i. In accordance to the problem condition, at the beginning the variable i is set to 1;
  • expression – conditional expression in which is verified the value of the variable i. The loop is executed until long as the value of the expression element is the true. In this case, checking is performed i<=n;
  • increment – expression that determines the values change of control variable after each iteration of the loop. In our case, variable is increased by 1.