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.
I may be an idiot, I'm very tired right now, but why the times 1000 or mod 2? Won't that always be 0? If this is any normal language rounding random will either give 0 or 1 which will go to 1000 or 0 with the multiplication. 1000 % 2 is zero, thus no matter what the output is zero
Am I missing something?
No shade BTW the joke was clear and funny even if I'm right
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 statement to execute literally the same code.~
I'm pretty sure that funky() would always return 0, as defined. I'll pseudocode that up:
funky takes no args, returns int {
a is assigned the value 0
b is assigned the value 1
test if a is equal to 1, if it is {
b is assigned the value 1
}
return a
}
The if in your function can never be reached, without some weird manipulation of the value of a that breaks variable scoping in most syntaxes.
I think that I see your logic but it is syntactically incorrect:
if ( a==1 ) {
b=1
}
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 variable b.
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.
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.
Translating structured logic into spoken language is iffy. (I'm sorry. I couldn't help myself)
The code reads to match OP if stated as: "If and only if the value of 'a' equals 1 then set the value of 'b' to equal one." Placing the conditional at the beginning of the sentence maintains the correct dependency.
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.