DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”„ Mastering Counter-Based Loops in Uniface: The for...endfor Statement

When working with Uniface, one of the most fundamental control structures you'll encounter is the counter-based loop. Today, let's dive deep into the for...endfor statement and explore how it can make your ProcScript more efficient and readable! πŸ’ͺ

This article is based on the official Uniface Documentation 10.4 and was created with the assistance of AI to ensure accuracy and completeness.

πŸ“‹ Basic Syntax

The for...endfor statement follows this structure:

for Counter = StartValue to EndValue
  {step StepValue}
  Your ProcScript
endfor
Enter fullscreen mode Exit fullscreen mode

🎯 Key Parameters

Parameter Data Type Description
Counter Number Current value of the counter
StartValue Number Initial value of the counter
EndValue Number End value of the counter
StepValue Number Step size (default: 1)

βš™οΈ How It Works

The loop continues executing until one of these conditions is met:

  • πŸ”Ό StepValue >= 0 and Counter > EndValue
  • πŸ”½ StepValue < 0 and Counter < EndValue
  • πŸ›‘ A break statement is encountered

πŸ’‘ Practical Example

Here's a real-world example that demonstrates the flexibility of the for...endfor loop:

variables
 numeric vCounter
 numeric vLoops
endvariables

vLoops = 0
for vCounter = 100 to 0 step -2
 vLoops += 1 
 putmess "Counter: %%vCounter, Loop count: %%vLoops "
 if (vLoops >= 6) 
 putmess "Loop processing stopped" 
 break
 endif
endfor
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output:

Counter: 100, Loop count: 1
Counter: 98, Loop count: 2
Counter: 96, Loop count: 3
Counter: 94, Loop count: 4
Counter: 92, Loop count: 5
Counter: 90, Loop count: 6
Loop processing stopped
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Advanced Features

Dynamic Loop Control

One of the most powerful features is that you can modify the Counter, EndValue, and StepValue within the loop itself! This allows for dynamic loop behavior based on runtime conditions. πŸŽ›οΈ

Infinite Loops

⚠️ Important: If you don't specify an EndValue, the loop runs forever and must be terminated with a break statement.

πŸŽͺ Loop Variants in Uniface

The for...endfor statement is part of a family of loop constructs in Uniface:

  • forentity...endfor - Entity-based loops
  • forlist...endfor - List-based loops
  • forlist/id...endfor - ID-based list loops

πŸ“š Best Practices

  • βœ… Always consider using break statements for conditional exits
  • βœ… Be mindful of infinite loops when omitting EndValue
  • βœ… Use descriptive counter variable names
  • βœ… Consider performance implications for large ranges

πŸŽ‰ Conclusion

The for...endfor statement in Uniface provides a robust and flexible way to implement counter-based loops. Its ability to handle both ascending and descending counters, combined with dynamic loop control, makes it an essential tool in your Uniface development toolkit! πŸ› οΈ

Have you used for...endfor loops in your Uniface projects? Share your experiences in the comments below! πŸ’¬


Happy coding! πŸš€

Top comments (0)