Hi,

I got a doubt about for loop while i was thinking about time complexity....

Is increment operation in for loop a two steps operation i.e., increment i and assign it the i or an one step operation just like just incrementing it?

Is there any pointer operation involved in this?

Recommended Answers

All 7 Replies

It's a two step operation. The increment operation increments i and also assigns the increment to i.

Is there any pointer operation involved in this?

It's hard to answer your question without a code sample of what you're talking about. Please elaborate.

It's hard to answer your question without a code sample of what you're talking about. Please elaborate.

Actually i'm not speaking of a particular program...

for(i=0;condition;i++)

in this, is i++ is a one step instruction or i=i+1?
I mean, if there is any inbuilt thing for for loop increment which accesses address of i address and increments it.
thank you....

Let's break this down somewhat; the increment operator isn't specific to for() loops, and for that matter, for() loops don't necessarily use an increment.

Let's take the simplest case of a separate increment expression:

i++;

Now, whether this compiles to a single assembly language operation or not depends on the processor type, and on how the compiler optimizes the variables as well. On most processors, incrementing a register is a single, atomic operation, but incrementing a memory value may require a separate load and store, which would mean at least two and possibly three operations. Even if the CPU type has an 'increment memory' operation (like the x86 that most people use), it may be slower than the equivalent register operation, sometimes by orders of magnitude depending on all sorts of things such as cache misses. Thus, it all comes down to what processor it is on, and how the compiler paints the registers, and to some extent where in the memory the variable is.

Note that all of this isn't necessarily relevant to algorithmic time complexity; that's usually stated in a manner that abstracts away the actual time for the operations, focusing instead on how many repetitions are needed by the algorithm.

in this, is i++ is a one step instruction or i=i+1?

It really depends on the underlying processor, but any decent compiler will use the most efficient instruction(s) to accomplish the task. Further, this isn't something you should be worrying about, as that kind of micro-management of instructions is often unnecessary.

i++ is same as i=i+1, and its required as per conditions of program.
inbuilt thing unable to get your words. Machine only understand the thing which we perform and follow our instructions. if i=0 then how it will increment automatically as machine is not having any sense until we tell him to do.
for decrement you can use like,

n=5;
while(n--)
{
    printf("hi ");
}

when n=0, while(0) condition wull false and automatically loop will end.

Still your words and not understood , refer with exact code.

i++ is same as i=i+1, and its required as per conditions of program.
inbuilt thing unable to get your words. Machine only understand the thing which we perform and follow our instructions. if i=0 then how it will increment automatically as machine is not having any sense until we tell him to do.
for decrement you can use like,

n=5;
while(n--)
{
    printf("hi ");
}

when n=0, while(0) condition wull false and automatically loop will end.

Still your words and not understood , refer with exact code.

I'm talking about c library and its compiler...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.