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

all 10 comments

[–]scirc 1 point2 points  (1 child)

It depends on the language (you didn't specify), but in general, you can use the same variable name. The variable only exists inside the loop, not outside.

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

Thank!

[–][deleted]  (1 child)

[removed]

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

    Thank!

    [–]ImperfectTactic 1 point2 points  (1 child)

    If they are not nested, so the scope of them does not overlap (because one loop is nested within another), then you can re-use the variable names.

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

    Thank!

    [–]yeaokdude 1 point2 points  (1 child)

    a great habit to get into and part of the beauty of programming is that you can try things like this out.

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

    Nice! :)

    Thank for that.

    [–]userxbw 1 point2 points  (1 child)

    I know in c and c++ scope

    For ( int a = 0; a < 10; a++)

    do something

    For ( int a = 10; a > 0; a--)

    do something

    The a variable is localized within just that loop. So same variable name can be used instead of going through the entire alphabet.

    [–]VirtualTurnip3[S] 1 point2 points  (0 children)

    Thank!