While loops are awesome. We will use it when there's need for some operation to run all the time, or until specific top level is reached.
First run this code, and then we will explain:
#include <iostream>
using namespace std;
int main() {
int counter = 0;
while (counter <= 20) {
cout << "Counter at the moment: " << counter << endl;
counter = counter + 1;
}
return 0;
}
Result:
Counter at the moment: 0
Counter at the moment: 1
Counter at the moment: 2
Counter at the moment: 3
Counter at the moment: 4
Counter at the moment: 5
Counter at the moment: 6
Counter at the moment: 7
Counter at the moment: 8
Counter at the moment: 9
Counter at the moment: 10
Counter at the moment: 11
Counter at the moment: 12
Counter at the moment: 13
Counter at the moment: 14
Counter at the moment: 15
Counter at the moment: 16
Counter at the moment: 17
Counter at the moment: 18
Counter at the moment: 19
Counter at the moment: 20
Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
First we must establish initial counter to be 0, OUTSIDE while loop:
int counter = 0;
Then while loop where counter must reach 20.
Inside curly brackets we will have operations of simple printing, but also for every pass inside while loop counter must be incremented by one. To do that we will say "counter = counter + 1;":
while (counter <= 20) {
cout << "Counter at the moment: " << counter << endl;
counter = counter + 1;
}
Perhaps "counter = counter + 1;" looks a bit weird but it's very usefull and we will use it all the time in programming. Sure, you can increment by any other number not just by one.
While loop with Input from Keyboard
In this source we will set counter starting position using keyboard. Jumps are hard-coded and set to be 5. Until now you know how to get stuff from keyboard using cin command.
Run this code with value 50, for example:
#include <iostream>
using namespace std;
int main() {
int counter;
cout << "Enter Starting Position: " << endl;
cin >> counter;
while (counter <= 100) {
cout << "Counter at the moment: " << counter << endl;
counter = counter + 5;
cout << "------------------------" << endl;
}
return 0;
}
Result:
Enter Starting Position:
50
Counter at the moment: 50
------------------------
Counter at the moment: 55
------------------------
Counter at the moment: 60
------------------------
Counter at the moment: 65
------------------------
Counter at the moment: 70
------------------------
Counter at the moment: 75
------------------------
Counter at the moment: 80
------------------------
Counter at the moment: 85
------------------------
Counter at the moment: 90
------------------------
Counter at the moment: 95
------------------------
Counter at the moment: 100
------------------------
Process returned 0 (0x0) execution time : 4.583 s
Press any key to continue.
Now we will have example with all 3 values set using keyboard. Values for starting position, top_level and jump (steps).
First we need 3 variables:
int counter;
int top_level;
int jump;
And 3 cins to grab values:
cout << "Enter Starting Position: " << endl;
cin >> counter;
cout << "Enter Top Level: " << endl;
cin >> top_level;
cout << "Enter Jump : " << endl;
cin >> jump;
In while loop we substituted hard-coded values with variables in their appropriate places:
while (counter <= top_level) {
cout << "Counter at the moment: " << counter << endl;
counter = counter + jump;
cout << "------------------------" << endl;
}
This is full source:
#include <iostream>
using namespace std;
int main() {
int counter;
int top_level;
int jump;
cout << "Enter Starting Position: " << endl;
cin >> counter;
cout << "Enter Top Level: " << endl;
cin >> top_level;
cout << "Enter Jump : " << endl;
cin >> jump;
while (counter <= top_level) {
cout << "Counter at the moment: " << counter << endl;
counter = counter + jump;
cout << "------------------------" << endl;
}
return 0;
}
Result for counter = 10, top_level = 50, jump = 5 using keyboard:
Enter Starting Position:
10
Enter Top Level:
50
Enter Jump :
5
Counter at the moment: 10
------------------------
Counter at the moment: 15
------------------------
Counter at the moment: 20
------------------------
Counter at the moment: 25
------------------------
Counter at the moment: 30
------------------------
Counter at the moment: 35
------------------------
Counter at the moment: 40
------------------------
Counter at the moment: 45
------------------------
Counter at the moment: 50
------------------------
Process returned 0 (0x0) execution time : 7.744 s
Press any key to continue.
Very useful.
No comments:
Post a Comment