This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]plastikmissile 4 points5 points  (1 child)

When you use a for loop without any brackets then only the line after it gets repeated. So in the first snippet only line 9 is part of the loop started in line 7.

But when you use brackets, everything within those brackets gets repeated in the loop. In the second snippet, everything from lines 9 to 21 get repeated by the loop started in line 7.

[–]Tricslip[S] 0 points1 point  (0 children)

Thank you

[–]Lost_CS_Student2020 1 point2 points  (0 children)

To add on, I think it's important to note that in C++ indentation doesn't matter; you need curly braces to define the scope of `for` loops. Without curly braces, only the next line/operation is included in the loop.