In this section, we will delve into the fundamental concepts of operators and expressions in RPG programming. Understanding these concepts is crucial as they form the basis for performing calculations, making decisions, and manipulating data within your programs.
Key Concepts
- Operators: Symbols that perform operations on variables and values.
- Expressions: Combinations of variables, values, and operators that yield a result.
Types of Operators
- Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | A + B |
- |
Subtraction | A - B |
* |
Multiplication | A * B |
/ |
Division | A / B |
** |
Exponentiation | A ** B |
- Relational Operators
Relational operators are used to compare two values.
Operator | Description | Example |
---|---|---|
= |
Equal to | A = B |
<> |
Not equal to | A <> B |
> |
Greater than | A > B |
< |
Less than | A < B |
>= |
Greater than or equal to | A >= B |
<= |
Less than or equal to | A <= B |
- Logical Operators
Logical operators are used to combine multiple conditions.
Operator | Description | Example |
---|---|---|
AND |
Logical AND | A AND B |
OR |
Logical OR | A OR B |
NOT |
Logical NOT | NOT A |
- Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assign | A = B |
+= |
Add and assign | A += B |
-= |
Subtract and assign | A -= B |
*= |
Multiply and assign | A *= B |
/= |
Divide and assign | A /= B |
Practical Examples
Example 1: Arithmetic Operations
DCL-S A INT(10); DCL-S B INT(10); DCL-S Result INT(10); A = 10; B = 5; Result = A + B; // Result is 15 Result = A - B; // Result is 5 Result = A * B; // Result is 50 Result = A / B; // Result is 2 Result = A ** B; // Result is 100000
Example 2: Relational Operations
DCL-S A INT(10); DCL-S B INT(10); DCL-S IsEqual IND; A = 10; B = 5; IsEqual = (A = B); // IsEqual is *OFF IsEqual = (A <> B); // IsEqual is *ON IsEqual = (A > B); // IsEqual is *ON IsEqual = (A < B); // IsEqual is *OFF IsEqual = (A >= B); // IsEqual is *ON IsEqual = (A <= B); // IsEqual is *OFF
Example 3: Logical Operations
DCL-S A IND; DCL-S B IND; DCL-S Result IND; A = *ON; B = *OFF; Result = A AND B; // Result is *OFF Result = A OR B; // Result is *ON Result = NOT A; // Result is *OFF
Example 4: Assignment Operations
DCL-S A INT(10); DCL-S B INT(10); A = 10; B = 5; A += B; // A is 15 A -= B; // A is 10 A *= B; // A is 50 A /= B; // A is 10
Practical Exercises
Exercise 1: Basic Arithmetic
Write a program that calculates the sum, difference, product, and quotient of two numbers.
DCL-S Num1 INT(10); DCL-S Num2 INT(10); DCL-S Sum INT(10); DCL-S Difference INT(10); DCL-S Product INT(10); DCL-S Quotient INT(10); Num1 = 20; Num2 = 4; Sum = Num1 + Num2; Difference = Num1 - Num2; Product = Num1 * Num2; Quotient = Num1 / Num2; DSPly ('Sum: ' + %Char(Sum)); DSPly ('Difference: ' + %Char(Difference)); DSPly ('Product: ' + %Char(Product)); DSPly ('Quotient: ' + %Char(Quotient));
Exercise 2: Relational and Logical Operations
Write a program that checks if a number is within a specified range and if it is even.
DCL-S Num INT(10); DCL-S IsInRange IND; DCL-S IsEven IND; Num = 15; IsInRange = (Num >= 10 AND Num <= 20); IsEven = (Num MOD 2 = 0); DSPly ('Is in range: ' + %Char(IsInRange)); DSPly ('Is even: ' + %Char(IsEven));
Solutions
Solution 1: Basic Arithmetic
DCL-S Num1 INT(10); DCL-S Num2 INT(10); DCL-S Sum INT(10); DCL-S Difference INT(10); DCL-S Product INT(10); DCL-S Quotient INT(10); Num1 = 20; Num2 = 4; Sum = Num1 + Num2; Difference = Num1 - Num2; Product = Num1 * Num2; Quotient = Num1 / Num2; DSPly ('Sum: ' + %Char(Sum)); DSPly ('Difference: ' + %Char(Difference)); DSPly ('Product: ' + %Char(Product)); DSPly ('Quotient: ' + %Char(Quotient));
Solution 2: Relational and Logical Operations
DCL-S Num INT(10); DCL-S IsInRange IND; DCL-S IsEven IND; Num = 15; IsInRange = (Num >= 10 AND Num <= 20); IsEven = (Num MOD 2 = 0); DSPly ('Is in range: ' + %Char(IsInRange)); DSPly ('Is even: ' + %Char(IsEven));
Common Mistakes and Tips
-
Mistake: Using the wrong operator for the intended operation.
- Tip: Double-check the operator you are using to ensure it matches the operation you want to perform.
-
Mistake: Forgetting to initialize variables before using them in expressions.
- Tip: Always initialize your variables to avoid unexpected results.
-
Mistake: Misunderstanding operator precedence.
- Tip: Use parentheses to explicitly define the order of operations in complex expressions.
Conclusion
In this section, we covered the various types of operators and how to use them in expressions. We also provided practical examples and exercises to reinforce your understanding. Mastering these concepts is essential for performing calculations, making decisions, and manipulating data in your RPG programs. In the next section, we will explore control structures, which will allow you to control the flow of your programs based on conditions and loops.
RPG Programming Course
Module 1: Introduction to RPG Programming
Module 2: Core Concepts
Module 3: Working with Data
Module 4: Advanced Programming Techniques
Module 5: RPG IV and Beyond
Module 6: Integrating RPG with Modern Technologies
Module 7: Real-World Applications
- Building a Simple Application
- Case Study: Inventory Management System
- Case Study: Payroll System
- Best Practices and Code Review