1 1 vote In the picture below:Can anyone explain to me why ‘while’ loop starting from 2 but in the case of ‘for’ loop it is starting from 1 (although i++ is available for both in Program 1 and Program 2) and also in the case of ‘while’ loop the outputs are different in Program 1 and Program 3 but in case of ‘for’ loop the outputs are same although in Program 2 it is post increment (i++) and in Program 4 it is pre increment (++i). Programming in C programming loop + – gopal_gate 974 views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 2 2 votes In for loop, ++i or i++ is executed after entire loop body is executed. After ++i/i++, condition is checked if it is true it will execute the loop body else will exit the loop. So it won’t make any diff in program 2 and 4. But in case of while ++i/i++ is done in the condition itself, so it will follow the pre increment (++i) and post increment (i++)/ Pre increment – ++i → i value will be incremented first then will be used in the expression. So in program 3 it will iterate from 2 to 10 and values printed will be 2 to 10. Post increment – ++ → i value will be used in the expression then it will be incremented. So in program 4 it will iterate from 1 to 10 but values printed will be 2 to 11. simi2426 answered Jul 29, 2023 • selected Oct 22, 2023 by gopal_gate simi2426 comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes The property of for loop is that it will execute the i++ part after the execution of the body. But in the while loop i++ part is executing before the body. That’s why you are getting these output. gopal_gate answered Sep 14, 2023 gopal_gate comment Share Follow 0 reply Please log in or register to add a comment.