Recent

Author Topic: Beginners: Did you know assignment operators can do this  (Read 1547 times)

TBMan

  • Sr. Member
  • ****
  • Posts: 285
Re: Beginners: Did you know assignment operators can do this
« Reply #15 on: October 03, 2025, 02:50:55 am »
I just saw this thread and I tried...

Code: Pascal  [Select][+][-]
  1. var
  2. x:integer;
  3. begin
  4. x := 6;
  5.  
  6. x+=1;
  7.  
  8. writeln(x);
  9.  
  10. end.
  11.  

And 7 was the output. I like that.  I was always jealous of the C syntax  X++;

Doing the following is self documenting though:
Code: Pascal  [Select][+][-]
  1.  
  2. Inc(x);  // if you know Pascal you know.  It's a procedural call though just to add 1 to a number, and
  3.  
  4. Inc(x,#)   // there is this too.          
  5.  
  6. // or
  7.  
  8. x := x+1;  // best thing for readability, but always felt clumsy
  9.  
  10.  

I love programming.

Some things I've done using PTCgraph:

NFL Retro Football (almost finished):
https://www.youtube.com/watch?v=78mTtsd7ppk


Solitaire games:
https://www.youtube.com/watch?v=zmtxI7FdWuQ&list=PLa4BPpFl34iVhFwX1JZwVm3vE5ay_i3R2

Khrys

  • Sr. Member
  • ****
  • Posts: 348
Re: Beginners: Did you know assignment operators can do this
« Reply #16 on: October 03, 2025, 07:13:53 am »
[...] plays games with expected Pascal precedence [...]

Expected Pascal precedence... more like unexpected Pascal precedence  :P

Code: Pascal  [Select][+][-]
  1. if not Number in [2, 3, 5, 7, 11] then {...}

if  statements don't require parentheses per se, but don't worry - you're gonna need lots of them regardless:

Code: Pascal  [Select][+][-]
  1. if X = 0 and I < N - 1       then { Error: Incompatible types: got "Boolean" expected "Int64" - Very helpful! }
  2. if (X = 0) and (I < (N - 1)) then { Whoops - got too paranoid there, (N - 1) doesn't need parentheses }
  3. if (X = 0) and (I < N - 1)   then {...}

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Beginners: Did you know assignment operators can do this
« Reply #17 on: October 03, 2025, 02:42:04 pm »
I know the precedence rules.
 It should be evaluated as x := x * y + 1 because that is what it shortcuts. Not x:= x * (y+1). Anyway, I am used to it, but that is not Pascal.
« Last Edit: October 03, 2025, 02:45:41 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Beginners: Did you know assignment operators can do this
« Reply #18 on: October 04, 2025, 12:29:40 pm »
// if you know Pascal you know.  It's a procedural call though just to add 1 to a number, and
Thankfully there's no mandate to make that a procedure call.
Code: Pascal  [Select][+][-]
  1. Inc(x);
  2. Inc(x,#);
  3.  
Is compiled by fpc, even without any switches, to:
Code: ASM  [Select][+][-]
  1. addl    $1,-8(%rbp)
  2. addl    $5,-8(%rbp)
  3.  
and with -O2:
Code: ASM  [Select][+][-]
  1. addl    $6,%edi
  2.  
The advantage of it being a compilerproc ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Beginners: Did you know assignment operators can do this
« Reply #19 on: October 04, 2025, 01:12:22 pm »
procedural call <> procedure call. The former also describes compilerproc
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018