This file is indexed.

/usr/share/doc/sludge/SLUDGEDevKitHelp/Loops_and_Conditionals.html is in sludge-doc 2.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<TITLE>Loops and Conditionals</TITLE>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<BODY>
<HR>
<div align="center"><img id="headerGraphic" src="images/sludge300.png" alt="SLUDGE"/></div>
<h2>Loops and Conditionals</h2>
<HR>


<P>
The styles of looping structure which SLUDGE features are described in this section. Firstly:
</P>

<H3>The &quot;for&quot; loop:</H3>

<P>
<pre>for (<i>initialisation</i>; <i>check</i>; <i>next</i>) {
   # Code here
}</pre>
</P>

<P>
Here, <i>initialisation</i>, <i>check</i> and <i>next</i> are all segments of code. The <i>initialisation</i> segment tells the program what to do at the start of the loop; the <i>check</i> segment tells the program how to test whether the loop needs to continue and will be treated as a boolean (see the <a href="Treating_Variables_as_Booleans.html">Treating Variables as Booleans</a> section), and the <i>next</i> segment tells the program what to do at the end of every loop (for example to get onto the next record or to make sure the loop won't continue forever).
</P>

<P>
The most common example of a for loop is the following:
</P>

<P>
<pre>var a;

for (a = 0; a &lt; 10; a ++) {
   # Code here
}</pre>
</P>

<P>
Or a shorter version:
</P>

<P>
<pre>for (var a = 0; a &lt; 10; a ++) {
   # Code here
}</pre>
</P>

<P>
This will set the value of a to 0, check whether a is less than 10 (it is), and then perform any code inside the body of the loop. This code can use the variable a, or even change it. After performing the code in the body of the loop, the program will execute the a++ segment (or &quot;increase the value of a by 1&quot;... a ++ is just a shorter way of saying a = a + 1). Now, a will have the value 1. The program will check whether it's less than 10 (it is), and so on. When the value of a reaches 10, the program will continue from the line of code after the closing bracket &quot;}&quot;.
</P>

<P>
For loops are common in programming languages in exactly this form, and present in most programming languages in some form or another. 
</P>

<H3>The &quot;loop&quot; loop:</H3>

<P>
In C and C++ (and other languages) it is common to see the following code:
</P>

<P>
<pre>for (;;) {
   # Code here
}</pre>
</P>

<P>
The above means &quot;execute the code again and again forever&quot;. However, SLUDGE is a little stricter with its for loops and the above will not work! Instead, in order to remove the need for abusing for loops in this way (although I'm sure they don't mind) SLUDGE features an equivalent structure:
</P>

<P>
<pre>loop {
   # Code here
}</pre>
</P>

<P>
Which also means &quot;execute the code again and again forever&quot;. You can escape from one of these loops - or from any function - by returning from the function within the code using the return. (Currently, SLUDGE contains no break statement.)
</P>

<H3>The &quot;while&quot; loop:</H3>

<P>
<pre>while (<i>check</i>) {
   # Code here
}</pre>
</P>

<P>
The code will be executed again and again until the value returned by <i>check</i> is TRUE (or equivalent - see the <a href="Treating_Variables_as_Booleans.html">Treating Variables as Booleans</a> section). Therefore <i>check</i> can be any segment of code which returns a value, whether a variable, a function call, a complex algorithm or whatever your program needs. If the value of <i>check</i> is FALSE when the program reaches the start of the loop, the contents of the loop are not executed at all.
</P>

<H3>Using &quot;if&quot; and &quot;else&quot;:</H3>

<P>
Sometimes you only want to perform a certain line (or section) of code in a particular situation. For example, if you're writing a function to handle keypresses, you will probably want to perform different actions for different keys...
</P>

<P>
<pre>sub handleKeyPress (key) {
   if (key == &quot;ESCAPE&quot;) quitGame ();
}</pre>
</P>

<P>
If you need to perform more than one line, group the lines together using curly braces (&quot;{&quot; and &quot;}&quot;).
</P>

<P>
<pre>var neverSpokenToFarmer = TRUE;

sub talkToFarmer () {
   say (ego, &quot;Hi...&quot;);
   if (neverSpokenToFarmer) {
      say (farmer, &quot;Hello? Who are you?&quot;);
      say (ego, &quot;I'm Egor the ego. I'm on a quest.&quot;);
      say (farmer, &quot;What quest would that be, then?&quot;);
      say (ego, &quot;I'm trying to learn SLUDGE.&quot;);
      say (farmer, &quot;Ar. Good luck to ya.&quot;);
      say (ego, &quot;Can I ask you something?&quot;);
      neverSpokenToFarmer = FALSE;
   }
   say (farmer, &quot;I'm a bit busy. Sorry.&quot;);
}</pre>
</P>

<P>
In the above example, the value of neverSpokenToFarmer will be initially set to TRUE. So, the first time the function is called, the ego character will say &quot;Hi...&quot;, the farmer character will say &quot;Hello? Who are you?&quot; and so on. The value of neverSpokenToFarmer will then be set to FALSE, and the farmer character will say the line &quot;I'm a bit busy. Sorry.&quot;
</P>

<P>
If the function is called again, the condition in the if statement will not be true, and so the conversation will jump straight from the ego character saying &quot;Hi...&quot; to the farmer character saying &quot;I'm a bit busy. Sorry.&quot;
</P>

<P>
Sometimes you may want to provide an alternative. For example, take the following example...
</P>

<P>
<pre>sub talkToChicken () {
   say (chicken, &quot;Have you spoken to the farmer yet?&quot;);

   if (neverSpokenToFarmer) {
      say (ego, &quot;No.&quot;);
      say (chicken, &quot;Lucky you.&quot;);
   }

   if (! neverSpokenToFarmer) {
      say (ego, &quot;Yes.&quot;);
      say (chicken, &quot;Did he ask about me? Huh? Huh?&quot;);
      say (ego, &quot;Nope, he didn't mention you at all.&quot;);
   }
}</pre>
</P>

<P>
This could be rewritten using the word else. The only place in which the word else can be used is straight after the group of commands to be executed by an if statement.
</P>

<P>
<pre>sub talkToChicken () {
   say (chicken, &quot;Have you spoken to the farmer yet?&quot;);

   if (neverSpokenToFarmer) {
      say (ego, &quot;No.&quot;);
      say (chicken, &quot;Lucky you.&quot;);
   } else {
      say (ego, &quot;Yes.&quot;);
      say (chicken, &quot;Did he ask about me? Huh? Huh?&quot;);
      say (ego, &quot;Nope, he didn't mention you at all.&quot;);
   }
}</pre>
</P>

<P>
Using else followed immediately by another if statement, it is possible to write complex structures like this...
</P>

<P>
<pre>sub countEggs () {
   if (neverSpokenToFarmer) {
      say (ego, &quot;I haven't introduced myself to the farmer.&quot;);
      say (ego, &quot;If someone he's not met messes with his eggs...&quot;);
      say (ego, &quot;Well, let's just say I'm not going to risk it.&quot;);
   } else if (numberOfEggs == 0) {
      say (ego, &quot;No eggs? You're a pretty lousy chicken!&quot;);
      say (chicken, &quot;Hey, I've been doing paperwork all morning.&quot;);
   } else if (numberOfEggs == 1) {
      say (ego, &quot;I can only see one egg.&quot;);
      say (chicken, &quot;Bite me.&quot;);
      say (ego, &quot;But I forgot to bring my oven...&quot;);
   } else {
      say (ego, &quot;There are &quot; + numberOfEggs + &quot; eggs here.&quot;);
      say (chicken, &quot;What can I say? I've been broody lately.&quot;);
   }
}</pre>
</P>

<P>
Note that there has always been some ambiguity with the following type of structure...
</P>

<P>
<pre>if (condition1)
if (condition2)
# Code here
else
# More code here</pre>
</P>

<P>
Does that mean this?
</P>

<P>
<pre>if (condition1) {
   if (condition2) {
      # Code here
   } else {
      # More code here
   }
}</pre>
</P>

<P>
Or this?
</P>

<P>
<pre>if (condition1) {
   if (condition2) {
      # Code here
   }
} else {
   # More code here
}</pre>
</P>

<P>
Well, in SLUDGE it means the second. Be warned. It's always best to use parentheses when using nested if structures to avoid potential problems and unexpected behaviour. Remember, your computer doesn't know what you mean unless you actually tell it... so be specific.
</P>

<P class="copyright-notice">SLUDGE and this SLUDGE documentation are <A HREF="Copyright.html">copyright</A> Hungry Software and contributors 2000-2012
</P>

<HR>
</BODY>
</html>