Recent

Author Topic: If then else  (Read 12727 times)

dfergfla

  • Jr. Member
  • **
  • Posts: 58
If then else
« on: September 25, 2017, 03:48:24 am »
I am just started playing around with Lazarus again.  I kind of played with it a few years ago.  I'm not a programmer but used to play with pascal way, way back in the 80's, ya I'm that old.  Anyway, so i am up to my eyeballs in what will be stupid questions to the people here.  But, i will never get anywhere if I don't ask.  The last programming language I toyed with in any serious way was VB 5 and that was well over 20 years ago.  So, now for my stupid question.

I was playing around trying to get comfortable with If then else.  But tried something that doesn't work and I thought it should.  I am clearly wrong and now I want to figure out how.

I have a button on a form and was trying to use an if then else statement to evaluate the text of the caption and change it...into something else and it looked like this....

If Button1.Caption := 'Yes' then
   Button1.Caption := 'no';

I am getting an error :unit1.pas(35,13) Error: Incompatible types: got "untyped" expected "Boolean"

I do know that If Then Else has to be boolean, but I thought this type of thing would eval to a yes or no.
So, how would I do this?


Thanks :)

Newbie

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #1 on: September 25, 2017, 04:12:30 am »
Just a quick question to see if you are able to help yourself:

How would Button1.Caption := 'Yes' evaluate to a boolean ?

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #2 on: September 25, 2017, 04:17:55 am »
@molly

Ya,  I realized that I was way off...but now I just can't seem to figure out how I would do something like that at all, never mind using If Then Else.  Would a case statement work?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #3 on: September 25, 2017, 04:29:10 am »
Just to make sure: you do know the difference between an assignment and a boolean expression (comparison in this case) ? see also if-then-else definition

Yes, a case statement would work as well.

edit: added links
« Last Edit: September 25, 2017, 04:36:33 am by molly »

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: If then else
« Reply #4 on: September 25, 2017, 04:40:07 am »
dfergfla,

A hint (trying to keep Mollys efforts intact).

Perform a Colectomy (Colectomy is surgery to remove the colon).
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #5 on: September 25, 2017, 04:50:31 am »
@ molly

Thanks for the links.  I had an ok(ish) understanding but better now.  I'm going to go lookup case statements and play with that.

You said case would work as well.  may I ask what you would do, if not case?

Thanks again.


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #6 on: September 25, 2017, 05:08:07 am »
@dfergfla
I have two choices there: either paste the answer or let you memorize a very important lesson (for a very long time, because you came up with the answer on yourself).

I would use a if-then-else construction if not using a case statement.

So, what would you prefer i do ?

@Bazzao:
thank you. it is a very good hint because i even had to read that twice before it 'hit' me  :D
« Last Edit: September 25, 2017, 05:17:47 am by molly »

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #7 on: September 25, 2017, 05:38:38 am »
I found TryStrToBool

But, the example isn't sinking in for me...

procedure TForm1.ToggleBox1Change(Sender: TObject);
var true : boolean;
begin
  if Button1.Caption:= TryStrToBool('test' , true) then
     Button1.Caption:= 'no test';
end;
       

gives me an err:   unit1.pas(36,52) Error: Incompatible type for arg no. 1: Got "Boolean", expected "TTranslateString"

I am also trying to learn how to read what the debugger is trying to tell me...


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #8 on: September 25, 2017, 05:51:33 am »
small advise: stick to the first error "Error: Incompatible types: got "untyped" expected "Boolean" as that is easier to grasp.

You're thinking waaay out of the box in your last attempt.

What the compiler is trying to tell you in the first error is that it is expecting a boolean (or expression that evaluates to a boolean result value) but it got a untyped of something instead.

Pascal is for instance not like c where every assignment is checked against zero and evaluates to a true or false value by some complicated compiler magic that nobody is able to explain when a statement get too complicated to comprehend.

Pascal needs you to be explicit, also in this case. Doing an assignment alone is not a boolean expression and will also not evaluate to a boolean.

So, a boolean expression is required. I already linked to some of the operators that you could use to 'make' a boolean expression.

So
  X := Y would assign Y to X
and
  X <> Y would be a expression that evaluates to a boolean that could be used for instance in a if-then-else construction.

edit: reworded and added links

edit2: finally i was able to find a link that has things summed up using simple words (i hope), here.
« Last Edit: September 25, 2017, 06:11:06 am by molly »

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #9 on: September 25, 2017, 06:36:05 am »
 ya.. way outside the box.  Thanks for the link.  and for the help.
« Last Edit: September 25, 2017, 06:42:04 am by dfergfla »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #10 on: September 25, 2017, 06:57:58 am »
Sorry dfergfla as it still is not the copy-paste answer that you are probably be looking for, but i think i provided enough information for you to be able to distinguish between an assignment and a comparison operator.

If that difference has reached your understanding then please do come back and show us. As for your example of comparing, assigning and changing the button caption there is much more to tell/learn then only the difference between an assignment and a comparison.

Just say the word and i will paste the literal answer to your question, with some additional notes about why you can't solve this gracefully with using the (fixed) code from your snippet alone.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: If then else
« Reply #11 on: September 25, 2017, 07:03:51 am »
Molly,

My turn ...

Colonize (add a colon to) assignments and decolonize (remove colon from) expressions.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #12 on: September 25, 2017, 07:21:39 am »
@Bazzao
Fair enough, i gave it my best shot and that didn't seem to be able to do the trick :-[

It's a very important lesson to learn though so i discarded just pasting the answer.

This kind of basic understanding should be part of the standard programming vocabulary otherwise the same questions will be asked over and over again and prohibits healthy progress. And yes, when i started out with Pascal (or any other language for that matter) i made the same stupid mistakes as any n00b makes, as i dislike reading manuals as well  :)

balazsszekely

  • Guest
Re: If then else
« Reply #13 on: September 25, 2017, 08:03:12 am »
Stop torturing the man. I did the same mistake a few days ago. I was staring my monitor 30 seconds before I realized what is wrong.  :D For a beginner is a perfectly understandable mistake.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: If then else
« Reply #14 on: September 25, 2017, 08:12:13 am »
Given your own example, apparently you know that the '=' operator in Pascal is not always the same as in VB. You should read a bit more about that first.
keep it simple

 

TinyPortal © 2005-2018