Recent

Author Topic: If then else  (Read 12701 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: If then else
« Reply #15 on: September 25, 2017, 11:51:39 am »
Quote from: GetMem
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.
100% agree.

@Molly why not givving the right answer with an example?

@dfergfla
with ':=' you assign a value to a variable. Using if ..  then pascal using '='

Code: Pascal  [Select][+][-]
  1.   if button1.Caption = 'Yes' then
  2.     button1.Caption := 'No';
  3.  
« Last Edit: September 25, 2017, 11:55:37 am by mangakissa »
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: If then else
« Reply #16 on: September 25, 2017, 12:20:43 pm »
@Molly why not givving the right answer with an example?
Because it's too easy. If one needs to do some brushing up like in this case, reading is the way to go, which can be done very comfortably using internet where you can find answers within minutes. If a compiler complains, finding out why on your own is the best way to understand that compiler, so you won't make that mistake again.
keep it simple

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: If then else
« Reply #17 on: September 25, 2017, 12:39:14 pm »
@Molly why not givving the right answer with an example?

0) the term self-sufficiency seems not well spend on your person (no offense)
1) i opted for a quick way out, TS did not responded to that,. In fact no other feedback whatsoever was provided other then thanks for the links.
2) i explained the thing into detail, again no feedback whatsoever other than thanks
3) every link i posted either provided an example or links to an example showing the literal difference between := and =

But i get it: just blame it on someone else and we can sleep well at night again :P

Remind me to never go on a survival with you  ... it will most definitely kill me   :D

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: If then else
« Reply #18 on: September 25, 2017, 03:03:30 pm »
Or like this...  :)

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.  Begin
  3.   Case Button1.Caption
  4.   Of
  5.    'YES': ShowMessage('YES');
  6.    'NO' : ShowMessage('NO');
  7.    '123': ShowMessage('123');
  8.    '456': Begin
  9.            Color            := clBlack;
  10.            Button1.Caption  := 'YES';
  11.            Button1.SetBounds(0, 0, 100, 50);
  12.           End;
  13.    Else ShowMessage('Something else...');
  14.   End;
  15.  End;
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #19 on: September 25, 2017, 06:50:35 pm »
@ molly and everyone

I did get it.  Even before I saw the sample code posted here.

Here is my solution:

  if (Button1.Caption = 'Test 1') then
     Button1.Caption := 'Test 2' else
       if (Button1.Caption = 'Test 2') then
          Button1.Caption := 'Test 3' else
               Button1.Caption := 'Done'     
                                           

I am not sure I needed the parenthesis

Just in case you were interested this is what gave me the clue as to what I was doing wrong.
"Colonize (add a colon to) assignments and decolonize (remove colon from) expressions."
It was right there in front of me, I just couldn't see it.

Donald (aka Newbie)
« Last Edit: September 25, 2017, 07:00:54 pm by dfergfla »

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #20 on: September 25, 2017, 06:52:21 pm »
@ Raw

I don't really understand everything you have there, but I am going to start playing with case statements today.


Thanks

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #21 on: September 25, 2017, 06:55:12 pm »
@ Raw

How exactly did you get the copy and pate to come from the IDE like that?

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: If then else
« Reply #22 on: September 25, 2017, 07:19:09 pm »
Quote
I am not sure I needed the parenthesis
Not in this case, but it's easy to check this for yourself, isn't it..?  :)

But in this case:
Code: Pascal  [Select][+][-]
  1. Procedure TForm1.FormClick(Sender: TObject);
  2.  Begin
  3.   If (Button1.Caption = 'Hallo') And (Button1.Top > 70)
  4.   Then ShowMessage('YES');
  5.  End;

Quote
How exactly did you get the copy and pate to come from the IDE like that?
The usual way:
1. CTRL+C
2. Press the "#"-Button (Insert Code-Button)
3. Place the cursor between (code=pascal) and (/code)
4. CTRL+V
5. Click On Preview...
« Last Edit: September 25, 2017, 07:30:21 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #23 on: September 25, 2017, 07:53:28 pm »
I've been playing around  :D

