Newbie If...Else... question lazarus / 9-Editor ---------------------------------------------------------------------- the.weavster · 2008-12-20 15:23 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 ---------------------------------------------------------------------- the.weavster · 2008-12-20 18:05 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 · 2008-12-20 18:47 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 · 2008-12-20 19:53 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 ---------------------------------------------------------------------- the.weavster · 2008-12-20 22:06 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 · 2008-12-21 13:31 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.