+ 1

Please explain this code?

#include <iostream> using namespace std; int main() { int num = 1; while (num < 6) { cout << "Number: " << num << endl; num = num + 1; } return 0; }

3rd Mar 2018, 6:54 AM
Kshitij
Kshitij - avatar
2 Answers
+ 16
while loop is execute till value of num<6 so num=1 so 1<6 true num=num+1 so num=1+1=2 2<6 true num=num+1 so num=2+1=3 3<6 true num=num+1 so num=3+1=4 4<6 true num=num+1 so num=4+1=5 5<6 true num=num+1 so num=5+1=6 num become 6 but it not goes in the loop because while loop condition false so number is print till 5 6<6 false loop break so output become number 1 number 2 number 3 number 4 number 5
3rd Mar 2018, 7:06 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
thank you very much gawen steasy
3rd Mar 2018, 7:12 AM
Kshitij
Kshitij - avatar