c to FB multiple assignmet is single line

New to FreeBASIC? Post your questions here.
Post Reply
stan1958
Posts: 5
Joined: Oct 24, 2020 21:17

c to FB multiple assignmet is single line

Post by stan1958 »

is multiple assignmet line allowed in FB?
similar to below

C:

first->x4 = second->x1 = (first->x3 + second->x2) * 0.5;


I have converted to
FB:

dim c as double = (first->x3 + second->x2) * 0.5
c = first->x4
c = second->x1

wonder is this the better way
angros47
Posts: 2346
Joined: Jun 21, 2005 19:04

Re: c to FB multiple assignmet is single line

Post by angros47 »

No, multiple assignment is not allowed in FreeBASIC, because in BASIC the operator "=" is also used for comparison, so an expression like

Code: Select all

A = B = C 
will compare B to C, and return -1 if they are equal or 0 if they aren't. So, the value of B is not modified, and the value of A can only be 0 or -1
This applies to other dialects of BASIC, not only to FreeBASIC

in C it's possible to do the double assignment because the operator used for comparison is not "=", but "==" (the operator "=" is used only for assignment, so there is no ambiguity). In BASIC you have to do two separate assignments (don't worry, the resulting compiled code should be identical, with the right optimizations enabled)
paul doe
Moderator
Posts: 1832
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: c to FB multiple assignmet is single line

Post by paul doe »

stan1958 wrote: Sep 11, 2024 23:12 is multiple assignmet line allowed in FB?
similar to below

C:

first->x4 = second->x1 = (first->x3 + second->x2) * 0.5;


I have converted to
FB:

dim c as double = (first->x3 + second->x2) * 0.5
c = first->x4
c = second->x1

wonder is this the better way
No, the C code is equivalent to:

Code: Select all

dim c as double = (first->x3 + second->x2) * 0.5
first->x4 = c
second->x1 = c
Post Reply