if statement == false
from tatterdemalion@programming.dev to science_memes@mander.xyz on 05 Dec 2024 21:07
https://programming.dev/post/22510734
from tatterdemalion@programming.dev to science_memes@mander.xyz on 05 Dec 2024 21:07
https://programming.dev/post/22510734
threaded - newest
what would be the alternative? to always execute if the condition is true, but sometimes execute it even when false, for funsies?
Hey, I like a little chaos in my codebase đ
is this AI?
Nope. Iâm staunchly against âAIâ. If the code sucks, itâs because I wrote bad code.
Edit: Oh, or did you mean is that how âAIâ works?
.
Wait, why the the *1000 or mod 2? Wonât that give a 50\50 chance or the same as Math.round(Math.random).
No shade, and I may be wrong myself I am very tired
Probably to make the fractional random value between 0 and 1 to become an integer so that you can divisibility check for even with mod 2
Brb, making a truly âifâ statement function in my products code base for funsies.
.
The âonlyâ part implies exclusivity, which may be false, because other things might run the code anyway.
IF âI can see the sunâ THEN âItâs day.â
Nothing wrong about that. However if we make it exclusive:
IF AND ONLY IF âI can see the sunâ THEN âItâs day.â
Thatâs obviously wrong. I can actually not keep the day away by sitting with closed eyes in my mothers basement with the curtains shut.
âOnly ifâ might make sense in a legal contract, but thereâs no way a piece of code can stop other pieces of code from calling the same functions.
But thatâs not how if statements in code work. So what youâve said isnât wrong, but the premise of this meme is completely off
Yes sure. Code is logical stepwise. By including the âonly ifâ it implies that other stuff is taken into account, which it isnât at that moment in code.
I mean, I donât need to extend the implications of an IF statement. It already does exactly what it says.
Anyway fuzzy logic does exist for people who want âsometimes ifâ. Itâs useful in certain cases. Iâve only ever considered it in music production, where things very often get to the point of complexity where it makes a (sometimes) useful difference.
In normal parlance, âif and only ifâ rules out that something could also happen as a result of other circumstances. EG, if you fall out of a plane, you will lose your glasses. But there are other conditions that would lead to the same result.
In code, the alternative would be to have a different if statement that executes identical code. Or *cough* you could use a jump instruction to execute literally the same code.
Not sure what youâre trying to achieve with that else block, as it affects nothing.
okay I fixed it
No, theyâre not.
Letâs assume they are. Let funky function be defined as:
Since a==1 if, and only if, b=1, in particular a==1 if b=1. We have b=1, therefore a==1. It follows funky will always return 1 but⌠it doesnât. QED.
.
.
I agree but itâs also what the original meme is doing. I thought we were all shitposting here.
Iâm pretty sure that
funky()
would always return0
, as defined. Iâll pseudocode that up:The
if
in your function can never be reached, without some weird manipulation of the value ofa
that breaks variable scoping in most syntaxes.I think that I see your logic but it is syntactically incorrect:
In most syntaxes, this is a conditional execution and value assignment. That is, the code in curly braces only gets executed, if the conditional evaluates as true. If the conditional evaluates as true, the code is executed, assigning the value 1 to the variable
b
.It does NOT imply that the assignment of the value 1 to the variable
b
is a conditional requiring the assignment of the value 1 to the variableb
.Remember:
=
in most programming is NOT an equality symbol but a value-assigment symbol. It would be nice if people creating the initial syntaxes used something else that is harder to confuse but they didnât.Yeah, Iâm not sure what the original intent was here. If weâre missing something Iâd like to know
Yes, I know, thatâs the point. Funky is specifically constructed to always return 0. Then we assume âifâ and âif, and only ifâ are equivalent and by following that assumption to its logical conclusion, we deduce that funky returns 1. Therefore, our assumption was incorrect because 0â 1. It follows that âifâ isnât equivalent to âif, and only ifâ. Also, itâs just a shitpost.
If reading the code as non-programming logic, that conclusion makes sense, yes. However,
if
, in most syntaxes, is a type of flow control. What it wraps has no meaning to theif
statement itself. Reading it through the lens of an interpreter/compiler makes it clear. The statement is approximately:To one not familiar with how programs are executed, it would make sense that the return value could be 1. But understanding how flow control works in programming, makes this interpretation a challenge.
I donât think youâre picking up what Iâm putting down. Iâm not arguing that the return value can be 1, Iâm well aware that it canât â I wrote the function so that it will always return 0. It only returns 1 if we make an incorrect assumption (and mix up semantics with formal logic, but thatâs another conversation), the incorrect assumption being âif is equivalent to if, and only ifâ
Sorry! I sometimes get carried away on correctness.
I mean, making an assumption and arriving to a contradiction is as correct as a proof gets.
An
if
statement with one condition is an if and only if statement. The moment you add a second (or more) condition (regardless of&&
or||
), then itâs no longer if and only if.Reminds me of this fun stack exchange q
IFF you use the universal quantifier
The boolean operator âIf and only ifâ do not have a relation with the program instruction âifâ.
The programatic âifâ is a jump, not a boolean operator. It do not have truth table.
In logic:, if and iff can be seen like functions taking two booleans and returning a boolean
âif a then bâ (noted a -> b): return true if a is false or b is true. Example: âif I eat pizza then I fartâ This is true even if I fart all the time (if b is true, we do not care about the value of a) as long as I fart when eating pizza (if a is true, b must be also true)
âa <-> bâ is equivalent to âa -> b and b -> aâ: the two should be true at the same time. I can only fart will eating pizza and cannot fart otherwise.
.
So in programming, youâd write âifâ as:
not pizza or fart
where the farting is irrelevant until the pizza is involved.While âiffâ would be:
pizza equals fart
where pizza means fart and no pizza means no fart.I actually wrote iff as
(not pizza and not fart) or (pizza and fart)
before, and Iâm pretty sure thatâs the way I wrote an iff in production code in the past, but your comment made me realize that âthey should be true at the same timeâ can be tested really easily with equality.If not pizza and not fart: pass
If pizza then fart else !fart
I donât love the pizza fart variable naming convention, but itâs better than foobar and I donât have a better suggestion đ
Donât you just use iif?
If and only if⌠haha unlessâŚ
I hope this memer is not a programmer or logician, but ideally neither.
Waiting for a programming language with
iff
Syntax