<p>I know I must be missing something, but in a <code>while</code> statement how does the variable hold the data, when it finishes the first pass and goes into the second pass?</p>

<hr>

<p>{</p>

<pre><code>int num1 = 0 ;
int num2 = 0;
int num3 = 0;

while (num1 &lt; 10)   

{cout &lt;&lt; "enter your first number:  ";
cin &gt;&gt; num1;

cout &lt;&lt; "Enter your second number:  ";
cin &gt;&gt; num2;

num1 = num1 + num2 ; 

    cout &lt;&lt; "Number 1 is now: " &lt;&lt; num1 &lt;&lt;endl;

    cout &lt;&lt; "Enter Number 3: " ;
    cin &gt;&gt; num3;

    num1 = num1 + num3;

    cout &lt;&lt; "Number 1 is now: " &lt;&lt; num1 &lt;&lt; endl;
    num1++;
};
</code></pre>

<p>In this code. The Variable doesn't hold the data.  I'm not sure what I'm doing wrong!</p>

[{'answer_id': 36133, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not sure I understand your question. In C any data that's not overwritten is carried over into the next iteration of the loop, and imagine that C++ works much the same way.</p>\n"}, {'answer_id': 36155, 'author': 'James Sutherland', 'author_id': 1739, 'author_profile': 'https://Stackoverflow.com/users/1739', 'pm_score': 2, 'selected': False, 'text': '<p>I\'m not clear exactly what you\'re asking, but variables will maintain their value for each iteration of a loop, as long as they\'re declared outside of the loop itself. For example:</p>\n\n<pre><code>int a = 0;\n\nwhile(a &lt; 10)\n{\n    int b = 0;\n\n    cout &lt;&lt; "a: " &lt;&lt; a &lt;&lt; " b: " &lt;&lt; b &lt;&lt; "\\n";\n\n    a++;\n    b++;\n}\n</code></pre>\n\n<p>In the above, the value output for b will always be 0, as it\'s declared inside the loop and is being reinitialized each time, whereas a will maintain its value and get incremented each iteration. If b were an object, rather than an int, its constructor and destructor would get called each iteration.</p>\n'}, {'answer_id': 36246, 'author': 'James Sutherland', 'author_id': 1739, 'author_profile': 'https://Stackoverflow.com/users/1739', 'pm_score': 3, 'selected': True, 'text': "<p>Is num1 the variable you're having trouble with? This line:</p>\n\n<pre><code>cin &gt;&gt; num1;\n</code></pre>\n\n<p>is setting num1 to the value input by the user. So the value calculated for it in the previous run through the loop is being overwritten each time by the new input.</p>\n"}, {'answer_id': 38252, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 1, 'selected': False, 'text': '<p>Do you understand how when you say "num1" you\'re referring to the same variable each time, and that each time you change num1 you replace the previous value?</p>\n'}]
['https://Stackoverflow.com/questions/36114', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/335696/']