-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathex10_21.cpp
More file actions
33 lines (29 loc) · 741 Bytes
/
ex10_21.cpp
File metadata and controls
33 lines (29 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*!
* @question:
* Write a lambda that captures a local `int` variable and
* decrements that variable until it reaches 0.
* Once the variable is 0 additional calls should
* no longer decrement the variable.
* The lambda should return a `bool` that indicates
* whether the captured variable is 0.
* @author: pezy
* @date: 2016-07-01
* @see:
*/
#include <iostream>
using std::cout;
using std::endl;
int main()
{
// local int variable
int local_val = 7;
auto decrement_to_zero = [&local_val]() {
if (local_val == 0)
return true;
else {
--local_val;
return false;
}
};
while (!decrement_to_zero()) cout << local_val << endl;
}