-
Loop variable post-execution
over 7 years ago
-
over 7 years ago
int i=0;
for(int i=0;i<5;i++){
//code goes here
}
Loop has 3 parts
initialization
condition
increement
flow goes this way
initialization, checks the condition(above loop will check for i, If i is less than 5), increement and run the loop statement
initialized with 0, check the condition(if i is still less than 5), increement i++(i=1), run the loop statement
check the condition (if i is still less than 5), increement i++(i=2), run the loop statement
check the condition (if i is still less than 5), increement i++(i=3), run the loop statement
check the condition (if i is still less than 5), increement i++(i=4), run the loop statement
check the condition (if i is still less than 5), increement i++(i=5), run the loop statement
check the condition (if i is still less than 5)- No value of i has become 5 now, so looping process is finised
And now, i(loop variable) is outside of the valid range of the condition, i.e value of i is not less than 5 now, it has become 5
Hope it solves your query, Questions are always welcome.
Cheers
Happy Coding!!!!
-
1 Answer(s)