DEV Community

Peter + AI
Peter + AI

Posted on

πŸ”€ Mastering Conditional Logic in Uniface: The if/endif Block Guide

Hey developers! πŸ‘‹ Today we're diving into one of the fundamental building blocks of Uniface ProcScript - the if/endif conditional block. This comprehensive guide is based on the official Uniface documentation 10.4, and I had some assistance from AI to put this together for you.

πŸš€ What is the if/endif Block?

The if/endif block in Uniface allows you to execute ProcScript conditionally based on logical expressions. Think of it as your decision-making tool in code! πŸ€”

πŸ“ Basic Syntax

if ( Condition 1 )
   ... one or more ProcScript statements ... 
{elseif ( Condition 2 ) 
   ... one or more ProcScript statements ... 
elseif ( Condition n )
   ... one or more ProcScript statements ... } 
{else
   ... one or more ProcScript statements ... } 
endif
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Key Components

Clause Description
if Defines a condition and marks the ProcScript to be executed if the condition is met
elseif Defines an alternative condition and marks the ProcScript to be executed if this condition is met
else Marks the ProcScript to be executed if none of the previous conditions have been met
endif Marks the end of the if conditional block

⚑ How It Works

The magic happens through sequential evaluation! ✨ Each condition is evaluated in order, starting with the if statement. When a condition evaluates to TRUE, the corresponding ProcScript statements are executed, and the rest of the block is skipped.

If no conditions are met and there's an else clause, those statements will execute. No else clause means no execution if all conditions fail.

🎯 Practical Examples

Example 1: Conditional Printing πŸ–¨οΈ

Here's how you can use if/endif for conditional printing operations:

trigger leavePrinted 
 AMOUNT.SUBTOTAL = (AMOUNT.SUBTOTAL + INVAMOUNT)
 if (INVDATE != $next(INVDATE)) ; if next invoice date different
 printbreak "SUBTOTAL" ; print "SUBTOTAL" break frame
 AMOUNT.SUBTOTAL = 0 ; set subtotal to 0
 eject ; start printing on next page
 else
 skip ; print next line empty
 endif
end; leavePrinted
Enter fullscreen mode Exit fullscreen mode

Example 2: Alphabetic Range Detection πŸ”€

This example shows how to determine the alphabetic range of a field:

$1 = NAME[1:1]
if ($1 < "M")
 message "NAME starts A-L"
elseif ($1 > "M")
 message "NAME starts N-Z"
else
 message "NAME starts M"
endif
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Pro Tips

  • Nesting Power: You can nest conditional statements up to 32 levels deep! πŸ—οΈ
  • Single-Line Shortcuts: For single ProcScript statements, you can write them on the same line as the condition
  • Multiple elseif: Use as many elseif clauses as you need
  • Boolean Conversion: Uniface automatically converts data types to boolean for condition evaluation

πŸŽͺ Where Can You Use It?

The best part? The if/endif block is allowed in all component types in Uniface! This makes it incredibly versatile for any scenario you encounter.

πŸš€ Conclusion

Mastering the if/endif block is essential for any Uniface developer. It's your gateway to creating intelligent, responsive applications that can make decisions based on data and user interactions. 🎯

Happy coding, and may your conditions always evaluate as expected! πŸ˜„

Top comments (0)