cplusplus:repetition_examples
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cplusplus:repetition_examples [2013/03/01 01:05] – created mithat | cplusplus:repetition_examples [2019/02/14 21:28] (current) – [Running total] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Control Structures: Repetition examples ====== | ====== Control Structures: Repetition examples ====== | ||
- | ===== while ===== | + | ===== The while statement |
<file c++ simple_while.cpp>// | <file c++ simple_while.cpp>// | ||
Line 34: | Line 34: | ||
cout << num << endl; | cout << num << endl; | ||
num++; | num++; | ||
- | cout << num << endl; | ||
return 0; | return 0; | ||
Line 40: | Line 39: | ||
</ | </ | ||
- | ===== Sentinel-controlled repetition ===== | ||
- | <file c++ simple-sentinel.cpp>// This program | + | ===== Repetition code patterns ===== |
- | // enters -999 when finished. | + | |
+ | ==== Counter-controlled repetition ==== | ||
+ | <file c++ counter-controlled-repetition.cpp>// This program | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 49: | Line 49: | ||
int main() | int main() | ||
{ | { | ||
- | int num = 1; | + | |
+ | | ||
+ | |||
+ | while (num <= UPPER_LIMIT) | ||
+ | { | ||
+ | cout << num << endl; | ||
+ | num++; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | }</ | ||
+ | |||
+ | ==== Sentinel-controlled repetition ==== | ||
+ | |||
+ | <file c++ simple-sentinel.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const unsigned int PASSING_SCORE = 60; | ||
+ | int score; | ||
+ | |||
+ | cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; | ||
+ | cout << "Enter -1 to quit: "; | ||
+ | cin >> score; | ||
+ | |||
+ | while (score != -1) | ||
+ | { | ||
+ | if (score >= PASSING_SCORE) | ||
+ | { | ||
+ | cout << | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | cout << | ||
+ | } | ||
+ | |||
+ | cout << "Enter a test scrore and I will tell you if it is a passing mark." << endl; | ||
+ | cout << "Enter -1 to quit: "; | ||
+ | cin >> score; | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Input validation ==== | ||
+ | <file c++ simple-input-validation.cpp>// | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const int LOWER_BOUND = 0; | ||
+ | const int UPPER_BOUND = 100; | ||
+ | int num; | ||
- | cout << " | + | cout << " |
- | | + | << " and " << UPPER_BOUND |
cin >> num; | cin >> num; | ||
- | while (num != -999) | + | |
+ | | ||
{ | { | ||
- | cout << num <<" | + | cout << "Enter a number between |
- | << endl; | + | << |
- | cout << "Enter an integer | + | |
- | cout << "Enter -999 to quit: "; | + | |
cin >> num; | cin >> num; | ||
} | } | ||
+ | |||
+ | cout << " | ||
return 0; | return 0; | ||
- | }</ | + | } |
+ | |||
+ | </ | ||
- | ===== for ===== | + | ==== Running total ==== |
- | <file c++ simple-for.cpp>// | + | <file c++ simple-running-total.cpp>// |
- | // using a for structure. | + | // Total all the integers between a lower and an upper bound. |
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const int LOWER_BOUND = 0; | ||
+ | const int UPPER_BOUND = 10; | ||
+ | |||
+ | int num = LOWER_BOUND; | ||
+ | int sum = 0; // accumulator | ||
+ | |||
+ | while (num <= UPPER_BOUND) | ||
+ | { | ||
+ | sum += num; // add the new value to the accumulator | ||
+ | num++; | ||
+ | } | ||
+ | |||
+ | cout << "The sum of integers from " << LOWER_BOUND | ||
+ | << | ||
+ | << | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== The for statement | ||
+ | <file c++ simple-for.cpp>// | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 109: | Line 197: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== break and continue ===== | ||
+ | <file c++ simple-break.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const int UPPER_LIMIT = 100; | ||
+ | int num = 1; // counter | ||
+ | char choice; | ||
+ | |||
+ | while (num <= UPPER_LIMIT) | ||
+ | { | ||
+ | cout << "2 to the " << num << " power is " << pow(2, num) << endl; | ||
+ | cout << " | ||
+ | cin >> choice; | ||
+ | if (choice == ' | ||
+ | break; | ||
+ | num++; | ||
+ | } | ||
+ | |||
+ | cout << " | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ |
cplusplus/repetition_examples.1362099913.txt.gz · Last modified: 2013/03/01 01:05 by mithat