+ 1

While loop in C++??

Could anyone explain to me the while loop in C++ with example codes? I am not understanding SoloLearn's explanation.

21st May 2018, 3:13 PM
Arjun Ajesh Pillai
Arjun Ajesh Pillai - avatar
5 odpowiedzi
+ 3
The while loop takes a condition like an if but unlike the if block, while repeats the code as long as the condition is true int x = 1; while(x <= 5) { cout << x << "\n"; x++; // same as x += 1 or x = x + 1 } The code above will print 1 to 5 on each line because... x (1) <= 5: true x (2) <= 5: true ... x (5) <= 5: true x (6) <= 5: false because x is 6 which is not equal or smaller than 5 so the loop stops
21st May 2018, 3:21 PM
TurtleShell
TurtleShell - avatar
+ 2
Loops have a lot of use Infinite loops (one that never ends because of the condition given, like... while (true)) in videogames For many algorithms (sorting, searching ect) Repetition of code Read a file line by line
21st May 2018, 3:29 PM
TurtleShell
TurtleShell - avatar
+ 1
A loop repeatedly executes a set of statements until a particular condition is satisfied
21st May 2018, 7:53 PM
Ken Kipruto
Ken Kipruto - avatar
+ 1
think about the countdown system. it was not manually input by the user but it was generated by the machine that has a while loop condition.
23rd May 2018, 3:25 PM
Junelle Zuela
Junelle Zuela - avatar
0
Thanks!!! Could you tell me some of its applications ,please???
21st May 2018, 3:26 PM
Arjun Ajesh Pillai
Arjun Ajesh Pillai - avatar