Someone already answered this question, but there's one more interesting thing to discuss. I noticed this question because it has the cppcheck tag. I guess you unintentionally touched upon an exciting topic :). As I can see, it’s your first question, so perhaps you thought that the "cppcheck" tag means "I have a problem in my code. Could you please check it?". Actually, "cppcheck" is the name of a static analyzer for searching code errors in C and C++.
Well, I found this topic interesting because actually static analysis can easily detect an error in the code fragment above. With a static analyzer you don't need to ask such questions. Therefore, analyzers can save your time – they will help review your code and find an error.
Unfortunately, Cppcheck failed to find the error – I went to the Cppcheck Online Demo webpage and checked the code fragment from this question. Unfortunately, this analyzer didn’t issue any warnings about the loop. Well, analyzers are not perfect, especially such relatively simple static analyzer. But it has at least two advantages — it's free and open source.
Well, I didn’t give up – I used another analyzer. That was the Online mode of the PVS-Studio static analyzer: https://godbolt.org/z/TEsdeG7oM
And can you imagine? PVS-Studio issued the warning that describes this error: V683 Consider inspecting the loop expression. It is possible that the i
variable should be incremented instead of the k
variable.
I hope you will find this information useful and interesting enough to use the static analyzer’s clues for learning C++ more efficiently.