Forum > Beginners

Nest assignment into if-statement

<< < (4/4)

marcov:
A statement is also an expression in C, even if it is only void.

BrainChemistry:

--- Quote from: User137 ---There was another option: .... That just assumes ex_func() is made by you, and set the ex_var in it.
--- End quote ---

In the real case, the function isn't made by me.


--- Quote from: Edson ---Pascal syntax was made to be clear. Probably you will need more lines to implement, but it will be more readable.  :)
--- End quote ---

I agree, and I like it. Compared to C possibilities to cryptify source code, the one-liner here is obvious to the reader and just convenient.


--- Quote from: wp ---Cramping "everything" into a single line of code will cost you hours of debugging months later when you'll have forgotten all the details.
--- End quote ---

It isn't as if the line I asked for is so cryptic to not get the idea behind this. (I know what you mean when you're talking about C though.)


--- Quote from: BBasile on January 09, 2016, 10:54:40 pm ---The problem is that in the Pascal construct if expression then statement, expression must be something that can be evaluated to boolean. The feature you speak about for example is more useful with languages that makes shortcuts in their if expression:


--- Code: D  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (auto vt = "key" in AA) {}<=>

--- Code: D  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ValueType* vt = "key" in AA;if (vt != null){}or

--- Code: D  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (size_t len = array.length) {}<=>

--- Code: D  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---size_t len = array.length;if (len != 0) {}
Because here the if expression allows to pass a pointer or an integral type (and boolean types of course), then the feature your speak about is yet more interesting.

--- End quote ---

I'm not sure if I get you right. The examples you constructed do not make it obvious to me why in Pascal this shouldn't be allowed. Unless this "feature" is just not supported in Pascal, meaning, assignments in the expression area are not allowed. - To my knowledge, I could construct similar code for pointer and integer types (and pass them). Hence, not sure about this remark.


--- Quote from: Abelisto ---It is about that the assignment operator in C language returns the value and in Pascal does not.
--- End quote ---

That makes sense, since


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if ( ex_func( ex_param ) = 0) thenbegin  //whateverend;
works.

I would have expected the assignment operator would return the value to the variable which then would be evaluated by the if-statement. Then both expressions (C and Pascal from OP) should work the same. - If the assignment operator in C assigns AND returns (like a function) but the Pascal assignment operator just assigns, the if-statement at the same time does not evaluate the variable value but the return value, I see that it never could work.

Thanks for the clarification!

fic:

--- Quote from: Abelisto on January 09, 2016, 11:09:13 pm ---Lets discuss the simplified example that directly related to the topic:


--- Code: C  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---int a, b, c;a = b = c = 1; 
 ;)

--- End quote ---

To be honest? This declaration is the one thing I really miss in Pascal, and that's the only thing!
Sure, one can write code in a few lines in C++, where a Pascal-programmer probably needs 10 lines or more to achieve the same result.
Lately, I've read lots of things people want to change in FPC/Lazarus, but this is a Pascal-forum.
Although I like this discussion. Pascal is and will hopefully stay a pgm-language in the way it was developed.
Sure, every language has its faults (and/or goods), but people here try to accomplisch something...
If you guys want to change that? Well, create your own Lazarus or C-Lazarus, or Java-Lazarus, or Ada-Lazarus... And please... Make it work like you all want!

Thaddy:

--- Quote from: BeniBela on January 09, 2016, 01:28:52 am ---You can only do it like this:


--- Code: ---function assignwrap(out assignee: integer; const data: integer): integer;
begin
  result := data;
  assignee := data;
end;

if assignwrap(ex_var, ex_func(ex_param)) = 0 then ...

--- End code ---



Pascal is an ugly language

--- End quote ---

Nope. write inline and look at the asm output:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---[code]function assignwrap(out assignee: integer; const data: integer): integer;inline;begin  result := data;  assignee := data;end;
Now, go and play with your toy.

There are many more ways to do that of course, but I'd thought it appropriate to stick to inline.

Navigation

[0] Message Index

[*] Previous page

Go to full version