# PeopleCode Evaluate

A question came up today that I have wondered about for a while: how does break and evaluate work in PeopleCode? I think I have been telling it wrong, so I decided to do a little experiment.

Note: This test uses the Unit Test Framework.

In my first series of tests, I wanted to know if the “break” is required.

```import TTS_UNITTEST:TestBase;

class EvalTest extends TTS_UNITTEST:TestBase
method EvalTest();
method Run();
end-class;

method EvalTest
%Super = create TTS_UNITTEST:TestBase("TestBaseTest");
end-method;

method Run
/+ Extends/implements TTS_UNITTEST:TestBase.Run +/

Local string &testVal = "one";

%This.Msg("Test 1");
Evaluate &testVal
When = "one"
When = "two"
%This.Msg("In eval for 'one' and 'two'");
When = "three"
%This.Msg("In eval for 'three'");
End-Evaluate;

%This.Msg("Test 2");
Evaluate &testVal
When = "one";
When = "two";
%This.Msg("In eval for 'one' and 'two'");
When = "three";
%This.Msg("In eval for 'three'");
End-Evaluate;

%This.Msg("Test 3");
Evaluate &testVal
When = "one"
When = "two"
%This.Msg("In eval for 'one' and 'two'");
Break;
When = "three"
%This.Msg("In eval for 'three'");
Break;
End-Evaluate;

end-method;
```

Here’s the output:

```Test 1
In eval for 'one' and 'two'
Test 2
Test 3
In eval for 'one' and 'two'
```

Most importantly, notice that test 1 and test 2 behave exactly the same way. So, the “break” doesn’t seem to make any difference.

Secondly, the semicolons on the “when” lines do make a big difference. Two “when” lines one after another make for an either/or effect. But, if you put a semicolon afterward, PeopleCode interprets that as a statement and breaks the either/or.

Exploring the syntax, I found that the “=” sign is not required. This works just as well:

```   Local string &testVal = "one";

%This.Msg("Test 1");
Evaluate &testVal
When "one"
When "two"
%This.Msg("In eval for 'one' and 'two'");
When "three"
%This.Msg("In eval for 'three'");
End-Evaluate;
```

Here’s another series of examples that proves that you can use more than just the “=” sign. It also shows that two “when” clauses perform the logical “or” operation. I don’t think there is a way to do an “and”.

```
Local number &testVal = 10;

%This.Msg("Test 1");
Evaluate &testVal
When > 5
When < 11
%This.Msg("In eval for '> 5' and '< 11'");
When = 100
%This.Msg("In eval for '= 100'");
End-Evaluate;

%This.Msg("Test 2");
Evaluate &testVal
When > 5
When < 2
%This.Msg("In eval for '> 5' and '< 2'");
When = 100
%This.Msg("In eval for '= 100'");
End-Evaluate;
```

Here’s the output:

```Test 1
In eval for '> 5' and '< 11'
Test 2
In eval for '> 5' and '< 2'
```

I did find a situation where the break does make a difference! If the condition can match multiple “when” clauses, it does make a difference.

```
Local number &testVal = 10;

%This.Msg("Test 1");
Evaluate &testVal
When > 5
%This.Msg("In eval for '> 5'");
When < 100
%This.Msg("In eval for '< 100'");
End-Evaluate;

%This.Msg("Test 2");
Evaluate &testVal
When > 5
%This.Msg("In eval for '> 5'");
Break;
When < 100
%This.Msg("In eval for '< 100'");
Break;
End-Evaluate;
```

Here’s the output:

```Test 1
In eval for '> 5'
In eval for '< 100'
Test 2
In eval for '> 5'
```

And, we can’t forget the other clause.

```
Local number &testVal = 2;

%This.Msg("Test 1");
Evaluate &testVal
When > 5
%This.Msg("In eval for '> 5'");
When > 100
%This.Msg("In eval for '> 100'");
When-Other
%This.Msg("Other!");
End-Evaluate;

%This.Msg("Test 2");
Evaluate &testVal
When > 1
%This.Msg("In eval for '> 5'");
When > 100
%This.Msg("In eval for '> 100'");
When-Other
%This.Msg("Other!");
End-Evaluate;
```

Here’s the output:

```Test 1
Other!
Test 2
In eval for '> 5'
```

## One thought on “PeopleCode Evaluate”

1. Dave says:

Thank you for the excellent write-up. It helped me organize my understanding:

The “;” matters because it means the When has a body. If a When doesn’t have a body then it uses the body of the next When (not When-Other) giving the “OR” functionality.

Evaluate considers each When in order and will execute the body of each matching When encountered. “Break” means leave the Evaluate and stop considering further When clauses.