I didn't understand this while loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I didn't understand this while loops

6th Feb 2018, 10:39 AM
Kabir Kamal
Kabir Kamal - avatar
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; }
6th Feb 2018, 10:50 AM
Frost
Frost - avatar
+ 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; }
6th Feb 2018, 11:07 AM
Kabir Kamal
Kabir Kamal - avatar
+ 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.
6th Feb 2018, 4:24 PM
Talluri Saisumanth
Talluri Saisumanth - avatar