Loops/While: Difference between revisions

→‎{{header|C++}}: writing the while loop with for; corrected lang tag
(→‎{{header|C++}}: writing the while loop with for; corrected lang tag)
Line 68:
 
=={{header|C++}}==
<lang c++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>
Indeed, in C++,
<lang cpp>
for (init; cond; update)
statement;
</lang>
is equivalent to
<lang cpp>
{
init;
while (cond)
{
statement;
update;
}
}
</lang>
 
=={{header|C sharp|C#}}==
973

edits