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 (int i = 1024; i > 0; i /= 2)
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 (init; cond; update){
statement;</lang>
}</lang>
is equivalent to
<lang cpp>{
init;
while (cond){
{
statement;
update;
22

edits