-
Notifications
You must be signed in to change notification settings - Fork 1
/
rust-31-loop-while-data-end-counter.rs
212 lines (210 loc) · 4.26 KB
/
rust-31-loop-while-data-end-counter.rs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
fn main() {
// rust - loop while model
// do: do work
// break: condition to break loop
// cnt: counter
// start/end: init cnt and stop cnt
// n: loop iteration times
// while = loop { break - do/cnt }
//----------------------------------------
// 1: do - break - cnt - start - end - n
let start = 1;
let end = 3;
let mut cnt = start;
loop {
// do
if cnt == end { break; }
cnt += 1;
}
//----------------------------------------
// 2: do - cnt - break - start - end+1 - n
let start = 1;
let end = 3;
let mut cnt = start;
loop {
// do
cnt += 1;
if cnt == end+1 { break; }
}
//----------------------------------------
// 3: break - do - cnt - start - end+1 - n+1
let start = 1;
let end = 3;
let mut cnt = start;
loop {
if cnt == end+1 { break; }
// do
cnt += 1;
}
//----------------------------------------
// 4: break - cnt - do - start - end+1 - n+1
let start = 1;
let end = 3;
let mut cnt = start;
loop {
if cnt == end+1 { break; }
cnt += 1;
// do
}
//----------------------------------------
// 5: cnt - break - do - start - end+2 - n+1
let start = 1;
let end = 3;
let mut cnt = start;
loop {
cnt += 1;
if cnt == end+2 { break; }
// do
}
//----------------------------------------
// 6: cnt - do - break - start - end+1 - n
let start = 1;
let end = 3;
let mut cnt = start;
loop {
cnt += 1;
// do
if cnt == end+1 { break; }
}
//----------------------------------------
// while = loop { break - do/cnt } end n
// while pre-check condition before loop
// while cnt <= end {
// do or cnt;
// }
//----------------------------------------
println!("1-3-use-end-cnt");
let mut i = 1;
let mut loop_cnt = 0;
loop {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
// use data
println!("use i: {}", i);
// end condition
if i == 3 {
break;
}
// counter
i += 1;
}
/*
1-3-use-end-cnt
loop cnt: 1, loop_in: 1
use i: 1
loop cnt: 2, loop_in: 2
use i: 2
loop cnt: 3, loop_in: 3
use i: 3
*/
println!("1-4-use-cnt-end");
let mut i = 1;
let mut loop_cnt = 0;
loop {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
// use data
println!("use i: {}", i);
// counter
i += 1;
// end condition
if i == 4 {
break;
}
}
/*
1-4-use-cnt-end
loop cnt: 1, loop_in: 1
use i: 1
loop cnt: 2, loop_in: 2
use i: 2
loop cnt: 3, loop_in: 3
use i: 3
*/
println!("1-4-end-use-cnt-one-more-iter");
let mut i = 1;
let mut loop_cnt = 0;
loop {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
// end condition
if i == 4 {
break;
}
// use data
println!("use i: {}", i);
// counter
i += 1;
}
/*
1-4-end-use-cnt-one-more-iter
loop cnt: 1, loop_in: 1
use i: 1
loop cnt: 2, loop_in: 2
use i: 2
loop cnt: 3, loop_in: 3
use i: 3
loop cnt: 4, loop_in: 4
*/
println!("0-3-end-cnt-use-one-more-iter");
let mut i = 0;
let mut loop_cnt = 0;
loop {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
// end condition
if i == 3 {
break;
}
// counter
i += 1;
// use data
println!("use i: {}", i);
}
/*
0-3-end-cnt-use-one-more-iter
loop cnt: 1, loop_in: 0
use i: 1
loop cnt: 2, loop_in: 1
use i: 2
loop cnt: 3, loop_in: 2
use i: 3
loop cnt: 4, loop_in: 3:w
*/
println!("while-1-3-end-use-counter");
let mut i = 1;
let mut loop_cnt = 0;
while i <= 3 {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
println!("use i: {}", i);
i += 1;
}
/*
while-1-3-end-use-counter-one-more-iter
loop cnt: 1, loop_in: 1
use i: 1
loop cnt: 2, loop_in: 2
use i: 2
loop cnt: 3, loop_in: 3
use i: 3
*/
println!("while-0-2-end-counter-use");
let mut i = 0;
let mut loop_cnt = 0;
while i <= 2 {
loop_cnt += 1;
println!("loop cnt: {}, loop_in: {}", loop_cnt, i);
i += 1;
println!("use i: {}", i);
}
/*
while-0-2-end-counter-use-one-more-iter
loop cnt: 1, loop_in: 0
use i: 1
loop cnt: 2, loop_in: 1
use i: 2
loop cnt: 3, loop_in: 2
use i: 3
*/
}