CARDIAC › Tips and tricks › Comparisons
Don't test what you just set
Say you're looking for the first card that's 7 or more, and want to print it if it's there.
A common way to write this sets a "found" flag when you find the card, leaves the loop, and then tests the flag afterwards to work out how the loop ended.
cell contents notes
10 030 INP 30 cells 10-14: read cards until one is 7 or more, or a negative one ends them
11 130 CLA 30
12 317 TAC 17
13 731 SUB 31
14 310 TAC 10
15 133 CLA 33 cells 15-16: found: set the flag and leave the loop
16 632 STO 32
17 132 CLA 32 cells 17-20: after the loop, test the flag
18 320 TAC 20
19 530 OUT 30
20 900 HRS 00
30 000
31 007
32 -001 the flag: -1 not found, 0 found
33 000
Step through the natural way in Cardigan
But at the moment you find the card, you already know you found it: the loop's own test just told you. So do the work right there and skip the flag entirely. Whenever you're about to set something and then jump to a test of it, jump straight to the answer instead.
cell contents notes
10 030 INP 30 cells 10-16: read cards until one is 7 or more, or a negative one ends them
11 130 CLA 30
12 316 TAC 16
13 731 SUB 31
14 310 TAC 10
15 530 OUT 30 found: act on it here, where the loop's own test already said so
16 900 HRS 00
30 000
31 007
Step through the CARDIAC way in Cardigan
Both print 9. The flag version takes 15 cells and runs 21 instructions; acting on the spot takes 9 cells and runs 17.
From the tips in
cardiac-code on GitHub. All the tips.