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 Overflowdecrement size_t in loop - C++ Forum
Decrementing Initialization of a For Loop (C) - EX: for(i--; i>0; i--) - Stack Overflow
Solved! help with decrementing a for loop
Output when using pre decrement in for loop in C - Stack Overflow
Videos
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) {
I just want to add to keep in mind that --i decrements first, then checks and i-- checks, then decrements.
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.
Each for loop can be replaced with a while loop. The initializing statement (1st part of for) comes first, then the conditional loop (condition: 2nd part of for), and the loop statement (3rd part of for) is executed right before the loop repeats.
Your loop:
for (i = n; i >= 1; --i)
{
printf("%d ", i);
}
is equivalent to:
i = n;
while (i>=1)
{
printf("%d ", i);
--i;
}
So the output is correct, and your expectation is wrong.
But why has the for loop the loop statement in its parentheses, so that some beginners assume it executes before the loop body?
This shows the coherence between all the loop's parts. Consider a loop body of multiple lines. The trailing loop statement (for example, increment the index) would be far away from the leading initialization statement and the condition. And this is more work for your brain to grasp.
First: you don't need the "stdlib.h" - remove this line.
Second: always check your code. Debug it with pen and paper if necessary. It may look ridiculous to you, but is one sure way you'll "walk" through your logic, step by step and will check what results is your code producing.
You can solve your problems several ways, but the first solution which came to my mind is to asign n-1 to i, in the begining of the for-loop:
for (i = n - 1; i >= 1; --i)
{
printf("%d ",i);
}
Output:
Enter the number 6
1 2 3 4 5 6
5 4 3 2 1
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)
{
}