How to perform a summation in a loop efficiently using Execute() instead of Evaluate().

ID: 22

				
					using uCalcSoftware;

var uc = new uCalc();
var VariableX = uc.DefineVariable("x = 0");
var Total = uc.DefineVariable("Total");
string Expression = "x++; Total = Total + x^2 + 5";

var ParsedExpr = uc.Parse(Expression);

for (double x = 1; x <= 10; x++) {
   // Execute() runs the parsed expression without the overhead of returning a value.
   // This provides near-native performance for loops across C#, VB, and C++.
   ParsedExpr.Execute();

   // Evaluate string interpolation to output the final calculated total
   Console.WriteLine(uc.EvalStr("$'{x}   Sub total = {Total}'"));
}

Console.WriteLine(uc.EvalStr("$'Total = {Total}'"));

ParsedExpr.Release();
VariableX.Release();
				
			
1   Sub total = 6
2   Sub total = 15
3   Sub total = 29
4   Sub total = 50
5   Sub total = 80
6   Sub total = 121
7   Sub total = 175
8   Sub total = 244
9   Sub total = 330
10   Sub total = 435
Total = 435
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   auto VariableX = uc.DefineVariable("x = 0");
   auto Total = uc.DefineVariable("Total");
   string Expression = "x++; Total = Total + x^2 + 5";

   auto ParsedExpr = uc.Parse(Expression);

   for (double x = 1; x <= 10; x++) {
      // Execute() runs the parsed expression without the overhead of returning a value.
      // This provides near-native performance for loops across C#, VB, and C++.
      ParsedExpr.Execute();

      // Evaluate string interpolation to output the final calculated total
      cout << uc.EvalStr("$'{x}   Sub total = {Total}'") << endl;
   }

   cout << uc.EvalStr("$'Total = {Total}'") << endl;

   ParsedExpr.Release();
   VariableX.Release();
}
				
			
1   Sub total = 6
2   Sub total = 15
3   Sub total = 29
4   Sub total = 50
5   Sub total = 80
6   Sub total = 121
7   Sub total = 175
8   Sub total = 244
9   Sub total = 330
10   Sub total = 435
Total = 435
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      Dim VariableX = uc.DefineVariable("x = 0")
      Dim Total = uc.DefineVariable("Total")
      Dim Expression As String = "x++; Total = Total + x^2 + 5"
      
      Dim ParsedExpr = uc.Parse(Expression)
      
      For x  As Double = 1 To 10
         '// Execute() runs the parsed expression without the overhead of returning a value.
         '// This provides near-native performance for loops across C#, VB, and C++.
         ParsedExpr.Execute()
         
         '// Evaluate string interpolation to output the final calculated total
         Console.WriteLine(uc.EvalStr("$'{x}   Sub total = {Total}'"))
      Next
      
      Console.WriteLine(uc.EvalStr("$'Total = {Total}'"))
      
      ParsedExpr.Release()
      VariableX.Release()
   End Sub
End Module
				
			
1   Sub total = 6
2   Sub total = 15
3   Sub total = 29
4   Sub total = 50
5   Sub total = 80
6   Sub total = 121
7   Sub total = 175
8   Sub total = 244
9   Sub total = 330
10   Sub total = 435
Total = 435