DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”„ Mastering Repeat/Until Loops in Uniface: A Developer's Guide

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)
Enter fullscreen mode Exit fullscreen mode

πŸ”§ 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 and while/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
Enter fullscreen mode Exit fullscreen mode

πŸ” What This Example Does:

  1. Stores the current occurrence position
  2. Loops through all entity occurrences
  3. Compares field values until a match is found
  4. Handles cases where no match exists
  5. Restores the original occurrence position if needed

πŸ“‹ Best Practices

  • Indent properly: Make your code readable by indenting statements between repeat and until πŸ“
  • 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)