When working with Uniface development, one of the fundamental control structures you'll encounter is the repeat/until
loop. With the help of AI, I've put together this comprehensive guide to help you understand and implement these loops effectively in your applications. π
π― What is a Repeat/Until Loop?
The repeat/until
statement in Uniface creates a loop that executes one or more ProcScript instructions repeatedly until a specified condition becomes TRUE. Unlike other loop types, this guarantees that the code block executes at least once before the condition is evaluated.
π Basic Syntax
repeat
... one or more ProcScript instructions ...
until (Condition)
π§ Key Parameters and Features
- Condition: A Boolean expression that must evaluate to TRUE to exit the loop
- Data Type Conversion: Uniface automatically converts any data types in the expression to boolean
- Status Independence: The repeat statement doesn't affect
$status
- Universal Usage: Allowed in all Uniface component types
β‘ Advanced Loop Control
You can enhance your loops with these features:
- Use the
break
statement to terminate loops early πͺ - Nest conditional statements up to 32 levels deep π
- Combine with
if/endif
andwhile/endwhile
statements
π‘ Practical Example
Here's a real-world example that searches through entity occurrences:
; F2.E1 is a character string field containing a name
; loop until field matches or setocc is out of occurrences
; field: NAMEDUMMY.E1
if (F2.E1 != NAMEDUMMY)
vCurrOcc = $curocc
$NAME$ = NAMEDUMMY
i = 1
repeat
setocc "E1", i
i = (i + 1)
until ((f2.E1 = $NAME$) | ($status < 0))
if ($status < 0)
message "%%$NAME$%%% is not available."
setocc "E1", vCurrOcc
return (-1)
endif
endif
π What This Example Does:
- Stores the current occurrence position
- Loops through all entity occurrences
- Compares field values until a match is found
- Handles cases where no match exists
- Restores the original occurrence position if needed
π Best Practices
- Indent properly: Make your code readable by indenting statements between
repeat
anduntil
π - Guard against infinite loops: Always ensure your condition can eventually become TRUE β οΈ
- Use meaningful variable names: Make your loop counters and conditions self-documenting π
- Consider break statements: Use them strategically for early loop termination π―
π Conclusion
The repeat/until
loop is a powerful tool in your Uniface development arsenal. Whether you're processing entity occurrences, validating user input, or implementing complex business logic, understanding this control structure will make your code more efficient and readable.
Happy coding! π
This article is based on the official Uniface Documentation 10.4
Top comments (0)