I've recently come across this type of scenario using if/let
and understand what it does thanks to this post. To my understanding, both conditions need to be met before the proceeding block is executed. I now have come to a point where I've seen it in a regular conditional statement:
if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil {
return false
} else {
return true
}
What's the difference between doing the above and simply using &&
as seen below?:
if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
return false
} else {
return true
}
The comma operator (,) allows you to evaluate multiple expressions wherever a single expression is allowed. The comma operator evaluates the left operand, then the right operand, and then returns the result of the right operand. First the left operand of the comma operator is evaluated, which increments x from 1 to 2.
Posted December 12, 2007 by constant-content in Freelance Writing Tips. “If, then” statements require commas to separate the two clauses that result. If I use correct punctuation, then I will include commas where necessary.
The programmer has used the comma operator to provide two unrelated expressions in a single statement. Because it's a single statement, both expressions are "inside" the if condition.
The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.
There does not appear to be a difference between using &&
for grouping conditionals and using commas. I too have so far only seen it used with optional binding, but apparently it also works for regular conditionals, as the following snippet works fine.
let bool1 = true;
let bool2 = true;
if bool1 , bool2 {
print("true");
} else {
print("false")
}
The comma is used when optional binding with boolean conditions, for example if let a = a, a.isValid()
whereas &&
is a typical AND
operator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With