+ 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.
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
+ 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
+ 1
A loop repeatedly executes a set of statements until a particular condition is satisfied
+ 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.
0
Thanks!!!
Could you tell me some of its applications ,please???