+ 1

Is the output od the following program, right? Is it common to include the increment before the other statements in a loop?

int num = 1; while (num < 6) { num = num + 1; cout << "Number: " << num << endl; } /* Outputs Number: 2 Number: 3 Number: 4 Number: 5 Number: 6 */

27th May 2017, 8:28 PM
Pacha
3 Respuestas
+ 1
Yeah, doing: cout << "Number: " << ++num << endl; Will result in the same output.
27th May 2017, 8:57 PM
Rrestoring faith
Rrestoring faith - avatar
0
it makes no real difference
27th May 2017, 8:29 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar
0
Locating the Incremental value is based on the requirement. Usually while loop holds the increment/decrement at the end of the loop. Here we missed the output for value '1'.
28th May 2017, 1:42 AM
Dashlin Jeno
Dashlin Jeno - avatar