I'm trying to do a multiple check of some values but I can't get it to work.
if DerobModel.HouseProperties.IntValue['VolumeNorth'] = 1 then
ivol[2] := 1;
else
if DerobModel.HouseProperties.IntValue['VolumeEast'] = 1 then
ivol[2] := 1;
end
else
if DerobModel.HouseProperties.IntValue['VolumeWest'] = 1 then
ivol[2]:=1;
end;
I want to check if "VolumeNorth" is 1, and then the index ivol[2] is going to be 1, otherwise if East is 1 ivol[2] should be 1, and if this isn't there's a check for West and also so for South (This isn't in the code yet). If none of them are 1 then nothing should happen with ivol[2]. How do I get this to work?
Thanks!
Why don't use boolean or?
if (DerobModel.HouseProperties.IntValue['VolumeNorth'] = 1) or
(DerobModel.HouseProperties.IntValue['VolumeEast'] = 1) or
(DerobModel.HouseProperties.IntValue['VolumeWest'] = 1) or
(DerobModel.HouseProperties.IntValue['VolumeSouth'] = 1) then
ivol[2] := 1;
if any of volume (North, East, West, South) is 1 set ivol[2] to 1; if none of volumes are equal to 1 nothing is happen (pay attention, there's no else block)
Dmitry's answer is right for your scenario. I'm also an advocate of using BEGIN and END with all IF statements, for code clarity. It then helps you understand your program flow better.
if DerobModel.HouseProperties.IntValue['VolumeNorth'] = 1 then
begin
ivol[2] := 1;
end
else if DerobModel.HouseProperties.IntValue['VolumeEast'] = 1 then
begin
ivol[2] := 1;
end
else if DerobModel.HouseProperties.IntValue['VolumeWest'] = 1 then
begin
ivol[2]:=1;
end;
Related
After several hours of investigations and researching the problem, I've found that TFormatSettings returns an incorrect ShortTimeFormat.
To show in TDateTimePicker a short time format with support for 24-hours, I need to use: TDateTimePicker.Format :='H:mm', and this is a default setting for my profile in Windows 10 for a short time.
But TFormatSettings.ShortTimeFormat return me a value of 'h:mm'.
To get the correct value, I should use:
GetLocaleStr(LOCALE_USER_DEFAULT, LOCALE_SSHORTTIME, '');
And this returns a 'H:mm' value.
This is the source of TFormatSettings from SysUtils.pas:
TimePrefix := '';
TimePostfix := '';
if StrToIntDef(GetLocaleStr(Locale, LOCALE_ITLZERO, '0'), 0) = 0 then
HourFormat := 'h'
else
HourFormat := 'hh';
if StrToIntDef(GetLocaleStr(Locale, LOCALE_ITIME, '0'), 0) = 0 then
if StrToIntDef(GetLocaleStr(Locale, LOCALE_ITIMEMARKPOSN, '0'), 0) = 0 then
TimePostfix := ' AMPM'
else
TimePrefix := 'AMPM ';
Result.ShortTimeFormat := TimePrefix + HourFormat + ':mm' + TimePostfix;
Result.LongTimeFormat := TimePrefix + HourFormat + ':mm:ss' + TimePostfix;
As we can see, that always use 'h' or 'hh', no way to get 'H' or 'HH'.
My question is: Why?
I have an example of a code and not sure what way is the best to use.
For example I have
if (x = 1) and (y = 2) and (if abc = false then check if z = 3) then
begin
...
check only
if x = 1
if y = 2
if abc = false check z = 3. if abc = true then dont check z = 3
i am not sure if i am explaining the best but hopefuly people will understand.
I want to know if this is possible or the best way to do it. Keeping in mind that rather than in example where its x, y, z and abc. there can be more in my use.
I currently have structure as...which i dont think is practical, and think theres a better way but i am not sure
if (abc = false) then
begin
if (x = 1) and (y = 2) and (z = 3) then
begin
...
end
else
begin
if (x = 1) and (y = 2) then
begin
...
Thanks in advance
I think you're looking for or. Now you will check that x must be 1, y must be 2, and if abc is false, z must be 3.
If abc = true, z can still be three, but it won't be checked.
Note that I just wrote abc instead of abc = true. Since it's a Boolean (true/false) already, that's allowed.
Also note how the operations are grouped using parentheses. The total sub-expression abc or (z=3) must return true for the total expression to return true.
Furthermore the sequence of the terms is significant - they are evaluated left-to-right. If the term (abc or (z=3)) is replaced by the logically-equivalent term ((z=3) or abc) then z=3 will be evaluated.
if (x = 1) and (y = 2) and (abc or (z = 3)) then
// Your magic goes here
Test program body to prove sequence is important
function z : Integer;
begin
writeln('Z being evaluated');
result := x + y;
end;
begin
x := 1;y := 2;
abc := true;
if (x=1) and (y=2) and (abc or (z=3)) then
writeln ('evaluated true')
else
writeln ('evaluated false');
writeln('done');
readln;
end.
Neither of your code samples compile, because neither is using the proper syntax.
This should get you started:
if (x = 1) and (y = 2) then
begin
if (abc) then
// Handle abc = True
else
begin
if (z = 3) then
// Handle abc = false and z = 3
else
// Handle abc = false and z <> 3
end;
end;
I have a problem with translating VHDL to Verilog.
It's part of my source code on VHDL.
With I/O I somehow understood, but have some problems to translate this string
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
to verilog?
COMPONENT GenerateModel
PORT(
ib1 : IN std_logic_vector(3 downto 0);
);
END COMPONENT;
--Inputs
signal ib1 : std_logic_vector(3 downto 0) := (others => '0');
BEGIN
uut: GenerateModel PORT MAP (
ib1 => ib1,
);
process
begin
for i in 0 to 15 loop
ib1 <= std_logic_vector(to_unsigned(i,ib1'length));
wait for 10 ns;
end loop;
end process;
end;
To extend into Verilog from Paebbels' comment, the line you are looking at does an explicit conversion from the type of the loop variable i to the port variable ib1. In Verilog, that explicit conversion is not needed, you can just assign the port variable directly. So, for example (in Verilog IEEE 1364-1995 compatible):
integer i;
...
for (i = 0; i < 16; i = i + 1) begin
ib1 = i; // <-- The line
#10; // -- Assume 1 step is 1 ns, can specific timescale if needed
end
If you want, you can even loop through the variable directly if its of type reg (ie, not a net):
for (ib1 = 0; ib1 < 15; ib1 = ib1 + 1) begin
#10;
end
#10;
[Note that as Greg mentioned, you need to be sure you dont create an infinite loop as if ib1 is 4-bits wide, it will always be less than 16, thus I fixed the example above to loop until ib1 is 15 (4'b1111)]
I get number webcams with using this code:
CountCamers := 0;
j := 0;
capture := cvCreateCameraCapture(700);
while Assigned(capture) do
begin
inc(CountCamers);
cvReleaseCapture(#capture);
capture := nil;
inc(j);
capture := cvCreateCameraCapture(700 + j);
end;
But, sometimes this code give at result number webcams equal 100 (max number camera of domain), but in reality only one webcam is connected. How to get number webcams? Thanks in advance.
When there is only 1 camera, the index is unused (you can pass -1).
Try instead to check the identity of the opaque struct pointer returned. I think (sorry, not tested because I have just 1 camera attached) that should be unique for each device.
You can to get number of webcams checking if you can to get a Frame. Example in python:
def get_num_cameras():
n = 0
num_cameras = 0
while n < 100:
camera = CaptureFromCAM(n)
if QueryFrame(camera):
num_cameras += 1
n += 1
return num_cameras
I found that a nested loop fails when some particular condition is reached, somehow when I = 1, J = 3 and k = 5
I tried to right click on the breakpoint and in the condition I set
(I = 1) and (J = 3) AND (K = 5)
anyway the breakpoint doesn't stop...
What is wrong?
I've just tried that in D2007 and it works fine. what version are you using?
procedure TForm85.FormClick(Sender: TObject);
var i,j,k : integer;
z:integer;
begin
for i := 0 to 10 do
for j := 0 to 10 do
for k := 0 to 10 do
BEGIN
z := z + i * j * k; // breakpoint on this line.
END;
ShowMessage(IntToStr(z));
end;
Have you considered that the breakpoint may not be reached because the condition is not being met?
You did add the breakpoint as a Breaking breakpoint I assume.
To verify this
open the Breakpoint properties window
click on Advanced
make sure the Break checkbox is checked.
May be according to your code
(I = 1) and (J = 3) AND (K = 5)
may never get this values at same time
Set breakpoint on a line of code before the condition is met and step through with F8?