Output when using pre decrement in for loop in C

132 views Asked by At

As you can see this the code in C, here I have used in for loop --i for decrement but I am not getting the expected output. Please explain

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i = 1;
    printf("Enter the number ");
    scanf("%d", &n);
    while (i <= n) {
        printf("%d ", i++);
    }
    printf("\n");
    for (i = n; i >= 1; --i) {
        printf("%d ", i);
    }
   return 0;
}

I am getting the output for n=6.

1 2 3 4 5 6
6 5 4 3 2 1

but I am expecting it to be like this

1 2 3 4 5 6
5 4 3 2 1
4

There are 4 answers

0
the busybee On BEST ANSWER

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.

0
Vlad from Moscow On

In this for loop

for(i=n;i>=1;--i)
{
    printf("%d ",i);
}

the variable i is initiallially set to n. As its value is not less than 1 then the value of n is outputted in the first iteration of the loop. After that before the second iteration of the loop the variable i is decremented.

The conditon of the loop evaluates to logical true when i is in the range [n, 1] where n is not less than 1.

Actually these two loops

for(i=n;i>=1;--i)
{
    printf("%d ",i);
}

and

for(i=n;i>=1; i--)
{
    printf("%d ",i);
}

are equivalent. It is unimportant whether you will use the pre-decrement operator --i or post-decrement operator i-- in the third expression of the for loop. In any case the value of the variable i will be the same after evaluting any of these expressions.

To get the expected result rewrite the for loop for example the following way

for (i = n; i > 1; )
{
    printf( "%d ", --i );
}
1
Sandrious On

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 
0
greg spears On

Here is a solution with explanation on why the initial code didn't meet expectations:

#include <stdio.h>

int main()
{
    int n,i=1;
    printf("Enter the number ");
    scanf("%d",&n); 
    while (i<=n)
    {
        printf("%d ",i++);
    }
    printf("\n%d \n", i); /* Updated: note that it prints '7' when input is '6'.  
    ** You/your code apparently expected i to be 6 ... but it's not the case*/

    /* This will do as expected */
    i = n;  /* Added. Assign i to n. */
    while(--i) /* Updated */
    {
        printf("%d ",i);
    }
    return 0;
}
/*-------------------    
Output:
Enter the number 6
1 2 3 4 5 6
7
5 4 3 2 1 
*-------------------*/