Recent

Author Topic: [SOLVED]if construction probleme !!  (Read 7028 times)

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
[SOLVED]if construction probleme !!
« on: December 30, 2014, 11:26:04 am »
Hello !!
I write this code :
// -----------------------------------------------
function fun1(const str: string): integer ;
begin
  Result := 1 ;
end;

and in an other function I did :
// ----------------------------------------------------
procedure ExecSQL();
var
  ret: integer ;
begin   
  if (ret:=fun1(BS_q1))=1 then
     Exit;
   

end;
and it gives mes error message :
bsmusermanager.pas(298,10) Fatal: Syntax error, ")" expected but ":=" found
 
I've tried the following :
  if ((ret:=fun1(BS_q1))=1) then
     Exit;     

but the same error message is given.

only the construction :
 
   ret := fun(BS_q1);
   if ret = 1 then 
      Exit;

PS: I'm new comer from Delphi, and I don't know if free pascal allow sach construction for if.
Thanks

« Last Edit: December 30, 2014, 01:55:44 pm by bsaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: if construction probleme !!
« Reply #1 on: December 30, 2014, 11:45:41 am »
You just need to test the function result directly in the if..then statement.  No need for the variable.
Code: [Select]
procedure ExecSQL();
begin   
  if (fun1(BS_q1)=1) then
     Exit;   
end;
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: if construction probleme !!
« Reply #2 on: December 30, 2014, 01:07:07 pm »
You just need to test the function result directly in the if..then statement.  No need for the variable.
Code: [Select]
procedure ExecSQL();
begin   
  if (fun1(BS_q1)=1) then
     Exit;   
end;
thnaks
So, it does'nt support the constructions as delphi ?
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED]if construction probleme !!
« Reply #3 on: December 30, 2014, 02:21:24 pm »
Maybe you are referring to this:

Code: [Select]
procedure ExecSQL();
var
  ret: integer ;
begin
  if ret = (fun1(BS_q1) = 1) then
     Exit;
end;   

Code: [Select]
    ///-boolean expression---------///   
            //boolean expression//
 if ret =     (fun1(BS_q1) = 1)           then

See this:

Code: [Select]
var
  ret, a, b, c: integer ;
begin
  if a = (b = (c = (ret = (fun1(BS_q1) = 1)))) then
     Exit;
end;
« Last Edit: December 30, 2014, 02:33:09 pm by typo »

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: [SOLVED]if construction probleme !!
« Reply #4 on: December 30, 2014, 02:32:09 pm »
Maybe you are referring to this:

Code: [Select]
procedure ExecSQL();
var
  ret: integer ;
begin
  if ret = (fun1(BS_q1) = 1) then
     Exit;
end;   

Code: [Select]
   ///-boolean expression---------///   
            //boolean expression//
 if ret =     (fun1(BS_q1) = 1)           then
No !
the return of fun1 must be put in the variable ret then will be tested against 1
 steps :
 
Code: [Select]
  ret = fun1(BS_q1);*
  if ret = 1 then
     Exit;
+ there is error of compatibility of datatypes.
your code just do not compiles.
« Last Edit: December 30, 2014, 02:34:12 pm by bsaidus »
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED]if construction probleme !!
« Reply #5 on: December 30, 2014, 02:46:32 pm »
Code: [Select]
if ret = integer(fun1(BS_q1) = 1) then

or:

Code: [Select]
if boolean(ret) = (fun1(BS_q1) = 1) then

Code: [Select]
if a = integer(b = integer(c = integer(ret = integer(fun1(BS_q1) = 1)))) then
« Last Edit: December 30, 2014, 02:54:28 pm by typo »

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: [SOLVED]if construction probleme !!
« Reply #6 on: December 30, 2014, 03:20:01 pm »
Yes after casting it works but it do not feet my needs
My goal is to retreave the return value of fun1() into the variable ret
Code: [Select]
ret := fun1(BS_q1) then do comparaison to 1
Code: [Select]
ret=1as we can do it in delphi
Code: [Select]
if (ret := fun1(BS_q1)) = 1 then
   ....

thanks !!
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8790
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [SOLVED]if construction probleme !!
« Reply #7 on: December 30, 2014, 05:50:52 pm »
Yes after casting it works but it do not feet my needs
My goal is to retreave the return value of fun1() into the variable ret
Code: [Select]
ret := fun1(BS_q1) then do comparaison to 1
Code: [Select]
ret=1as we can do it in delphi
Code: [Select]
if (ret := fun1(BS_q1)) = 1 then
   ....

thanks !!
which Delphi version are you using? I don't think Delphi allows that since assignment is always statement in Pascal, and Delphi implementation is no exception.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: [SOLVED]if construction probleme !!
« Reply #8 on: December 30, 2014, 05:55:27 pm »
I too have deep doubts about Delphi allowing this.

Perhaps bsaidus has misunderstood some boolean expression.
« Last Edit: December 31, 2014, 10:16:27 pm by typo »

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: [SOLVED]if construction probleme !!
« Reply #9 on: January 03, 2015, 11:10:01 am »
Excuuuuuuuuuuuuuuuuuuuuuse me please !!
I made a confusion from C to Delphi.
It's C that allow such construction.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

int fun1(const char * p)
{ return 1 ; }


int main(int argc, char *argv[]) {
int ret ;

if ((ret=fun1(NULL))==1)
{ printf("good %d", ret); }
system("pause");
return 0;
}
 

thakns !!
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8790
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [SOLVED]if construction probleme !!
« Reply #10 on: January 03, 2015, 02:10:23 pm »
Excuuuuuuuuuuuuuuuuuuuuuse me please !!
I made a confusion from C to Delphi.
It's C that allow such construction.
As expected. In C family of languages, assignment is expression, not a statement.

 

TinyPortal © 2005-2018