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
π― 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
andCounter > EndValue
- π½
StepValue < 0
andCounter < 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
π€ 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
π§ 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)