Recent

Author Topic: Newbie If...Else... question  (Read 5524 times)

the.weavster

  • New Member
  • *
  • Posts: 16
Newbie If...Else... question
« on: December 20, 2008, 04:23:32 pm »
Having only ever used dialects of basic in the past I'm finding having 'If' without an 'End If' confusing, how do I change this code so that the database statements only run if a connection is established?
Code: [Select]
     IB1.KeepConnection:= True;  //   IB1 is a TIBConnection
     IB1.Open;
     If IB1.Connected Then
       TR1.StartTransaction  //   TR1 is a TSQLTransaction
       SQL1.DataBase := IB1    //   SQL1 is a TSQLQuery
       SQL1.Transaction := TR1
       SQL1.SQL.Add('UPDATE EMPLOYEE SET FIRST_NAMES = :fValue WHERE FIRST_NAME = :fName ;')
       SQL1.Params.ParamByName('fValue').AsString:='Doris'
       SQL1.Params.ParamByName('fName').AsString:='Bingo'
       SQL1.ExecSQL
       TR1.Commit
       txtTalk.Text := 'Connected'
     Else
       txtTalk.Text := 'Failed to connect';
Thanks
« Last Edit: December 20, 2008, 06:59:40 pm by the.weavster »

the.weavster

  • New Member
  • *
  • Posts: 16
Re: Newbie If...Else... question
« Reply #1 on: December 20, 2008, 07:05:12 pm »
Could somebody please help me with this, it should be so easy but I can not make it work no matter what I do.

If I only put the semi colon at the end, as my Pascal book suggests you should do, I get an error. If I put a semi-colon at the end of each line I get an error.

Could somebody please put me out of my misery.

the.weavster

  • New Member
  • *
  • Posts: 16
Re: Newbie If...Else... question
« Reply #2 on: December 20, 2008, 07:47:25 pm »
I think I've got it!

If ... Then
  Begin
    //   statements terminated with a ;
  End //   no ; here
Else
  Begin
    //   statements terminated with a ;
  End; //   ; is only at the end of the last option

SlackBrane

  • New Member
  • *
  • Posts: 39
Re: Newbie If...Else... question
« Reply #3 on: December 20, 2008, 08:53:55 pm »
I know not a lot about database things so I'll be not much help.....

However...

If 1 = 1 then
  Edit1.Text := '1 equals 1';
  Edit1.Text := 'Gotcha!';

When you write an If statement in Pascal then it does the next statement until it hits a ; and then continues doing the following statements. So you would end up with a 'Gotcha!'.

If you wanted it to do a bunch of statements before continuing then you would write...

If 1 = 1 then
begin
  Edit1.Text := '1 equals 1';
  CookBaconSandwich(BrownBread,Medium,Rare);
  NuclearLaunch := True;
end;
Edit1.Text := 'Gotcha!';

When you want your If, or other, statement to do lots of other stuff before moving on you enclose the other stuff with begin and end.

In my last shoddy example things would still continue to have Edit1.Text say 'Gotcha!'. Then you get to your else...

If 1 = 1 then
begin
  Edit1.Text := 'Bacon Sandwich is Cooking, Armageddon to Follow ';
  CookBaconSandwich(BrownBread,Medium,Rare);
  NuclearLaunch(Red);
end
else
Edit1.Text := 'Stand Down From Red Alert';
HammerHamster(Head);

Which is still rather rubbish but the important bit is you do not put a ; at the end of the end that is before the else. The other thing is the else only deals with the bit up to the next ; and the Hamster still gets its head hammered....

If NuclearRequest() = 1 then
begin
  Edit1.Text := 'Bacon Sandwich is Cooking, Armageddon to Follow ';
  CookBaconSandwich(BrownBread,Medium,Rare);
  NuclearLaunch(Red);
end
else
begin
  Edit1.Text := 'Stand Down From Red Alert';
  CookBaconSandwich(BrownBread,Medium,Burnt);
  HammerHamster(Tail);
end;

This time sort of as before if you want the else to do a number of things before moving on then you enclose them with begin and end.

So.... then you want an else if but Pascal does not do ElseIf as such, coz thems is rubbish,.... It's the way, I guess, the compiler does its translation.

If NuclearRequest() = 1 then
begin
  Edit1.Text := 'Bacon Sandwich is Cooking, Armageddon to Follow ';
  CookBaconSandwich(BrownBread,Medium,Rare);
  NuclearLaunch(Red);
end
else
If NuclearRequest() = 2 then
begin
  Edit1.Text := 'Stand Down From Red Alert';
  CookBaconSandwich(BrownBread,Medium,Burnt);
  HammerHamster(Tail);
end
else
If NuclearRequest() = 3 then
begin
  Edit1.Text := 'We're Going Down The Pub';
  PhonePub(Guiness,3,Pints);
  PhoneTaxi(HomeAddress,PubAddress);
  HammerHamster(Feed);
end;

Mind you.... it looks like you have already worked it out.

Just to be boring Pascal does not do SelectCase like Basic does. Then again I am drunk person and old and whatever. I'll assume someone will slap me but in my above rubbish NuclearRequest() is a function call that returns a number. Case statements in Pascal deal with 'ordinals'....?

Case NuclearRequest() of
  1: begin
         Edit1.Text := 'Bacon Sandwich is Cooking, Armageddon Follows ';
         CookBaconSandwich(BrownBread,Medium,Rare);
         NuclearLaunch(Red);
      end;
  2: begin
         Edit1.Text := 'Stand Down From Red Alert';
         CookBaconSandwich(BrownBread,Medium,Burnt);
         HammerHamster(Tail);
      end;
  3: begin
        Edit1.Text := 'We're Going Down The Pub';
        PhonePub(Guiness,3,Pints);
        PhoneTaxi(HomeAddress,PubAddress);
        HammerHamster(Feed);
     end;
  end; {case}

There you go, I do comments as well.

Keith

 

« Last Edit: December 20, 2008, 08:57:05 pm by SlackBrane »

the.weavster

  • New Member
  • *
  • Posts: 16
Re: Newbie If...Else... question
« Reply #4 on: December 20, 2008, 11:06:45 pm »
Thank you  :)

Pascal is taking a little getting used to but it's not an ugly language and the Lazarus IDE is so good I know it's going to be worth the effort.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Newbie If...Else... question
« Reply #5 on: December 21, 2008, 02:31:17 pm »
if then else is treated as one statement, ; is used to separate statements.

So, you if you write a ; before else, the compiler thinks you ended the statement a new statement started.

 

TinyPortal © 2005-2018