Multiple Conditions for Swift 'If' Statement? - if-statement

Is there a way to write an If statement is Swift such as the following?
if a>b or c/d {
//Do Something
}

Just like everywhere:
if a > b || d % c == 0 {
// do sth
}
I assume your c/d means you'd like d to be a multiple of c.

Swift uses the same operators as all C-based languages, so
if a > b || c < d {
}
where || is the OR operator, && is the AND operator.
The list of all operators can be found in Swift Basic Operators
Not sure what c/d condition is supposed to mean.

for and operation we can use
if x==y && y == z { //perform some operations }
for or operation we can use
if x==y || y == z { //perform some operations }

Related

What's Syntax about this line of Ternary Operator

I trying many time to understand how to work or what's syntax code it ?
greatest=(a>b&&a>c)?a:(b>c)?b : c;
I know the main syntax of Ternary Operator
but in greatest i don't know how come it
the Required I want to anyone explain me that and give me syntax of them .
thanks.
This operator is essentially similar to these if-else statements
if ( a > b && a > c )
{
greatest = a;
}
else if ( b > c )
{
greatest = b;
}
else
{
greatest = c;
}
To make the expression with two nested conditional operators more clear use parentheses
greatest = ( a > b && a > c ? a: ( b > c ? b : c ) );
You could use the standard algorithm std::max instead of the expression with the two nested conditional operators. For example
greatest = std::max( { a, b, c } );

Shorten If Statement And Making It Less Redundant

New here. I was just wondering if it's possible to make this if statement shorter and less redundant.
if (!a && b)
{
if (c == d && e > 0)
{
return;
}
}
else if (a && !b)
{
if (c != d)
{
return;
}
}
else if (!a && !b)
{
return;
}
Here's what I've ended up with
if ((!a && b && c == d && e > 0) || (a && !b && c != d) || (!a && !b))
{
return;
}
All I did was join nested if statements with an && operator, and if-else if statements with || operator. Now I'm stuck, is it possible to make this even shorter? If you could share some tips or your way of approaching this kind of scenario, I'd be glad to hear it out.
Neither of the approaches is readable. It will be better to create a function that indicates the kinds of checks you are performing.
if ( !my_very_sensible_special_conditions_are_met(a, b, c, d, e) )
{
return;
}
After that, whether you use the first approach or the second approach in the implementation of the function is less of an issue.
condition ladder(if,else if) shortening
As you mentioned, in all blocks(if, else if) if you are just having the same set of statements then you can put all the conditions with a single if statement with proper conditions with appropriate brackets.By doing this lines of code will get reduced But you will have some disadvantages as per my point of view
Readability will get reduced
In future, if you want to change your code for a particular condition, it will take some time to change.it is not that much easy as you do in condition ladder
if you are not having any problem with the above two points, you can go with your approach itself.But code your conditions as atomic as possible.it will help you out in a long run.
Given the number and complexity of conditions, I'd at least consider making it table driven. For the moment, I've assumed that a and b are actually ints containing either a 0 or a 1.
int index = a | (b<<1) | (int(c==d)<<2) | (int(e>0) << 3);
static const bool should_return[] = {
1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
};
if (should_return[index])
return;
The table is still basically incomprehensible, but at least it encodes that incomprehensibility fairly compactly, and makes it easy for the code to use it.
Since you commented that you are always returning, and since your code does not specify if you do anything else in these if - else cases why not do just
if (! (a && b) )
{
return;
}
You can shorten it a tiny bit further:
if (!a && !b || !b && c != d || !a && c==d && e> 0) {
return;
}
But that doesn't increase readability and most likely also not performance. What I would focus on is, what reads most natural with the real variable names.
As a general advice, I'd start with checking the simpler / shorter conditions first. That makes it often more easier to see if you make redundant checks in the following ones.
In any case, as #R.Sahu suggested, a complex condition like that belongs into a separate, named and commented function. If you don't find a good name for it use something generic like e.g. check_<main_function>_preconditions.
EDIT:
Sorry, I can't resist: If you wan to go overboard, you can make it pretty compact by throwing in a trinary statement:
if (!a && !b || c != d ? !b : !a && e> 0) {
return;
}
DON'T do that in production code!

Evaluation of logical condition in ternary operator

I do not know whether parens are required before the ternary operator.
Example:
int a,b,d,e,f; // Some numbers
int l = ( a > b || d < e ) ? a : d;
is surely fine, if a>b or d<e then l = a else l = d.
I am not sure if instead
int l = a > b || d < e ? a : d;
is equally valid, and what about longer statements?
int l = ( a > b || d ) && e > f ? a : d;
I guess that my question is: the ternary operator always treat whatever comes before him as a logical expression and evaluates it, and therefore there is no need for parens before it?
Your first example is correct.
And the second statement is equally valid to the first one.
Your second example reads as follows:
(...) have highest precedence and > and < have higher precedence than && which has higher precedence than || which has higher precedence than ?:.
So
if the result of a > b || d (d is converted to 1 if different than 0) is true
and
if the result of e > f is true
then
l=a
otherwise l=b.

Comparison of int values in an if statement

I had a question in my test paper in which we had to compare the values of int type variables. The first thought that came to my mind was that it was missing the && operator but i am not sure.
int a=2, b=2, c=2;
if(a==b==c)
{
printf("hello");
}
I have a doubt, will the above statement will execute or not in c or c++? Can i have the reason as well.
Thank You
It will execute but with what I believe unexpected results to you.
One of the == will evaluate to a boolean value, which will then be converted to an int and then the second comparison will be performed, comparing an int to either 1 or 0.
The correct statement is a==b && b==c.
For example:
3 == 3 == 3
evaluates to
true == 3
1 == 3
false
a==b==c
is equivalent to
(a == b) == c
The result of a == b is 1 (if true) or 0 (if false), so it will probably not achieve what you expect.
Use a == b && b == c to check if the value of the three objects are equal.
a == b == c is a comparison between c and result of a==b (1 or 0) operation.
use a==b&&b==c.
the condition a==b==c is equivalent to (a==b)==c which will provide the required result iff c==1, else the code will fail.

Variable x equals a or b

Is this:
if(x == a || b){//do something}
same as:
if(x == a || x == b){//do something}
?
I think it is not, because in the first case we evaluate if x equals a and if b is true or false.
In the second case, we evaluate if x equals a and if x equals b. And I understand that in the lazy evaluation if x equals a than we are not evaluating further.
But somebody thinks that in the first case we ask if x equals a or b, so I wanna make sure.
No.
In C++, this:
x == a || b // Same as (x == a) || b
Is equivalent to this:
(x == a) || (bool)b
Which evaluates to true if x and a are equal OR if b evaluates to true when converted to bool. In C, on the other hand, it is equivalent to this:
(x == a) || (b != 0)
Which evaluate to true if x and a are equal OR if b is different from 0 (here we must make the implicit assumption that b is of integral type, otherwise this won't compile).
On the other hand, this:
(x == a || x == b) // Same as ((x == a) || (x == b))
Evaluates to true when either x and a are equal OR x and b are equal (i.e., if x is either equal to a or equal to b) both in C++ and in C.
The two expressions are not equivalent. This
if(x == a || b)
is the equivalent of
if( (x == a) || (b))
i.e an OR of x==a and b. In C++, if b evaluates to anything other than 0 or false, it is taken as true.
The second one tests whether x==b instead of simply testing b.
No. In C this is equivalent to:
if(x == a || b != 0)
The first reads as "if x is equal to a, or if b is truthy"
The second reads as "if x is equal to a, or if x is equal to b"
No.
if (x == a || b)
is equal to
if ((x == a) || (bool)b)
because operator == has higher precedence than operator ||. See Operator Precedence Table.
You are almost right, the first case means x equals a OR b is true.
Lazy evaluation means that the expression will be evaluated only until the result is obvious. In an OR expression, for example (x || y), the result will be known when x==true – then the whole expression must be true too. In the case of AND, like (x && y), the result will be evident when x==false. So you are right, if x==a, we know the answer already and no more work is needed.