Practical: Builds a custom `Repeat` loop control structure that executes an action a specified number of times.

ID: 1230

				
					using uCalcSoftware;

var uc = new uCalc();

static void MyRepeat(uCalc.Callback cb) {
   var count = cb.ArgInt32(1);
   var action = cb.ArgExpr(2);
   var i = 0;
   for ( i = 1; i <= count; i++) {
      action.Execute(); // Evaluate the expression on each iteration
   }
}

// Define a counter variable in the engine
uc.DefineVariable("counter = 0");

// The action 'counter++' is passed as an unevaluated expression
uc.DefineFunction("Repeat(count As Int, ByExpr action)", MyRepeat);

// Execute the custom loop
uc.Eval("Repeat(5, counter++)");

Console.Write("Final counter value: ");
Console.WriteLine(uc.Eval("counter"));
				
			
Final counter value: 5
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyRepeat(uCalcBase::Callback cb) {
   auto count = cb.ArgInt32(1);
   auto action = cb.ArgExpr(2);
   auto i = 0;
   for ( i = 1; i <= count; i++) {
      action.Execute(); // Evaluate the expression on each iteration
   }
}
int main() {
   uCalc uc;
   // Define a counter variable in the engine
   uc.DefineVariable("counter = 0");

   // The action 'counter++' is passed as an unevaluated expression
   uc.DefineFunction("Repeat(count As Int, ByExpr action)", MyRepeat);

   // Execute the custom loop
   uc.Eval("Repeat(5, counter++)");

   cout << "Final counter value: ";
   cout << uc.Eval("counter") << endl;
}
				
			
Final counter value: 5
				
					Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyRepeat(ByVal cb As uCalc.Callback)
      Dim count = cb.ArgInt32(1)
      Dim action = cb.ArgExpr(2)
      Dim i = 0
      For i  = 1 To count
         action.Execute() '// Evaluate the expression on each iteration
      Next
   End Sub
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define a counter variable in the engine
      uc.DefineVariable("counter = 0")
      
      '// The action 'counter++' is passed as an unevaluated expression
      uc.DefineFunction("Repeat(count As Int, ByExpr action)", AddressOf MyRepeat)
      
      '// Execute the custom loop
      uc.Eval("Repeat(5, counter++)")
      
      Console.Write("Final counter value: ")
      Console.WriteLine(uc.Eval("counter"))
   End Sub
End Module
				
			
Final counter value: 5