Differencebetween Pre-Increment and Post-Increment in the Cycle (For/While)

In a for loop, is there a difference between pre/post-incrementing a loop control variable in terms of the total quantity of iterations?

There is no difference. In older compilers, ++counter was faster because it did not create a temporary variable but all modern compilers can optimize that out. For heavy objects with custom (non-inlined) increment operators, ++counter can still be more efficient.

As for when evaluation takes place:

for (initialization; condition; increment/decrement)
code;

is evaluated as

{
initialization;
while (condition)
{
code;
increment/decrement;
}
}

Is there a performance difference between i++ and ++i in C?

Executive summary: No.

i++ could potentially be slower than ++i, since the old value of i
might need to be saved for later use, but in practice all modern
compilers will optimize this away.

We can demonstrate this by looking at the code for this function,
both with ++i and i++.

$ cat i++.c
extern void g(int i);
void f()
{
int i;

for (i = 0; i < 100; i++)
g(i);

}

The files are the same, except for ++i and i++:

$ diff i++.c ++i.c
6c6
< for (i = 0; i < 100; i++)
---
> for (i = 0; i < 100; ++i)

We'll compile them, and also get the generated assembler:

$ gcc -c i++.c ++i.c
$ gcc -S i++.c ++i.c

And we can see that both the generated object and assembler files are the same.

$ md5 i++.s ++i.s
MD5 (i++.s) = 90f620dda862cd0205cd5db1f2c8c06e
MD5 (++i.s) = 90f620dda862cd0205cd5db1f2c8c06e

$ md5 *.o
MD5 (++i.o) = dd3ef1408d3a9e4287facccec53f7d22
MD5 (i++.o) = dd3ef1408d3a9e4287facccec53f7d22

Pre increment vs post increment inside if check C++

Both your if conditions have undefined behavior, since you are modifying and accessing a variable in the same expression.

The program could print A, B, AB, nothing, or the program could crash your computer. Absolutely anything can happen when your program has undefined behavior.

Make it a habit to compile programs with warnings enabled, e.g. -Wall, and then the compiler will warn you that you are doing something wrong.

Why doesn't changing the pre to the post increment at the iteration part of a for loop make a difference?

The loop is equivalent to:

int x = 2;
{
int y = 2;
while (y > 0)
{
System.out.println(x + " "+ y + " ");
x++;
y--; // or --y;
}
}

As you can see from reading that code, it doesn't matter whether you use the post or pre decrement operator in the third section of the for loop.

More generally, any for loop of the form:

for (ForInit ; Expression ; ForUpdate)
forLoopBody();

is exactly equivalent to the while loop:

{
ForInit;
while (Expression) {
forLoopBody();
ForUpdate;
}
}

The for loop is more compact, and thus easier to parse for such a common idiom.

Difference between pre-increment and post-increment in a loop?

a++ is known as postfix.

add 1 to a, returns the old value.

++a is known as prefix.

add 1 to a, returns the new value.

C#:

string[] items = {"a","b","c","d"};
int i = 0;
foreach (string item in items)
{
Console.WriteLine(++i);
}
Console.WriteLine("");

i = 0;
foreach (string item in items)
{
Console.WriteLine(i++);
}

Output:

1
2
3
4

0
1
2
3

foreach and while loops depend on which increment type you use. With for loops like below it makes no difference as you're not using the return value of i:

for (int i = 0; i < 5; i++) { Console.Write(i);}
Console.WriteLine("");
for (int i = 0; i < 5; ++i) { Console.Write(i); }

0 1 2 3 4

0 1 2 3 4

If the value as evaluated is used then the type of increment becomes significant:

int n = 0;
for (int i = 0; n < 5; n = i++) { }

Why post-increment needs to make a copy while pre-increment does not

The difference is someval++ returns what someval is before the increment and to do that you need remember what someval is with a copy. How else would you return the original value while updating it if the original value wasn't stored somewhere?

Is there a difference between a for loop without increment vs. one with a non incremented variable?

Under normal circumstances: no, there is no difference.

The for statement creates a loop that consists of three optional
expressions, enclosed in parentheses and separated by semicolons,
followed by a statement (usually a block statement) to be
executed in the loop.

The three expressions (initialization, condition and final-expression) are all optional (unlike the semicolons themselves), so if you omit the final-expression (which is most commonly used to increment the i/index/counter) that part simply will not be used.

If you use an isolated i as the final-expression, then this will be evaluated after every iteration of the for-loop. But an isolated variable on its own usually doesn't have any side effects. The value wont change and the value that is returned by the i expression (the value of i) is ignored in case of the final-expression.