C Programming - Control Instructions - Discussion
#include<stdio.h>
int main()
{
int i = 5;
while(i-- >= 0)
printf("%d,", i);
i = 5;
printf("\n");
while(i-- >= 0)
printf("%i,", i);
while(i-- >= 0)
printf("%d,", i);
return 0;
}
Step 1: Initially the value of variable i is '5'.
Loop 1: while(i-- >= 0) here i = 5, this statement becomes while(5-- >= 0) Hence the while condition is satisfied and it prints '4'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 2: while(i-- >= 0) here i = 4, this statement becomes while(4-- >= 0) Hence the while condition is satisfied and it prints '3'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 3: while(i-- >= 0) here i = 3, this statement becomes while(3-- >= 0) Hence the while condition is satisfied and it prints '2'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 4: while(i-- >= 0) here i = 2, this statement becomes while(2-- >= 0) Hence the while condition is satisfied and it prints '1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 5: while(i-- >= 0) here i = 1, this statement becomes while(1-- >= 0) Hence the while condition is satisfied and it prints '0'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 6: while(i-- >= 0) here i = 0, this statement becomes while(0-- >= 0) Hence the while condition is satisfied and it prints '-1'. (variable 'i' is decremented by '1'(one) in previous while condition)
Loop 7: while(i-- >= 0) here i = -1, this statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1
Step 2: Then the value of variable i is initialized to '5' Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1
Step 3: The third while loop, while(i-- >= 0) here i = -1(because the variable 'i' is decremented to '-1' by previous while loop and it never initialized.). This statement becomes while(-1-- >= 0) Hence the while condition is not satisfied and loop exits.
Hence the output of the program is
4,3,2,1,0,-1
4,3,2,1,0,-1
printf("/n %d/",i)
Why does it print i = 4 ? why not i = 5 ? in post decrement, at first time use, the value is unchanged, but in second use it's decremented, but here its decrementing in first use itself .
printf("/n %d/",i)
why does it print i = 4 ? why not i = 5 ? in post decrement , at first time use ,the value is unchanged , but in second use it's decremented ,but here its decrementing in first use itself .
How cum integer value of I is printed if %i is declared. ?
%i is a format specifier for integer same as %d.
In above we are using a post increament so the value printed from 4.
It is not in the use of first time. The post decrement means it will be decremented after the execution of that particular instruction.
But due to post-decrement property, the value of i will be decremented only after it had been used for comparison.
So at the first iteration, the condition is true because 5>=0 and then i is decremented. Therefore 4 will be printed.
Similarly the loop continues and the value of i keeps on decrementing.
Let us see what what happen when condition of while loop becomes 0 >= 0. At this time, condition is met and i is decremented.