forked from DlangRen/Programming-in-D
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhile.d
230 lines (166 loc) · 5.92 KB
/
while.d
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
Ddoc
$(DERS_BOLUMU $(IX while) $(IX loop, while) $(CH4 while) Loop)
$(P
The $(C while) loop is similar to the $(C if) statement and essentially works as a repeated $(C if) statement. Just like $(C if), $(C while) also takes a logical expression and evaluates the block when the logical expression is $(C true). The difference is that the $(C while) statement evaluates the logical expression and executes the expressions in the block repeatedly, as long as the logical expression is $(C true), not just once. Repeating a block of code this way is called $(I looping).
)
$(P
Here is the syntax of the $(C while) statement:
)
---
while (a_logical_expression) {
// ... expression(s) to execute while true
}
---
$(P
For example, the code that represents $(I eat cookies as long as there is cookie) can be coded like this:
)
---
import std.stdio;
void main() {
bool existsCookie = true;
while (existsCookie) {
writeln("Take cookie");
writeln("Eat cookie");
}
}
---
$(P
That program would continue repeating the loop because the value of $(C existsCookie) never changes from $(C true).
)
$(P
$(C while) is useful when the value of the logical expression changes during the execution of the program. To see this, let's write a program that takes a number from the user as long as that number is zero or greater. Remember that the initial value of $(C int) variables is 0:
)
---
import std.stdio;
void main() {
int number;
while (number >= 0) {
write("Please enter a number: ");
readf(" %s", &number);
writeln("Thank you for ", number);
}
writeln("Exited the loop");
}
---
$(P
The program thanks for the provided number and exits the loop only when the number is less than zero.
)
$(H5 $(IX continue) The $(C continue) statement)
$(P
The continue statement starts the next iteration of the loop right away, instead of executing the rest of the expressions of the block.
)
$(P
Let's modify the program above to be a little picky: instead of thanking for any number, let's not accept 13. The following program does not thank for 13 because in that case the $(C continue) statement makes the program go to the beginning of the loop to evaluate the logical expression again:
)
---
import std.stdio;
void main() {
int number;
while (number >= 0) {
write("Please enter a number: ");
readf(" %s", &number);
if (number == 13) {
writeln("Sorry, not accepting that one...");
$(HILITE continue);
}
writeln("Thank you for ", number);
}
writeln("Exited the loop");
}
---
$(P
We can define the behavior of that program as $(I take numbers as long as they are greater than or equal to 0 but skip 13).
)
$(P
$(C continue) works with $(C do-while), $(C for), and $(C foreach) statements as well. We will see these features in later chapters.
)
$(H5 $(IX break) The $(C break) statement)
$(P
Sometimes it becomes obvious that there is no need to stay in the $(C while) loop any longer. $(C break) allows the program to exit the loop right away. The following program exits the loop as soon as it finds a special number:
)
---
import std.stdio;
void main() {
int number;
while (number >= 0) {
write("Please enter a number: ");
readf(" %s", &number);
if (number == 42) {
writeln("FOUND IT!");
$(HILITE break);
}
writeln("Thank you for ", number);
}
writeln("Exited the loop");
}
---
$(P
We can summarize this behavior as $(I take numbers as long as they are greater than or equal to 0 or until a number is 42).
)
$(P
$(C break) works with $(C do-while), $(C for), $(C foreach), and $(C switch) statements as well. We will see these features in later chapters.
)
$(H5 $(IX loop, infinite) Infinite loop)
$(P
Sometimes the logical expression is intentionally made a constant $(C true). The $(C break) statement is a common way of exiting such $(I infinite loops).
)
$(P
The following program prints a menu in an infinite loop; the only way of exiting the loop is a $(C break) statement:
)
---
import std.stdio;
void main() {
/* Infinite loop, because the logical expression is always
* true */
while ($(HILITE true)) {
write("0:Exit, 1:Turkish, 2:English - Your choice? ");
int choice;
readf(" %s", &choice);
if (choice == 0) {
writeln("See you later...");
$(HILITE break); // The only exit of this loop
} else if (choice == 1) {
writeln("Merhaba!");
} else if (choice == 2) {
writeln("Hello!");
} else {
writeln("Sorry, I don't know that language. :/");
}
}
}
---
$(P
$(I $(B Note:)) Exceptions $(I can terminate an infinite loop as well. We will see exceptions in a later chapter.)
)
$(PROBLEM_COK
$(PROBLEM
The following program is designed to stay in the loop as long as the input is 3, but there is a bug: it doesn't ask for any input:
---
import std.stdio;
void main() {
int number;
while (number == 3) {
write("Number? ");
readf(" %s", &number);
}
}
---
$(P
Fix the bug. The program should stay in the loop as long as the input is 3.
)
)
$(PROBLEM
Make the computer help Anna and Bill play a game. First, the computer should take a number from Anna in the range from 1 to 10. The program should not accept any other number; it should ask again.
$(P
Once the program takes a valid number from Anna, it should start taking numbers from Bill until he guesses Anna's number correctly.
)
$(P
$(I $(B Note:) The numbers that Anna enters obviously stays on the terminal and can be seen by Bill. Let's ignore this fact and write the program as an exercise of the $(C while) statement.)
)
)
)
Macros:
SUBTITLE=while Loop
DESCRIPTION=The while loop and the related statements break and continue
KEYWORDS=d programming language tutorial book while loop statement break continue
MINI_SOZLUK=