Page 1 of 1

Changing variables in NXC

Posted: 26 Aug 2011, 13:47
by penguinplus
I am new to NXC so how do I change a variable? I made the variable that turn = 0 and it is all set up. But how do I change it to another number?

Re: Changing variables in NXC

Posted: 26 Aug 2011, 15:05
by h-g-t
Not quite sure what you mean here.

If you want to change the variable name throughout the program then use search and replace.

If you want to change the value of turn then any assignment will do -

turn = put in the name of any other variable here
turn = put in any number here
turn = turn + 16*2
put in the name of any other variable here = turn
put in the name of any other variable here = turn + 56

Is that the sort of thing you mean?

Re: Changing variables in NXC

Posted: 26 Aug 2011, 16:06
by penguinplus
I try to change a variable in a if statement bot it gives me errors.

Re: Changing variables in NXC

Posted: 26 Aug 2011, 16:34
by haydenstudios
It often helps to provide your code for us to read. Even more so, it is easier to read if you provide your code this way:

Code: Select all

[code](your code)
[/code]
Maybe you accidentally did the wrong combination of wether or not to use spaces for certain commands. It's a possible exlpanation for why your if statement isn't working, but like I said, it's best if you provide your code for us to see.

Re: Changing variables in NXC

Posted: 26 Aug 2011, 17:12
by afanofosc
penguinplus wrote:I am new to NXC so how do I change a variable? I made the variable that turn = 0 and it is all set up. But how do I change it to another number?
I think you should try reading the NXC Tutorial and the NXC Programmer's Guide. Both of these have many examples of how you use variables.

Code: Select all

task main()
{
  int turn = 0;
  turn++; // increment turn
  turn = 4; // set turn's value to 4
  turn += 4; // add 4 to turns current value
  turn = Random(); // set turn to a random number
}
To change a variable's value you use an assignment statement. lhs = rhs. I.e., the left hand side "equal sign" the right hand side. All of this is fully explained in the tutorial and programmer's guide. And in the example code that comes with BricxCC. There are also a ton of C tutorials on the internet which you could read to help understand the C programming language.

John Hansen