2022-10-25
This commit is contained in:
parent
5a68fb5229
commit
369a286ab7
@ -16,6 +16,68 @@ ideas' witlessness;
|
|||||||
ideas' witnesses;
|
ideas' witnesses;
|
||||||
ideas-
|
ideas-
|
||||||
|
|
||||||
|
2022-10-25
|
||||||
|
|
||||||
|
i am logical, if not for time
|
||||||
|
|
||||||
|
In C conditional logic is usually expressed in if statements. The very
|
||||||
|
narrow textbook example of this is thus:
|
||||||
|
|
||||||
|
if (condition) {
|
||||||
|
do_something();
|
||||||
|
} else {
|
||||||
|
do_another_thing();
|
||||||
|
}
|
||||||
|
|
||||||
|
I don't like this. There are a couple of supposed truths within this
|
||||||
|
example that are false:
|
||||||
|
- brackets are necessary for the if statement body (they aren't)
|
||||||
|
- ifs are the only way to perform conditional logic in C (they aren't)
|
||||||
|
this may not be stated outright in the example, but it's implicit in
|
||||||
|
that it's the only way textbooks will show much logic
|
||||||
|
|
||||||
|
This "blah" doesn't exist to express solid facts, just my loose and
|
||||||
|
flimsy opinions and experiences.
|
||||||
|
|
||||||
|
Here are four ways to do something in C that are each functionally
|
||||||
|
identical to each other:
|
||||||
|
|
||||||
|
bool aisfive(bool c, int *a) {
|
||||||
|
if (c == 1) {
|
||||||
|
*a = 5;
|
||||||
|
} else {
|
||||||
|
*a = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool aisfive(bool c, int *a) {
|
||||||
|
if(c)
|
||||||
|
*a = 5;
|
||||||
|
else
|
||||||
|
*a = 6;
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool aisfive(bool c, int *a) {
|
||||||
|
*a = c ? 5 : 6;
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool aisfive(bool c, int *a) {
|
||||||
|
*a = 5 + !c;
|
||||||
|
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
I prefer the bottom-most example but the difference won't matter to a
|
||||||
|
good compiler. To me, algebraic expression is just as good as if-else
|
||||||
|
expression. But I'm an Internet crank that's still programming in C.
|
||||||
|
|
||||||
|
|
||||||
2022-10-24
|
2022-10-24
|
||||||
|
|
||||||
i will twerk now, get in the conga line
|
i will twerk now, get in the conga line
|
||||||
|
Loading…
Reference in New Issue
Block a user