473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Why is i++ used instead of ++i in array for loops

hey i wna know that y we usei++ instead of ++i in array proegram for loops
Feb 19 '13 #1
3 1643
weaknessforcats
9,208 Expert Mod 8TB
Using i++ is less efficient than ++i.

The reason is that ++i immediately increments the variable whereas i++ makes a copy of the variable and increments the copy. So i++ is slower by the amount of time required to make that copy.

Example:

Expand|Select|Wrap|Line Numbers
  1. int i = 10;
  2.  
  3. if (i++ == 11)   //here i is 10. It won't be 11 until the next statement
  4. {
  5. }
  6. int j = 10;   
  7.  
  8. if (++j == 11)   //here j is 11
  9. {
  10.  
  11. }
  12.  
If you are just incrementing a variable as in a loop then either i++ or ++i will work because you are not testing the variable but only incrementing it. Good programming will use ++i in these situations.
Feb 19 '13 #2
donbock
2,426 Expert 2GB
@weaknessforcats: should line 3 be i++?

Either form of autoincrement can be used in loops. i++ typically indicates that i enters the statement with the value to use in this iteration and exits the statement with the value to use in the next iteration. ++i typically indicates that i enters the statement with value used in the previous iteration and exits the statement with the value used in this iteration. These are generalities -- it would be pretty easy to provide examples that violate these statements.
Feb 21 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Yes it should. I fixed the line.
Feb 21 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Efim | last post by:
Hi all, Due to performance issue, I want to pevent execution of ToString() function in the code like the following: if(reporting_level & DEBUG_LEVEL) log(reporting_level,string.Format("Event of...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.