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
Output when using pre decrement in for loop in C - Stack Overflow
c - Which loop has better performance? Increment or decrement? - 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.
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)
{
}
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');