Go to Post FIRST doesn't end after high school, for some of us, it is only beginning. - David Kelly [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-12-2006, 23:01
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
incrementing

HEy Quick Question

If i say..

static int i = 1;

i++;

Then it will increase by 1..... how do i increase by 2... or maybe 3... would it be?

static int i = 1;

i++2;

... maybe? lol i ono talk to me
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #2   Spotlight this post!  
Unread 07-12-2006, 23:07
Rob2713g's Avatar
Rob2713g Rob2713g is offline
Registered User
FRC #0540 (Screaming Eagles)
 
Join Date: Apr 2006
Rookie Year: 2004
Location: VA
Posts: 143
Rob2713g has a spectacular aura aboutRob2713g has a spectacular aura aboutRob2713g has a spectacular aura about
Re: incrementing

Just i = i + 2

i++ is a shortcut, otherwise just use the number you would like to add

Last edited by Rob2713g : 08-12-2006 at 11:26.
  #3   Spotlight this post!  
Unread 07-12-2006, 23:09
Ari Allyn-Feuer Ari Allyn-Feuer is offline
Registered User
FRC #0753
 
Join Date: Feb 2006
Location: Bend, OR
Posts: 22
Ari Allyn-Feuer is on a distinguished road
Re: incrementing

Quote:
Originally Posted by teh_pwnerer795
HEy Quick Question

If i say..

static int i = 1;

i++;

Then it will increase by 1..... how do i increase by 2... or maybe 3... would it be?

static int i = 1;

i++2;

... maybe? lol i ono talk to me
You can use the ordinary mathematical operators in C. The syntax is:

name_of_var = [EXPRESSION]

You can string them together:

var1 = var2 = 3

sets both var1 and var2 to 3.

You can use +, -, *, divide, everything. Please note that with integers, divide just truncates. Also, there are a few little things C does. Like ++ adds one without the equals, -- subtracts without it. varname += n will add the value n to varname. same for -=, *=.

Also, you can use the symbol % for remainder in division, or for modular arithmetic. Like 7%3=1, 56%10=6, 39%17=5.

If you need any more help, ask me. ari.allynfeuer@gmail.com

Ari.
  #4   Spotlight this post!  
Unread 07-12-2006, 23:19
TubaMorg TubaMorg is offline
Programmermechanicalelect ricalcoach
AKA: Dan
FRC #1480 (Robatos Locos)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Houston
Posts: 450
TubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond repute
Re: incrementing

i++ is shorthand for i = i + 1 since incrementing by one is such a common task in programming. Similarly i-- is the same as i = i -1. If you want to add or subtract something different then you have to use the long version as in i = i + 3.


Not many people realize you can also go ++i and --i which is subtly different. In most cases the end effect is the same but not always. With ++i (prefixed) the value is computed before the expression is evaluated. The postfix version i++ is the opposite where the value is computed AFTER the expression is evaluated. This doesn't make any difference if you just go:

i++;
or
++i;

However if you try:

