cplusplus:repetition_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cplusplus:repetition_examples [2019/02/12 02:17] – [increment operator] 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: | ||
</ | </ | ||
- | ===== Counter-controlled repetition | + | |
+ | ===== Repetition code patterns ===== | ||
+ | |||
+ | ==== Counter-controlled repetition ==== | ||
<file c++ counter-controlled-repetition.cpp>// | <file c++ counter-controlled-repetition.cpp>// | ||
#include < | #include < | ||
Line 48: | Line 50: | ||
{ | { | ||
const int UPPER_LIMIT = 5; | const int UPPER_LIMIT = 5; | ||
- | int num = 1; | + | int num = 1; // initialize the counter |
- | while (num <= UPPER_LIMIT) | + | while (num <= UPPER_LIMIT) |
{ | { | ||
cout << num << endl; | cout << num << endl; | ||
- | num++; | + | num++; |
} | } | ||
return 0; | return 0; | ||
}</ | }</ | ||
- | ===== Sentinel-controlled repetition | + | |
+ | ==== Sentinel-controlled repetition ==== | ||
<file c++ simple-sentinel.cpp> | <file c++ simple-sentinel.cpp> | ||
Line 94: | Line 97: | ||
</ | </ | ||
- | ===== Input validation | + | ==== Input validation ==== |
<file c++ simple-input-validation.cpp>// | <file c++ simple-input-validation.cpp>// | ||
#include < | #include < | ||
Line 124: | Line 127: | ||
</ | </ | ||
- | ===== Running total ===== | + | ==== Running total ==== |
<file c++ simple-running-total.cpp>// | <file c++ simple-running-total.cpp>// | ||
// Total all the integers between a lower and an upper bound. | // Total all the integers between a lower and an upper bound. | ||
Line 140: | Line 143: | ||
while (num <= UPPER_BOUND) | while (num <= UPPER_BOUND) | ||
{ | { | ||
- | sum += num; // add the new value to the sum | + | sum += num; // add the new value to the accumulator |
num++; | num++; | ||
} | } | ||
Line 152: | Line 155: | ||
</ | </ | ||
- | ===== for ===== | + | |
+ | ===== The for statement | ||
<file c++ simple-for.cpp>// | <file c++ simple-for.cpp>// | ||
#include < | #include < |
cplusplus/repetition_examples.1549937845.txt.gz · Last modified: 2019/02/12 02:17 by mithat