Join us today!

Notifications
Clear all

[Solved] instruction increment a variable by one, var +=1;?

5 Posts
3 Users
3 Reactions
1,423 Views
runtimevictor
Posts: 156
Topic starter
(@runtimevictor)
Estimable Member
Joined: 2 years ago

Hello,
I want to increment or decrement a variable type INT by one,
this instruction works for me in Tia Portal, in TwinCAT how would it be?..

PROGRAM MAIN
VAR
 var : INT;
END_VAR

var +=1;

Reply
4 Replies
3 Replies
twinControls
Admin
(@twincontrols)
Joined: 2 years ago

Member
Posts: 115

@runtimevictor 

Hi, 

To increase by 1:

var := var + 1; 

 

To decrease by 1:

var := var - 1; 

 

I wouldn't recommend using 'var' as your variable name since it's a keyword in TwinCAT 3. 

Reply
runtimevictor
(@runtimevictor)
Joined: 2 years ago

Estimable Member
Posts: 156

@twincontrols ,

Isn't there a more compact instruction?

Variable Name "var" was an example without taking anything into account...

Thanks.

Reply
twinControls
Admin
(@twincontrols)
Joined: 2 years ago

Member
Posts: 115

@runtimevictor 

If you'd like you can create a function to do that as well. 

 

FUNCTION F_IncreaseBy:

FUNCTION F_IncreaseBy : BOOL
VAR_INPUT
	nNumber :REFERENCE TO INT ;
	nIncreaseBy : INT;
END_VAR
VAR
END_VAR

nNumber :=  nNumber + nIncreaseBy;
F_IncreaseBy := TRUE;

 

MAIN:

PROGRAM MAIN
VAR
	nNumber : INT; 
END_VAR

F_IncreaseBy(nNumber,2); // number, increase amount

 

Reply
kolyur
Posts: 12
(@kolyur)
Active Member
Joined: 2 years ago

I'd love to have a full set of assignment operators in ST, plus pre- and post- increment/decrement. But alas, 'tis not to be. I sometimes find myself typing them in by mistake then gritting my teeth as the compile errors stack up.

Reply
Share: