why does it always printone number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why does it always printone number

int num=1; while(num>6){ cout<<num<<endl; num=num++; } why it doesnt print 12345?

23rd Sep 2016, 9:30 AM
Alion_Lee
Alion_Lee - avatar
3 Answers
+ 3
also your condition is false. change it to num<6
23rd Sep 2016, 12:12 PM
Zaman
Zaman - avatar
+ 2
It actually doesn't print anything, right? The first line says that num is 1, and the second asks if 1 is bigger then 6 which is false. Though it never enters in your while loop. The correct would be: int num=1; while (num < 6){ cout << num << endl; num++; }
23rd Sep 2016, 12:04 PM
Vinícius Dias
Vinícius Dias - avatar
+ 2
if you want increase your counter, in this case num, you can use one of this statements: num++; ++num; num = num + 1; num = ++num; apparently your way of increasing num is known as "undefined behaviour" and it is a complicated issue to understand by new programmers such me. so dont use this: num = num++;
23rd Sep 2016, 12:10 PM
Zaman
Zaman - avatar