Code: Pascal  [Select][+][-]
  1. begin
  2.   if Button1.Caption = 'Test 1' then
  3.      begin
  4.      Button1.Caption := 'Test 2';
  5.      Form1.Color := clBlack;
  6.      end
  7.      else
  8.        if Button1.Caption = 'Test 2' then
  9.           Button1.Caption := 'Test 3' else
  10.              begin
  11.              Button1.Caption := 'Done';
  12.              Form1.Color:=clDefault;
  13.              end;
  14.  
  15. end;  ;
« Last Edit: September 25, 2017, 07:56:36 pm by dfergfla »

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: If then else
« Reply #24 on: September 25, 2017, 08:58:22 pm »
Quote
Form1.Color := clBlack;
Try
Code: Pascal  [Select][+][-]
  1. Self.Color:= clBlack;
Or just
Code: Pascal  [Select][+][-]
  1. Color:= clBlack;
... just in case you want to rename the form (Form1)... :)
Otherwise you need to rename the whole source code if you change the name of the form.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: If then else
« Reply #25 on: September 25, 2017, 08:59:19 pm »
Code: Pascal  [Select][+][-]
  1.   case Button1.Caption of
  2.   'Test 1':Button1.Caption := 'Test 2' ; // will not reach test2
  3.   'Test 2':Button1.Caption := 'Test 3';
  4.   else
  5.      Button1.Caption := 'Done';
  6.   end;

Note that Pascal syntax does not allow for fall-through, like is possible with C syntax     
Similar answer to RAW's
                                           
« Last Edit: September 25, 2017, 09:01:34 pm by Thaddy »
Specialize a type, not a var.

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #26 on: September 26, 2017, 12:09:59 am »
@ Thaddy

Thanks, been playing around with it for a little bit.  How would I do more than one thing given a case?

Like change the button caption and the form color if Button1.Caption := 'test 2'

In the if then else I used
Code: Pascal  [Select][+][-]
  1. begin
  2.    Statement 1
  3.    Statement 2
  4. End

but that doesn't work with case.  I tried looking up examples but could not find one that executed more than one statement per case.

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #27 on: September 26, 2017, 02:56:50 am »
got it  :)

Code: Pascal  [Select][+][-]
  1.   case Button1.Caption of
  2.   'Test 1':
  3.     begin
  4.     Button1.Caption := 'Test';
  5.     self.Color := clBlack;
  6.     end;
  7.   'Test 2':Button1.Caption := 'Test 3';
  8.   else
  9.      Button1.Caption := 'Done';
  10.      self.color := clRed
  11.   end;

dfergfla

  • Jr. Member
  • **
  • Posts: 58
Re: If then else
« Reply #28 on: September 26, 2017, 03:10:11 am »
Question:

This works as expected.  The color changes each time:
Code: Pascal  [Select][+][-]
  1. begin
  2.   case Button1.Caption of
  3.   'Test 1':
  4.     begin
  5.     Button1.Caption := 'Test 2';
  6.     self.Color := clBlack;
  7.     end;
  8.   'Test 2':
  9.     begin
  10.     Button1.Caption := 'Test 3';
  11.     Self.Color := clRed ;
  12.     end;
  13.   else
  14.      begin
  15.      Button1.Caption := 'Done';
  16.      self.Color := clDefault;
  17.      end;
  18.   end;
  19.   //self.color := clDefault

In this case the color always stays at default
Code: Pascal  [Select][+][-]
  1. begin
  2.   case Button1.Caption of
  3.   'Test 1':
  4.     begin
  5.     Button1.Caption := 'Test 2';
  6.     self.Color := clBlack;
  7.     end;
  8.   'Test 2':
  9.     begin
  10.     Button1.Caption := 'Test 3';
  11.     Self.Color := clRed ;
  12.     end;
  13.   else
  14.      begin
  15.      Button1.Caption := 'Done';
  16.      //self.Color := clDefault;
  17.      end;
  18.   end;
  19.   self.color := clDefault

Never mind, I figured out why.

Thanks
« Last Edit: September 26, 2017, 07:26:10 am by dfergfla »

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: If then else
« Reply #29 on: September 26, 2017, 07:29:10 am »
Yes, stop torturing the poor soul - it's a mistake we've all made, especially those of us who use more than one programming language.
In Pascal A := 99 sets A to the value 99, whereas If A=99 tests A to see if it has the value 99.
The latter form is used when doing comparisons, and returns a Boolean (i.e. True or False).
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

 

TinyPortal © 2005-2018