From 22e9b6ee58aee783b7b562261a07a183b1dd4cb9 Mon Sep 17 00:00:00 2001 From: Yves Wienecke <30377629+iyves@users.noreply.github.com> Date: Mon, 29 Oct 2018 20:13:12 -0700 Subject: [PATCH] Added example of common mistake people have with do whiles (#20375) --- guide/english/cplusplus/do-while-loop/index.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/guide/english/cplusplus/do-while-loop/index.md b/guide/english/cplusplus/do-while-loop/index.md index a881350578..e182211fd3 100644 --- a/guide/english/cplusplus/do-while-loop/index.md +++ b/guide/english/cplusplus/do-while-loop/index.md @@ -39,3 +39,16 @@ Do something first and then test if we have to continue. The result is that the return 0; } ``` + +A common mistake with do-while loops is attempting to use a variable that is local to the loop in the conditional statement. This will result in a compilation error because the local variable is not in the correct scope for the conditional statement to use it. Be sure to declare any conditional variables outside of the do while loop. +```cpp +int main() +{ + do { + // This will not compile because the counter is in the scope of the do, whereas the while conditional is outside. + int counter = 10; + } while (counter < 10); + return 0; +} + +```