# simple loop

How to make code below to not consider into "if" statment variables b,c,d,e,g if them value is zero?
example:
b=0
``````If not a=0 then begin
If (a<=b) and (a<=c) and  (a<=d) and (a<=e) and (a<=g) then
richeditWriteOKcursor:=true;
end;
``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
So, you want the conditional to fail if any of your variables are zero?
``````If not a=0 then begin
If ((b<>0) and (a<=b)) and ((c<>0) and (a<=c)) and  ((d<>0) and (a<=d)) and ((e<>0) and (a<=e)) and ((g<>0) and (a<=g)) then
richeditWriteOKcursor:=true;
end;
``````
0
Author Commented:
No.
My loop must not work with variable which value is zero.

0
Author Commented:
if b=0,c=0,d=0,e=0

``````If not a=0 then begin
if  (a<=g) then
richeditWriteOKcursor:=true;
end;
``````
0
EngineerCommented:
Do you need to check ALL the variables first then for 0?
``````If ( a = 0) or ( b = 0) or ( c = 0) or ( d = 0) or ( e = 0) then exit;

If (a<=b) and (a<=c) and  (a<=d) and (a<=e) and (a<=g) then
richeditWriteOKcursor:=true;
``````
0
Author Commented:

I must compare variable "a" to all other variables which value is not zero.
0
Author Commented:
If (a<=b) and (a<=c) and  (a<=d) and (a<=e) and (a<=g) then
richeditWriteOKcursor:=true;

Variable  richeditWriteOKcursor must be also true if b,c,d,e,g are zero.
0
EngineerCommented:
Then if you only need to check a, your very first example was almost correct.

``````if not ( a = 0) then
if ( ( b = 0) or ( c = 0) or ( d = 0) or ( e = 0)) or
( ( a <= b) and ( a <= c) and ( a <= d) and ( a <= e)) then
richeditWriteOKcursor:=true;
end;
``````
0
EngineerCommented:
You said "Variable richeditWriteOKcursor must be also true if b,c,d,e,g are zero"
Maybe it might be this instead, just a little hard to understand your exact logic.
``````if not ( a = 0) then
if ( ( b = 0) and ( c = 0) and ( d = 0) and ( e = 0)) or
( ( a <= b) and ( a <= c) and ( a <= d) and ( a <= e)) then
richeditWriteOKcursor:=true;
``````
0
EngineerCommented:
Or if you want to remove the second "if" statement
``````if not ( a = 0) then
richeditWriteOKcursor := ( ( b = 0) and ( c = 0) and ( d = 0) and ( e = 0)) or ( ( a <= b) and ( a <= c) and ( a <= d) and ( a <= e))
``````
0
Author Commented:
Variable richeditWriteOKcursor must be true when a is biger than any other variable but  it mast consider only variables which are not equal to zero.
0
Author Commented:
It must exclude variables which are zero from comparision.
0
EngineerCommented:
Then what Ghoti_AZ wrote should do the job by changing them to OR
``````if not ( a = 0) then
if ( ( b <> 0) and ( a <= b)) or
( ( c <> 0) and ( a <= c)) or
( ( d <> 0) and ( a <= d)) or
( ( e <> 0) and ( a <= e)) or
( ( g <> 0) and ( a <= g)) then
richeditWriteOKcursor := true;
``````
0
EngineerCommented:
In fact I just noticed "Variable richeditWriteOKcursor must be true when a is biger than any other variable"
``````if not ( a = 0) then
if ( ( b <> 0) and ( a > b)) or
( ( c <> 0) and ( a > c)) or
( ( d <> 0) and ( a > d)) or
( ( e <> 0) and ( a > e)) or
( ( g <> 0) and ( a > g)) then
richeditWriteOKcursor := true;
``````
0
Commented:
Personally, I hate big complex conditionals. It's always hard for me to figure out at a glance what's going on. How about using a simple function to take some of the complexity out:

function OK(a, x: integer): boolean;
begin
result := (x <> 0) and (a > x);
end;

then the main part would go:

RichEditWriteOKCursor := (a <> 0) and OK(a,b) or OK(a,c) or OK(a,d) or OK(a,e) or OK(a,g);

