answersLogoWhite

0

Consider what the ++ operator does relative to the value returned by the expression.

In ++u (pre-increment) the value of u is incremented by one and then the value of that result is returned, that is the expression ++u has the value of u+1 (relative to the initial value of u).

Whereas in u++ (post-increment) the value of u is returned by the expression and then u is incremented by one, that is the expression u++ has the value of the initial u (whilst the value of u itself is incremented by one).

So why would it be preferable to use one over the over for a loop?

It all depends upon for what purpose the variable u was being put during the loop and/or after the loop has exited.

If the current value of the "loop" variable is used during the loop, a pre-increment on the while() condition ensures it is the current value that is used during the loop, and holds that value if the loop is exited before the condition (via break;) - this is particularly important with pointers (where it should be noted that the ++ operator increments by one thing to which it is pointing, not necessarily by 1 byte in memory).

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


What is a loop?

A loop is something that goes over and over infinity


What is CPU usage small programm c plus plus?

Example: int main (void) { LOOP: goto LOOP; }


What are sentinental loop c plus plus?

A sentinel loop is a loop that iterates over a series of values until a special "sentinel" value is encountered. For instance, when iterating over the characters in a string, the null-terminator acts as the sentinel value, indicating that the end of the string has been reached. Sentinel loops typically have the following form: while( get_value(value) != sentinel ) { // do something with value... }


Does 'for loop' works in C plus plus?

In C++, a for loop is structured as follows: for( int index = 0; index < 10; ++i ) { //do something }


Example of flowchart of while loop in c plus plus?

kk


C plus plus file processing that will input 12 integers?

#include<iostream> #include<vector> int main() { std::vector<int> integers (12); for (size_t loop=0; loop<integers.size(); ++loop) cin >> integers[loop]; }


How do you convert meters to centimeters in c plus plus using the for loop construct?

It is unnecessary to use a for loop to convert meters to centimeters. Just multiply by 0.01.


Why is PC preferred over laptop?

if you mean why is a desktop preferred over a laptop. then i would answer that they aren't and that laptops are preferred because of their convenience


How do you alternate the input numbers using for loop in c plus plus?

Input a variable.


How do you start finger knitting?

You wrap the wool twice around your finger, then put the back loop over, then loop one more, back loop over, and so on.