Recent

Author Topic: If [boolean condition] And [boolean condition]  (Read 10450 times)

A

  • Guest
If [boolean condition] And [boolean condition]
« on: December 27, 2017, 09:43:14 pm »
Hello,

As the title says, I am trying to use the 'And' keyword in my If statement to express the result of two conditions being true. Here's my code:

Code: Pascal  [Select][+][-]
  1.    If NameEdit.Text = '' And DirEdit.Text = ''
  2.    Then OKButton.Enabled := False
  3.    Else OKButton.Enabled := True;

This results in the following error:

unit2.pas(56,26) Error: Operator is not overloaded: "Constant String" and "TTranslateString"

I haven't a clue what this error means; am I expressing 'And' correctly? Should it be '&', '&&', or something instead?

Thank you in advance.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: If [boolean condition] And [boolean condition]
« Reply #1 on: December 27, 2017, 09:44:47 pm »
Code: Text  [Select][+][-]
  1. If (NameEdit.Text = '') And (DirEdit.Text = '')
  2.  

A

  • Guest
Re: If [boolean condition] And [boolean condition]
« Reply #2 on: December 27, 2017, 09:49:11 pm »
Thank you ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: If [boolean condition] And [boolean condition]
« Reply #3 on: December 27, 2017, 10:01:06 pm »
Code: Pascal  [Select][+][-]
  1. f (NameEdit.Text = '') And (DirEdit.Text = '')
  2.  
Code: Pascal  [Select][+][-]
  1. if NameEdit.Text.IsEmpty And DirEdit.Text.isEmpty // it is nearly 2018....
  2.  
« Last Edit: December 27, 2017, 10:05:18 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: If [boolean condition] And [boolean condition]
« Reply #4 on: December 27, 2017, 10:02:11 pm »
if NameEdit.Text.IsEmpty And DirEdit.Text.isEmpty
too glamorous!
an old-school guy here.

to be honest, I'd do it this way:
Code: Text  [Select][+][-]
  1. OKButton.Enabled := (NameEdit.Text <> '') or (DirEdit.Text <> '');
  2.  
but this could be a bit too complicated for beginners. (as well as it was not the question :))
« Last Edit: December 27, 2017, 10:05:25 pm by skalogryz »

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: If [boolean condition] And [boolean condition]
« Reply #5 on: December 27, 2017, 10:04:04 pm »
Rephrased above...  :D If i would have to express your pascal knowledge in Boolean logic it probably ends up true...
« Last Edit: December 27, 2017, 10:08:26 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

A

  • Guest
Re: If [boolean condition] And [boolean condition]
« Reply #6 on: December 27, 2017, 10:09:54 pm »
Thanks :)

I guess there's always more than one way of doing things.
I'll try to remember that way.
Just one last thing, is there a way of making 2 things happen If [CONDITION(S)]?

E.g.
Code: Pascal  [Select][+][-]
  1.    If (NameEdit.Text = '') Or (DirEdit.Text = '')Then
  2.    OKButton.Enabled := False
  3.    FinalDir.Clear
  4.    Else OKButton.Enabled := True;

Thank you  >:D

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: If [boolean condition] And [boolean condition]
« Reply #7 on: December 27, 2017, 10:10:51 pm »
Code: Pascal  [Select][+][-]
  1. If (NameEdit.Text = '') Or (DirEdit.Text = '')Then begin
  2.    OKButton.Enabled := False;
  3.    FinalDir.Clear;
  4.    end Else OKButton.Enabled := True;
  5.  

A

  • Guest
Re: If [boolean condition] And [boolean condition]
« Reply #8 on: December 27, 2017, 10:14:09 pm »
Thanks. Just one last thing, I promise (sorry to be needy) but for the sake of knowledge, what does <> actually do? And when would I use it instead of '='?

Thanks for helping me out!

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: If [boolean condition] And [boolean condition]
« Reply #9 on: December 27, 2017, 10:16:05 pm »
= equal
<> not equal

in case of
Code: Pascal  [Select][+][-]
  1. NameEdit.Text <> ''
  2.  
it compares the text not to be empty
(not equal to a blank string)

A

  • Guest
Re: If [boolean condition] And [boolean condition]
« Reply #10 on: December 27, 2017, 10:16:53 pm »
Good to know, thanks.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: If [boolean condition] And [boolean condition]
« Reply #11 on: December 27, 2017, 10:17:53 pm »
Here's another way:
Code: Pascal  [Select][+][-]
  1.   OKButton.Enabled := not ( (NameEdit.Text = '') Or (DirEdit.Text = '') );
  2.   if not OKButton.Enabled then FinalDir.Clear;
  3.  

A

  • Guest
Re: If [boolean condition] And [boolean condition]
« Reply #12 on: December 27, 2017, 11:47:37 pm »
Thank you!

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: If [boolean condition] And [boolean condition]
« Reply #13 on: December 28, 2017, 10:19:58 am »
If it comes to IF blocks Pascal can be quite confusing to those who are new to the language, probably more than any other language. Multiple conditions require parentheses. Multiple statements require begin..end and you must know when the statement closes (semicolon):
Code: Pascal  [Select][+][-]
  1. If (NameEdit.Text = '') Or (DirEdit.Text = '') Then
  2.   begin
  3.     OKButton.Enabled := False;
  4.     FinalDir.Clear;
  5.   end // <- NO semicolon here
  6. Else
  7.   OKButton.Enabled := True; // <- end of entire IF statement
  8.  
Nested IFs in particular require your attention:
Code: Pascal  [Select][+][-]
  1. if condition then
  2.   begin
  3.     if condition then
  4.       begin
  5.         statement1;
  6.         statement2;
  7.       end; // <- nested IF block ends
  8.   end // <- IF block continues
  9. else if condition then
  10.   begin
  11.     if condition then
  12.       begin
  13.         statement3;
  14.         statement4;
  15.       end; // <- nested IF block ends
  16.   end // <- IF block continues
  17. else
  18.   statement5; // IF block ends (single statement so BEGIN..END not necessary)
Another issue that comes up frequently is the difference between assignment (:=) and comparison (=).
« Last Edit: December 29, 2017, 12:02:46 am by Munair »
It's only logical.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: If [boolean condition] And [boolean condition]
« Reply #14 on: December 28, 2017, 12:04:46 pm »
Well, Munair, you missed the point: the point is that to set a boolean condition from a system of boolean conditions you do not over use if/then/else, because such a system can always be resolved using boolean math only.
Both I and skalogryz gave simple examples.

Here's your example my way:
Code: Pascal  [Select][+][-]
  1. if (condition1 and condition2)  then
  2.       begin
  3.         statement1;
  4.         statement2;
  5.       end
  6. else if (condition3 and condition4) then
  7.       begin
  8.         statement3;
  9.         statement4;
  10.       end else statement4;
This code will execute much faster. [I made some edits]
I can probably improve on this too:
Code: Pascal  [Select][+][-]
  1. case condition of
  2.    condition1 and condition2:
  3.    begin
  4.      statemen1;
  5.      statement2;
  6.   end;
  7.   condition3 and condition4:
  8.   begin
  9.      statement3;
  10.      statement4;
  11.    end;
  12. else
  13.      statement4;
  14. end;
« Last Edit: December 28, 2017, 01:00:38 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018