m = ((++i)*(j++)/5;

i will increment by 1 BEFORE the rest of the expression is evaluated. j will be incremented AFTER the expression is evaluated.

Good luck!
  #5   Spotlight this post!  
Unread 07-12-2006, 23:33
teh_pwnerer795 teh_pwnerer795 is offline
Registered User
AKA: !!Mitch!!
FRC #1392
Team Role: Programmer
 
Join Date: Nov 2006
Rookie Year: 2006
Location: Ajax, Canada
Posts: 142
teh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really niceteh_pwnerer795 is just really nice
Send a message via MSN to teh_pwnerer795 Send a message via Yahoo to teh_pwnerer795
Re: incrementing

Quote:
Originally Posted by TubaMorg
i++ is shorthand for i = i + 1 since incrementing by one is such a common task in programming. Similarly i-- is the same as i = i -1. If you want to add or subtract something different then you have to use the long version as in i = i + 3.


Not many people realize you can also go ++i and --i which is subtly different. In most cases the end effect is the same but not always. With ++i (prefixed) the value is computed before the expression is evaluated. The postfix version i++ is the opposite where the value is computed AFTER the expression is evaluated. This doesn't make any difference if you just go:

i++;
or
++i;

However if you try:

m = ((++i)*(j++)/5;

i will increment by 1 BEFORE the rest of the expression is evaluated. j will be incremented AFTER the expression is evaluated.

Good luck!
Awesome.. thanxs for the tip more for my programming knowledge
__________________
Great Art is Created; Master Pieces are Stolen

[n00b]Teh_Foxdonut teh_pwnerer795 - CounterStrike:Source
  #6   Spotlight this post!  
Unread 08-12-2006, 03:39
TimCraig TimCraig is offline
Registered User
AKA: Tim Craig
no team
 
Join Date: Aug 2004
Rookie Year: 2003
Location: San Jose, CA
Posts: 221
TimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to behold
Re: incrementing

Quote:
Originally Posted by teh_pwnerer795
Then it will increase by 1..... how do i increase by 2... or maybe 3... would it be?

static int i = 1;

i++2;
i++ and its prefix version ++i only add one. There's no way to increment by a different value using this notation.

To add 2 there are two ways to do it.

The "classic" programming construct
i = i + 2;

and
i += 2;

The second version can be more than just a slightly shorter way of writing the expression if you have a non optimizing compiler or one that doesn't optimize very well.

To digress a little and provide some history, there are three ways to add 1 to a variable.

The increment operator in either postfix or prefix form: i++ or ++i

An in place add: i += 1

And the load, add, and assign: i = i + 1

With a non optimizing compiler and a fairly rich native machine instruction set, these three forms will all generate different machine code and some forms will be more efficient. The programmer would chose the form to use based on knowing how the compiler would translate it.

For instance on an Intel x86 chip the increment operator maps directly to the assembly instruction INC so you do the increment in one instruction.

The second form generates machine code that directly adds the number to the variable. It's longer since it first loads 1 into the accumulator and adds that to the variable directly. Two instructions, so not as efficient.

And finally, the third form causes the processor to load the variable i into the accumulator, add 1 to it, and then store the result back into i's memory location. Even more instructions and slower execution.

On processors like the PICs in the IFI controllers the instruction set is much more limited so it's not as likely to make as much difference. Also, modern optimizing compilers will typically find the "best" machine instructions to use. With some you can even tell them to optimize for smaller code size or faster execution. Sometimes, paradoxically, smaller code is also faster.
  #7   Spotlight this post!  
Unread 08-12-2006, 06:48
chris31 chris31 is offline
Team 2021 Captain
AKA: Chris Davidson
FRC #2021 (FA Robotics)
Team Role: Mentor
 
Join Date: Nov 2005
Rookie Year: 2006
Location: Atlanta, GA/ Fredericksburg,VA
Posts: 949
chris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond reputechris31 has a reputation beyond repute
Send a message via AIM to chris31
Re: incrementing

Easiest way to do it is just i+=2 or i=i+2
  #8   Spotlight this post!  
Unread 08-12-2006, 07:33
John Gutmann John Gutmann is offline
I'm right here
AKA: sparksandtabs
FRC #0340 (GRR)
Team Role: Mechanical
 
Join Date: Feb 2005
Rookie Year: 2004
Location: rochester
Posts: 804
John Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant future
Send a message via AIM to John Gutmann Send a message via MSN to John Gutmann Send a message via Yahoo to John Gutmann
Re: incrementing

you can just do i+=2 or i+=3 not much diff, but hey! it is the same number of keys your pressing.
  #9   Spotlight this post!  
Unread 08-12-2006, 15:25
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: incrementing

Quote:
Originally Posted by TimCraig
i++ and its prefix version ++i only add one. There's no way to increment by a different value using this notation...
Here's something few people know about C: pointer arithmetic scales integers by the size of the type being pointed to. For example, if ptr is a pointer to an int, then ptr++ will add 2 (or whatever the native size of an int is for a particular target). If parr is a pointer to a structure of twelve bytes, parr = parr + 3 will add 36.

But don't abuse this fact. Pointer arithmetic is not necessarily something useful for programming PIC processors to control FRC robots.
  #10   Spotlight this post!  
Unread 08-12-2006, 16:43
Qbranch Qbranch is offline
wow college goes fast.
AKA: Alex
FRC #1024 (Kil-A-Bytes)
Team Role: Alumni
 
Join Date: Apr 2006
Rookie Year: 2006
Location: Indianapolis
Posts: 1,174
Qbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond reputeQbranch has a reputation beyond repute
Re: incrementing

*chuckle

well yeah theres lots of ways to say it, but it all compiles the same. personally, <operator>= is my favorite. but hey, thats just me.

see you all later... i'll be rockin out to them xm seasonal channels

-Q
__________________
Electrical Engineer Illini
1024 | Programmer '06, '07, '08 | Driver '08
  #11   Spotlight this post!  
Unread 09-12-2006, 23:40
TimCraig TimCraig is offline
Registered User
AKA: Tim Craig
no team
 
Join Date: Aug 2004
Rookie Year: 2003
Location: San Jose, CA
Posts: 221
TimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to beholdTimCraig is a splendid one to behold
Re: incrementing

Quote:
Originally Posted by Alan Anderson View Post
Here's something few people know about C: pointer arithmetic scales integers by the size of the type being pointed to. For example, if ptr is a pointer to an int, then ptr++ will add 2 (or whatever the native size of an int is for a particular target). If parr is a pointer to a structure of twelve bytes, parr = parr + 3 will add 36.

But don't abuse this fact. Pointer arithmetic is not necessarily something useful for programming PIC processors to control FRC robots.
That's true, however using a pointer that way for general arithmentic and casting it back to an int type would definitely be abusive and the programmer need not apply for a job in my shop.

One place I've used pointer arithmetic along those lines with the PIC is to be able to treat the PWM outputs as an array. Although there the increment in address defaults back to 1.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 20:43.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi