I want check for a condition in makefile using ifeq, & not sure how to go about:
ifeq ( cond1 = yes || cond2 = yes )
set value x = 1;
else
set value x = 2;
endif
Please suggest the proper way to do it?
In addition to the correct answer given above:if you want to check if x=4 or x=6
ifeq ($(x),$(filter $(x),4 6))
x is either 4 or 6. do whatever you like with it
else
x is neither 4 nor 6
endif
ifeq ($(filter $(cond1) $(cond2),yes),)
x := 2
else
x := 1
endif
Alternate answer is:
ifneq (,$(filter yes,$(cond1) $(cond2)))
x := 1
else
x := 2
endif
Related
I'm new to programming and I'm trying to program something but there's some kind of syntax error which I can't work out. Any help would be much appreciated. Here's my code:
begin
puts"Enter a number to count, or to exit type 0."
y = gets.chomp.to_i
if y == 0
exit
end
puts"Now put the number you're starting with"
x = gets.chomp.to_i
if y + x == 12 or y + x < 12
print x + y
end
if y + x > 12
n = y + x - 12
end
begin
if n < 12 or n == 12
print n
end
if n > 12
n = n - 12
end
end until if n < 12 or n == 12
end until y == 0
end
Your use of until if if wrong. They are each control sequences. You shouldn't need both.
Your n is not visible later in the code. Declare n=0 for example before if y + x > 12 to make it visible and accessible in the relevant code blocks.
Then, until if is wrong, this should simply be until
Lastly, delete the last end keyword.
Are := and += same in Linux Kernel Makefiles and could be used interchangeably? If not, then what is the difference between the two ?
As others say, := is assignment.
But there's a fine difference between := and =. In most cases it doesn't matter, but it may make a big difference.
X = $(Y) defines X as a recursive variable, which is something like a C preprocessor macro.
Whenever X is referenced, the value of Y will be used.
The expansion happens when X is expanded. So you can define Y after you've defined X, and it's OK.
X := $(Y) defines X as a simple variable. This is more like a C assignment.
Now, Y is expanded at the time of definition, so changing it later will do nothing.
X += $(Y) appends to X, but keeps its type.
If X was previously defined with =, Y will not be expanded immediately. Same if X was never defined.
If X was previously defined with :=, Y will be expanded immediately.
You can try this exmaple makefile:
X = 1
Y = $(X)
Z := $(X)
X = 2
Y += $(X)
Z += $(X)
X = 3
test:
#echo "Y = $(Y)"
#echo "Z = $(Z)"
It prints
Y = 3 3
Z = 1 2
:= is similar to =, i. e it is an assignment.
+= is a concatenation-and-assignment operator.
Example:
VARIABLE := abc
# here VARIABLE is abc
VARIABLE := def
# VARIABLE is now def only!
VARIABLE_2 = abc
# VARIABLE_2 is now abc
VARIABLE_2 += def
# VARIABLE_2 is abc def
:= is assignment. += is concatenation. See here.
:= clears the previous value of the variable you are assigning to,whereas
+= adds (concatenates) to the variable
For eg, lets say CFLAGS has been set to -Wundef
If we do,
CFLAGS := -Wall
CFLAGS is set to '-Wall' now
If we do,
CFLAGS += -Wall
CFLAGS now becomes '-Wundef -Wall'
It is quite often used in Linux Makefiles.
I need to calculate odd/even sum, here is what I've got so far:
PROGRAM EvenOddSum;
USES
WinCrt;
VAR
odd, even, x: INTEGER;
BEGIN
WriteLn('Calculation of sum');
WriteLn;
odd := 0;
even := 0;
Write('Enter value(s)');
WHILE x > 0 DO BEGIN
IF x mod 2:= 0 THEN BEGIN
even := even + x;
ELSE
odd := odd + x;
ReadLn(x);
END;
WriteLn;
WriteLn('Even sum is = ', even);
WriteLn('Odd sum is =', odd);
END.
I use freepascal.org compiler and I get this error :
SYNTAX error THEN expected but := found
And I just can't see the problem with this code.
In Pascal, := is the assignment operator. Replace it with = on the line that reads IF x mod 2:= 0 THEN BEGIN.
Also, remove the BEGIN. The result should read:
IF x mod 2 = 0 THEN
It's in here:
IF x mod 2:= 0 THEN BEGIN
The := is used for assignment, use '=' or '==' for comparison.
(Off the top of my head, I don't know if Pascal uses '=', '==', or both for comparisons. One of them should do the trick).
If am not wrong, := is used for declaring & assigning the value. For condition, you should use = .
change x mod 2:= 0 to x mod 2 = 0
You could say:
x mod 2 > 0 then writeln(x);
This will print all the odd numbers.
Is there a way without using logic and bitwise operators, just arithmetic operators, to flip between integers with the value 0 and 1?
ie.
variable ?= variable will make the variable 1 if it 0 or 0 if it is 1.
x = 1 - x
Will switch between 0 and 1.
Edit: I misread the question, thought the OP could use any operator
A Few more...(ignore these)
x ^= 1 // bitwise operator
x = !x // logical operator
x = (x <= 0) // kinda the same as x != 1
Without using an operator?
int arr[] = {1,0}
x = arr[x]
Yet another way:
x = (x + 1) % 2
Assuming that it is initialized as a 0 or 1:
x = 1 - x
Comedy variation on st0le's second method
x = "\1"[x]
Another way to flip a bit.
x = ABS(x - 1) // the absolute of (x - 1)
int flip(int i){
return 1 - i;
};
Just for a bit of variety:
x = 1 / (x + 1);
x = (x == 0);
x = (x != 1);
Not sure whether you consider == and != to be arithmetic operators. Probably not, and obviously although they work in C, more strongly typed languages wouldn't convert the result to integer.
you can simply try this
+(!0) // output:1
+(!1) // output:0
You can use simple:
abs(x-1)
or just:
int(not x)
This loop does not terminate after I type x. I'm really new to Ruby, and so far, it is so much different than what I learned before - quite interesting,
total = 0
i = 0
while ((number = gets) != "x")
total += number.to_i
i += 1
end
puts "\nAverage: " + (total / i).to_s
Any help is greatly appreciated.
Because gets gives you the newline as well. You need to chomp it.
Try:
while ((number = gets.chomp) != "x")
and you'll see it starts working:
pax> ruby testprog.rb
1
5
33
x
Average: 13