0

Can somebody explain me this code

int num = 1; int number; while (num <= 5) { cin >> number; num++; }

18th Dec 2017, 1:22 PM
Paawani Chauhan
Paawani Chauhan - avatar
4 Réponses
+ 3
first line you are declaring a variable of type integer and assigned a value of 1. (the counting) 2nd line, you are declaring another integer with no value assigned. in the while loop, you will be asked to enter number continuously until you num becomes 5. there will not be any output
18th Dec 2017, 1:46 PM
Core i9
Core i9 - avatar
+ 2
this is what happens, first loop. while (1<=5){ cin>>number //user enters a number n++;} //1+1 while (2<=5){ cin>>number //user enters number n++;} //2+1 ..... while (5<=5){ cin>>number //user enters a number n++;} //now 5+1 is greater than 5 so loop stops in the entire process the value of int number gets overwritten by the user entered value. To see output, use this Instead. while(num<=5){ cin>>number; cout<<number; num++;}
18th Dec 2017, 1:52 PM
Core i9
Core i9 - avatar
+ 1
thanks broo
18th Dec 2017, 2:22 PM
Paawani Chauhan
Paawani Chauhan - avatar
+ 1
NP
18th Dec 2017, 3:08 PM
Core i9
Core i9 - avatar