0
The while Loop
#include <iostream> using namespace std; int main() { int num = 1; while (num < 6) { cout << "Number: " << num << endl; num = num + 1; } return 0; } /* Outputs Number: 1 Number: 2 Number: 3 Number: 4 Number: 5 */ Why it is not starting with Number: 2? When we have below details: int num = 1; num = num + 1; ?
2 Answers
0
You incement num after printing it so the value of num before the incement will be printed
0
if you want to start from Number: 2 num need to be 2 and
cout << "Number: " << num << endl; is before num = num + 1; which means num is still 1.
just need to change num to 2.
int main()
{
int num = 2;
while (num < 6) {
cout << "Number: " << num << endl;
num = num + 1;
}
return 0;
}