2 single: Control Structures - First Style; Introduction
4 ================================
5 Control Structures - First Style
6 ================================
8 In this chapter we are going to learn about the control structures provided by
9 the Ring programming language.
12 pair: Control Structures; Branching
45 if nOption = 1 see "Enter your name : " give name see "Hello " + name + nl
46 but nOption = 2 see "Sample : using if statement" + nl
48 else see "bad option..." + nl
53 pair: Control Structures - First Style; Switch Statement
82 On 1 See "Enter your name : " Give name See "Hello " + name + nl
83 On 2 See "Sample : using switch statement" + nl
85 Other See "bad option..." + nl
90 pair: Control Structures - First Style; Looping
96 pair: Control Structures - First Style; While Loop
125 See "Enter your name : "
127 See "Hello " + name + nl
129 See "Sample : using while loop" + nl
133 See "bad option..." + nl
138 pair: Control Structures - First Style; For Loop
146 for identifier=expression to expression [step expression]
154 # print numbers from 1 to 10
155 for x = 1 to 10 see x + nl next
162 See "Start : " give nStart
163 See "End : " give nEnd
164 See "Step : " give nStep
165 For x = nStart to nEnd Step nStep
173 # print even numbers from 0 to 10
174 for x = 0 to 10 step 2
182 # print even numbers from 10 to 0
183 for x = 10 to 0 step -2
189 pair: Control Structures - First Style; For In Loop
197 for identifier in List/String [step expression]
205 aList = 1:10 # create list contains numbers from 1 to 10
206 for x in aList see x + nl next # print numbers from 1 to 10
209 pair: Control Structures - First Style; Step Option
211 Using The Step option with For in
212 =================================
214 We can use the Step option with For in to skip number of items in each iteration
220 aList = 1:10 # create list contains numbers from 1 to 10
221 # print odd items inside the list
222 for x in aList step 2
228 pair: Control Structures - First Style; for in to modify lists
230 Using For in to modify lists
231 =============================
233 When we use (For in) we get items by reference.
235 This means that we can read/edit items inside the loop.
241 aList = 1:5 # create list contains numbers from 1 to 5
242 # replace list numbers with strings
252 see aList # print the list items
255 pair: Control Structures - First Style; Do Again Loop
279 pair: Control Structures - First Style; Exit
284 Used to go outside one or more of loops.
291 exit [expression] # inside loop
304 pair: Control Structures - First Style; Exit from two loops
309 The next example presents how to use the exit command to exit from two loops in one jump.
317 see "x=" + x + " y=" + y + nl
319 exit 2 # exit from 2 loops
325 pair: Control Structures - First Style; Loop Command
330 Used to jump to the next iteration in the loop.
336 loop [expression] # inside loop
344 see "Number Three" + nl
351 pair: Control Structures - First Style; Short-circuit evaluation
353 Short-circuit evaluation
354 ========================
356 The logical operators and/or follow the `short-circuit evaluation <http://en.wikipedia.org/wiki/Short-circuit_evaluation>`_.
358 If the first argument of the AND operator is zero, then there is no need to evaluate the second
359 argument and the result will be zero.
361 If the first argument of the OR operator is one, then there is no need to evaluate the second
362 argument and the result will be one.
376 if (x = 0 and nice()) and (y = 10 and nice())
380 func nice see "nice" + nl return 1
391 if (x = 1 and nice()) and (y = 10 and nice())
395 func nice see "nice" + nl return 1
409 if (x = 0 and nice()) or (y = 10 and nice())
413 func nice see "nice" + nl return 1
417 pair: Control Structures - First Style; Comments about evaluation
419 Comments about evaluation
420 =========================
422 * True, False, nl & NULL are variables defined by the language
430 * NULL = empty string = ""
432 * Everything evaluates to true except 0 (False).
438 # output = message from the if statement
440 if 5 # 5 evaluates to true because it's not zero (0).
441 see "message from the if statement" + nl