+ 2
I didn't understand this while loops
3 Answers
+ 20
#ur libraries
int main(){
initialization
while(This condition is true){
//This will happen;
}
This will happen when the above condition is false and the loop breaks;
}
eg:-
#include <iostream>
using namespace std;
int main() {
int i=0;
while(i<5){
cout<<i;
i++;
}
cout<<endl<<"Done";
return 0;
}
+ 2
ya but how this example works?
#include <iostream>
using namespace std;
int main()
{
int num = 1;
int number;
int total = 0;
while (num <= 5) {
cin >> number;
total += number;
num++;
}
cout << total << endl;
return 0;
}
+ 2
till the condition is satisfied the loop will run and it will continuously take input from the user and add it to total variable.
finally it will display the sum of all the numbers entered.