or alternatively, a slight longer way (same results):

if a = 0
then RichEditWriteOKCursor := false
else RichEditWriteOKCursor := OK(a,b) or OK(a,c) or OK(a,d) or OK(a,e) or OK(a,g);
0
EngineerCommented:
hee hee I understand where you are coming from JosephGlosz.
Thats why I tend to space out the conditionals like I do and keep like for like together.
0
Oracle dbaCommented:
this will not work faster, with variants it never is faster but ...
you could use this

if you want a to be bigger than all other variables then a is the biggest

function Biggest(Values: Variant): Variant;
var I: Integer;
begin
Result := 0;
for I := VarArrayLowBound(Values, 1)  to VarArrayHighBound(Values, 1) do
if (Values[I] <> 0) and (Values[I] > Result) then
Result := Values[I];
end;

use it like
RichEditWriteOKCursor := (a <> 0) and (a = Biggest(VarArrayCreate([a, b, c, d, e, f])));
0
PresidentCommented:
You may need a slightly different perspective...

if A must be less than or equal to all of the non zero values then it stands to reason that it must be less than or equal to the least of the individual non zero values.  The second statement would be functionally equivalent while changing the means of implementing it.  This is pseudocode, but you should get the idea.

procedure MeEvent(Sender : TObject);
var
Min : integer;

begin
if Min = 0 then
min := val
else
if val < min then
min := val;
end;

richeditWriteOKcursor := false;
If a <> 0 then
begin
min := 0;
{min will only be zero if all are zero}
If (min <> 0) and (a <= min) then
richeditWriteOKcursor:=true;
end;
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:

The code above does what i need.
(when "a" is less (sorry for wrong information before) than other variables and
it looks only variables which are not zero)

thank you
``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Shape1: TShape;
Button1: TButton;
a: TLabel;
b: TLabel;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
richeditWriteOKcursor :bool;
a,b,c,d,e,g,min:  integer;

implementation

{\$R *.dfm}

begin
if Min = 0 then
min := val
else
if val < min then
min := val;
end;

procedure MeEvent();
begin
a:=strtoint(form1.edit1.text);
b:=strtoint(form1.edit2.text);
c:=strtoint(form1.edit3.text);

richeditWriteOKcursor := false;
If a <> 0 then begin
min := 0;

{min will only be zero if all are zero}
If (min <> 0) and (a <= min) then
richeditWriteOKcursor:=true;
if  richeditWriteOKcursor then form1.Shape1.Brush.Color:=clgreen else  form1.Shape1.Brush.Color:=clred;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MeEvent();
end;

end.
``````
0
Oracle dbaCommented:
are you a fun of OO or procedural coding

you are mixing 2 types of coding and using very bad variable / component naming

have you tried your example with a = 1, b = 5, c = 10 ?

min := 0;
If (min <> 0) and (a <= min) then
richeditWriteOKcursor:=true; // True

and with a = 5, b = 10, c = 1
min := 0;
If (min <> 0) and (a <= min) then
richeditWriteOKcursor:=true; // False

a evaluation of your code resulted in this very short piece of code
with no extra functions except from the math unit

``````unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
EditA: TEdit;
EditB: TEdit;
EditC: TEdit;
Shape1: TShape;
Button1: TButton;
lblA: TLabel;
lblB: TLabel;
lblC: TLabel;
procedure Button1Click(Sender: TObject);
private
FRicheditWriteOKcursor :bool;
public

end;

var
Form1: TForm1;

implementation

uses Math;

{\$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const BrushColors: Array[Boolean] of TColor = (clRed , clGreen);  // clRed = False,  clGreen = True
var a, b, c, x: integer;
begin
a := strtoint(editA.text);
b := strtoint(editB.text);
c := strtoint(editC.text);
x := min(a, ifthen(b>0, b, a));
x := min(x, ifthen(c>0, c, x));
FRicheditWriteOKcursor := (x > 0) and (a = x);
Shape1.Brush.Color := BrushColors[FRicheditWriteOKcursor];
end;

end.
``````
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.