Loops/While: Difference between revisions
→{{header|C++}}: bit shift operator
(→{{header|C++}}: bit shift operator) |
|||
Line 314:
=={{header|C++}}==
<lang cpp>int i = 1024;
while(i > 0)
std::cout << i << std::endl;
i /= 2;
}</lang>
Alternatively, it can be done with <code>for</code>:
<lang cpp>for
std::cout << i << std::endl;</lang>
Instead of <code>i /= 2</code> one can also use the bit shift operator <code>i >>= 1</code> on integer variables.
Indeed, in C++,
<lang cpp>for
statement;
}</lang>
is equivalent to
<lang cpp>{
init;
while
statement;
update;
|