I think you mean > instead of <:
for (int i = 10; i > 0; --i) {
If you want the values of i to be the same as in the original code except in reverse order (i.e. 9, 8, 7, ..., 1, 0) then you also need to change the boundaries:
for (int i = 9; i >= 0; --i) {
Answer from Mark Byers on Stack OverflowI think you mean > instead of <:
for (int i = 10; i > 0; --i) {
If you want the values of i to be the same as in the original code except in reverse order (i.e. 9, 8, 7, ..., 1, 0) then you also need to change the boundaries:
for (int i = 9; i >= 0; --i) {
I just want to add to keep in mind that --i decrements first, then checks and i-- checks, then decrements.
Decrementing Initialization of a For Loop (C) - EX: for(i--; i>0; i--) - Stack Overflow
decrement size_t in loop - C++ Forum
c - Which loop has better performance? Increment or decrement? - Stack Overflow
For loop in C, with automatic increment or decrement depending on init and end conditions - Stack Overflow
Videos
In your version of the loop you print the '\r' character. This is a carriage return character. The result is that printf overwrites already written characters. Please see this question: What's the Use of '\r' escape sequence?
The loop as written will count down from 99 to 1, overprinting each time. Since you have no delay in the loop, it will run faster than you can see and you'll end up with just 1 on the terminal. Add a sleep(1); to the loop just after the printf and you'll better see what is happening.
The second "may" be better, because it's easier to compare i with 0 than to compare i with 10 but I think you can use any one of these, because compiler will optimize them.
I do not think there is much difference between the performance of both loops.
I suppose, it becomes a different situation when the loops look like this.
for(int i = 0; i < getMaximum(); i++)
{
}
for(int i = getMaximum() - 1; i >= 0; i--)
{
}
As the getMaximum() function is called once or multiple times (assuming it is not an inline function)
naja, e.g. simple approach:
int inc=startVal>stopVal?-1:1;
for (int i=startVal ; i != stopVal ; i+=inc ) {
Not sure how tricky that needs to be, should be as easy as:
for(int i = startVal; i != stopVal; i < stopVal ? i += stepSize : i -= stepSize)
{
}
Note that this assumes that stopVal is an integer number of stepSize steps from startVal. If that's not true, it will overshoot.
It's of course possible to protect against that too, but it becomes a bit unwieldy:
for(int i = startVal;
stopVal > startVal ? i < stopVal : i > stopVal;
i < stopVal ? i += stepSize : i -= stepSize)
{
}
You can use any expression:
int n = 10;
while (n > 0) // Note change compared with original!
{
// Do something
n = round(n/3.0) - 1; // Note assignment and floating point
}
Note that you can only decrement variables, not expressions.
You could also use a for loop:
for (int n = 10; n > 0; n = round(n/3.0) - 1)
{
// Do something
}
In this case, the sequence of values for n will be the same (n = 10, 2) whether you round using floating point or not, so you could write:
n = n / 3 - 1;
and you'd see the same results. For other upper limits, the sequence would change (n = 11, 3). Both techniques are fine, but you need to be sure you know what you want, that's all.
Yes, it is possible to add or subtract any number to your variable n.
Usually, if you want to do something a very predictable number of times, you would use a for loop; when you aren't sure how many times something will happen, but rather you are testing some sort of condition, you use a while loop.
The rarest loop is a do / while loop, which is only used when you want to execute a loop one time for certain before the first time the while check occurs.
Examples:
// do something ten times
for (i = 0; i < 10; ++i)
do_something();
// do something as long as user holds down button
while (button_is_pressed())
do_something();
// play a game, then find out if user wants to play again
do
{
char answer;
play_game();
printf("Do you want to play again? Answer 'y' to play again, anything else to exit. ");
answer = getchar();
} while (answer == 'y' || answer == 'Y');