• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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;

Open in new window

0
pr2501
Asked:
pr2501
  • 7
  • 7
  • 2
  • +3
1 Solution
 
Ghoti_AZCommented:
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;

Open in new window

0
 
pr2501Author Commented:
No.
My loop must not work with variable which value is zero.
 
 
0
 
pr2501Author Commented:
if b=0,c=0,d=0,e=0
 
 
 

If not a=0 then begin
  if  (a<=g) then
        richeditWriteOKcursor:=true;
    end;

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
mikelittlewoodCommented:
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;

Open in new window

0
 
pr2501Author Commented:

I must compare variable "a" to all other variables which value is not zero.
0
 
pr2501Author 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
 
mikelittlewoodCommented:
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;

Open in new window

0
 
mikelittlewoodCommented:
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;

Open in new window

0
 
mikelittlewoodCommented:
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))

Open in new window

0
 
pr2501Author 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
 
pr2501Author Commented:
It must exclude variables which are zero from comparision.
0
 
mikelittlewoodCommented:
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;

Open in new window

0
 
mikelittlewoodCommented:
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;

Open in new window

0
 
JosephGloszCommented:
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
 
mikelittlewoodCommented:
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
 
Geert GOracle 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
 
developmentguruPresidentCommented:
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;

  procedure Adjust(val : 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;
    Adjust(b);
    Adjust(c);
    Adjust(d);
    Adjust(e);
    Adjust(g);
    {min will only be zero if all are zero}
    If (min <> 0) and (a <= min) then
      richeditWriteOKcursor:=true;
  end;
0
 
pr2501Author 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}
  
 
 
  procedure Adjust(val : integer);
  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;
     Adjust(a);
    Adjust(b);
    Adjust(c);
 
    {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.

Open in new window

0
 
Geert GOracle 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;
Adjust(a); // min = 1
Adjust(b); // min = 1
Adjust(c); // min = 1
If (min <> 0) and (a <= min) then
   richeditWriteOKcursor:=true; // True

and with a = 5, b = 10, c = 1
min := 0;
Adjust(a); // min = 5
Adjust(b); // min = 5
Adjust(c); // min = 1
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.

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 7
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now