emulating_20the_20c_20assignment_20function
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
emulating_20the_20c_20assignment_20function [2018/03/31 13:19] – external edit 127.0.0.1 | emulating_20the_20c_20assignment_20function [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, July 2009//\\ \\ In the **C** programming language, an // | //by Richard Russell, July 2009//\\ \\ In the **C** programming language, an // | ||
+ | <code c> | ||
variable = expression | variable = expression | ||
+ | </ | ||
can be used either as a // | can be used either as a // | ||
+ | <code c> | ||
while (variable = expression) | while (variable = expression) | ||
{ | { | ||
// Do something useful here | // Do something useful here | ||
} | } | ||
+ | </ | ||
Here **variable** is set equal to the value of **expression** and if its new value is // | Here **variable** is set equal to the value of **expression** and if its new value is // | ||
+ | <code bb4w> | ||
variable = expression | variable = expression | ||
WHILE variable | WHILE variable | ||
Line 14: | Line 19: | ||
variable = expression | variable = expression | ||
ENDWHILE | ENDWHILE | ||
+ | </ | ||
As you can see, this involves writing the assignment statement **twice**, once outside the loop and again inside the loop. This is inelegant and potentially error-prone, | As you can see, this involves writing the assignment statement **twice**, once outside the loop and again inside the loop. This is inelegant and potentially error-prone, | ||
+ | <code bb4w> | ||
DEF FNassign(RETURN variable, expression) | DEF FNassign(RETURN variable, expression) | ||
variable = expression | variable = expression | ||
= variable | = variable | ||
+ | </ | ||
Now you can write the loop as follows:\\ \\ | Now you can write the loop as follows:\\ \\ | ||
+ | <code bb4w> | ||
WHILE FNassign(variable, | WHILE FNassign(variable, | ||
REM Do something useful here | REM Do something useful here | ||
ENDWHILE | ENDWHILE | ||
+ | </ | ||
Note that since **variant** numeric variables are used in the function (i.e. without a ' | Note that since **variant** numeric variables are used in the function (i.e. without a ' |
emulating_20the_20c_20assignment_20function.1522502358.txt.gz · Last modified: 2024/01/05 00:18 (external edit)