Is it possible to have something like below in a Makefile.am?
if CONDITION_1 and CONDITION_2
...
endif
Currently I have something like below in my configure.ac
AM_CONDITIONAL([COMBINED_CONDITION],
[test "$cond_1" != "no" -a "$cond_2" != "no"])
And use it as below in my Makefile.am.
if COMBINED_CONDITION
...
endif
I am fairly new to autotools and couldn't get the ANDed condition to work and want to know if it is at all possible. So far I have not been able to find any reference that suggests it is.
I would use nested if ... endif like
if CONDITION_1
if CONDITION_2
...
endif
endif
to implement the pseudo code
# pseudo code
if CONDITION_1 and CONDITION_2
...
endif
The same logic applies for the other three pseudo code conditions
if CONDITION_1 and not(CONDITION_2)
if not(CONDITION_1) and CONDITION_2
if not(CONDITION_1) and not(CONDITION_2)
which can be implemented as e.g.
if CONDITION1
if !CONDITION2
...
endif
endif
However, the negation of a complete boolean expression cannot be expressed by nested if ... endif. So if you need to implement OR logic
# pseudo code
if CONDITION_1 or CONDITION_2
...
endif
you cannot apply De Morgan's law and do something purely with Automake if ... else ... endif because you cannot formulate a combined else branch. So you will need to define specially formulated AM_CONDITIONAL for an OR condition, or any other more complicated condition than a bunch of ANDs.
As an aside regarding the combined conditional, I recall -a and -o being listed as a non-portable argument to test and therefore
AM_CONDITIONAL([COMBINED_CONDITION_AND],
[test "x$cond_1" != xno && test "x$cond_2" != xno])
AM_CONDITIONAL([COMBINED_CONDITION_OR],
[test "x$cond_1" != xno || test "x$cond_2" != xno])
being recommended over
AM_CONDITIONAL([COMBINED_CONDITION_AND],
[test "$cond_1" != "no" -a "$cond_2" != "no"])
AM_CONDITIONAL([COMBINED_CONDITION_OR],
[test "$cond_1" != "no" -o "$cond_2" != "no"])
(See https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.71/html_node/Limitations-of-Builtins.html)
Note also the letter at the beginning of "x$cond" in the test arguments. This avoids unwanted results if e.g. the value of $cond is -z or -f or something similar which test would interpret. You can use any letter you want here, but "x" is a popular choice.
If you know what the values of the CONDITION_1 and CONDITION_2 variables will be when they are what you want you can just check them both for equality, something like:
ifeq ($(CONDITION_1)/$(CONDITION_2),no/no)
or similar.
Related
Will every && run before if on the same line in Ruby?
For example:
#building.approved = params[:approved] if xyz && abc && mno...
Can an unlimited number of && be used on the right side of an if without using parentheses?
I'm inclined to use parentheses but I'd like to understand the default behaviour.
Everything after the if must be part of the condition by virtue of the syntax. The only way to get around this is to be really specific:
(#building.approved = params[:approved] if xyz) && abc && ...
Which is obviously not what you're intending here.
Operator binding strength isn't an issue here since if is a syntax element not an operator, so it has the absolute lowest priority.
The only conditions that will be evaluated are the ones that produce a logically false value, or come after one that was logically true as the first one to return logically false will halt the chain.
That is:
if a && b && c
Will stop at a if that is a logically-false value. b and c will not be evaluated. There's no intrinsic limit on chaining though.
I prefer to avoid nested if but I wonder if there's time difference between these two :
if(a && b)
{
...
}
or
if(a)
{
if(b)
{
...
}
}
They're exactly identical.
However it depends on your compiler , you have not mentioned the language you will be using.
But most of the compiler will interpret these both code into same thing
example for java
Should if(a&&b) take more time than if(a) if(b)?
example for c++
Nested if statements and "&&" operator
It is the same.
After a gives true in (a && b && c && ...), b is tested, then c and so on – one by one until some expression in chain will result false. After it happens all subsequent expressions will be skipped. As far as I know it works the same way in all C-like languages.
Your code will be more readable if you use && operator instead of nesting many if blocks.
I'm working on a project. Currently I have a fairly large conditional statement, that assigns a value to a variable based on some input parameters. So, I have something like this.
if some condition
x = some value
elsif another condition
x = a different value
...
What's the best way to refactor this? I'm hoping that I might end up with something like
x = some value if some condition || another value if another condition
Is there a pattern for this sort of thing?
Just put the assignment outside the if.
x = if some condition
some value
elsif another condition
a different value
Or you could use a Hash.
x = dict[some condition]
It's not a pattern, but an operator. The one you're referring to is the ternary operator:
If Condition is true ? Then value X : Otherwise value Y
Here is an example:
speed = 90
speed > 55 ? puts("I can't drive 55!") : puts("I'm a careful driver")
Using the ternary statement is short, sweet, and does the job.
x = some condition ? some value :
another condition ? a different value : ...
A conditional statement is also an expression, so one of the first things you can do, if the variable is the same in each condition, is:
x = if cond1
expr1
elsif cond2
expr2
....
end
If the conditions are all states of a single expression, you can make this even neater, using a case statement.
However, the next most obvious re-factoring exercise is to get the big conditional isolated into a method, which should be fed the bare minimum data required to evaluate all the conditions and expressions.
E.g.
# Where conditional is currently, and x assigned, assuming the conditionals
# need a couple of variables . . .
x = foo param1, param2
# Elsewhere
private
def foo p1, p2
if cond1
expr1
elsif cond2
expr2
....
end
end
If you want to refactor for code clarity and flexibility, consider the replacing conditional with polymorphism refactor.
There's not enough detail in your question to go much further with recommendations, but this refactor will make your code base much more resistant to change. If you receive a new requirement, it's bad form to break open the conditional and modify it (more prone to introducing bugs, more difficult to do); it's preferable to create a new object that you can plug into the existing codebase. This flexibility what the Open/Closed Principle (the "O" in the SOLID acronym) describes.
Most for loops have the syntax:
for(initializer; condition; incrementer) {
// code
// code
}
If theres only one line of code, it may follow this syntax:
for(initializer; condition; incrementer)
// code
Or
for(initializer; condition; incrementer) // code
So, my question is, how does this,
for(initializer; condition; incrementer)
;
Or this,
for(initializer; condition; incrementer);
behave? ; is a valid statement in many programming languages. So, does ; at the end of the for loop signify that the loop should keep looping with no statements to execute, or is the ; considered the statement to execute and loops this ; statement until the loop terminates?
In C-like languages (really the only place this makes sense), your second description is the correct one: the empty statement is executed as the loop body.
can anyone recommend software (preferably for mac) or a web based tool, that can be used to evaluate logic expressions?
For example I would like to be able to quickly test whether two expressions like:
$a = 'foo';
$b = 'bar';
$c = 'foo';
( !(($a == $c) && ($b == $c)) )
// and
( ($a != $c) || ($b != c$) )
are interchangeable or not.
And also, is there generally an agreed upon best practice in relation to how to construct such expressions? For example to try to minimize the use of the negation, the order of the elements or something like that?
Sometimes I find myself struggling a bit with these things :)
You can also use Wolfram Alpha
https://www.wolframalpha.com/input/?i=P+%26%26+(Q+%7C%7C+R)&lk=3
or
https://www.dcode.fr/boolean-expressions-calculator
You can use something like http://www-cs-students.stanford.edu/~silver/truth/ and compare the generated truth tables.
I like this site which does exactly what you are looking for, but it only supports limited logical operators:
https://electronics-course.com/boolean-algebra