SlideShare a Scribd company logo
Ring Documentation, Release 1.9
Example:
See "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Give nOption
Switch nOption
On 1 See "Enter your name : " Give name See "Hello " + name + nl
On 2 See "Sample : using switch statement" + nl
On 3 Bye
Other See "bad option..." + nl
Off
22.2 Looping
• While Loop
Syntax:
while Expression
Block of statements
end
Example:
While True
See "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Give nOption
Switch nOption
On 1
See "Enter your name : "
Give name
See "Hello " + name + nl
On 2
See "Sample : using while loop" + nl
On 3
Bye
Other
See "bad option..." + nl
Off
End
• For Loop
Syntax:
22.2. Looping 219
Ring Documentation, Release 1.9
for identifier=expression to expression [step expression]
Block of statements
next
Example:
# print numbers from 1 to 10
for x = 1 to 10 see x + nl next
Example:
# Dynamic loop
See "Start : " give nStart
See "End : " give nEnd
See "Step : " give nStep
For x = nStart to nEnd Step nStep
see x + nl
Next
Example:
# print even numbers from 0 to 10
for x = 0 to 10 step 2
see x + nl
next
Example:
# print even numbers from 10 to 0
for x = 10 to 0 step -2
see x + nl
next
• For in Loop
Syntax:
for identifier in List/String [step expression]
Block of statements
next
Example:
aList = 1:10 # create list contains numbers from 1 to 10
for x in aList see x + nl next # print numbers from 1 to 10
22.3 Using The Step option with For in
We can use the Step option with For in to skip number of items in each iteration
Example:
aList = 1:10 # create list contains numbers from 1 to 10
# print odd items inside the list
for x in aList step 2
see x + nl
next
22.3. Using The Step option with For in 220
Ring Documentation, Release 1.9
22.4 Using For in to modify lists
When we use (For in) we get items by reference.
This means that we can read/edit items inside the loop.
Example:
aList = 1:5 # create list contains numbers from 1 to 5
# replace list numbers with strings
for x in aList
switch x
on 1 x = "one"
on 2 x = "two"
on 3 x = "three"
on 4 x = "four"
on 5 x = "five"
off
next
see aList # print the list items
22.5 Do Again Loop
Syntax:
do
Block of statements
again expression
Example:
x = 1
do
see x + nl
x++
again x <= 10
22.6 Exit Command
Used to go outside one or more of loops.
Syntax:
exit [expression] # inside loop
Example:
for x = 1 to 10
see x + nl
if x = 5 exit ok
next
22.4. Using For in to modify lists 221
Ring Documentation, Release 1.9
22.7 Exit from two loops
The next example presents how to use the exit command to exit from two loops in one jump.
Example:
for x = 1 to 10
for y = 1 to 10
see "x=" + x + " y=" + y + nl
if x = 3 and y = 5
exit 2 # exit from 2 loops
ok
next
next
22.8 Loop Command
Used to jump to the next iteration in the loop.
Syntax:
loop [expression] # inside loop
Example:
for x = 1 to 10
if x = 3
see "Number Three" + nl
loop
ok
see x + nl
next
22.9 Exit/Loop inside sub functions
While we are inside a loop, we can call a function then use the exit and/or loop command inside that function and the
command will work on the outer loop.
Example:
# print numbers from 1 to 10 except number 5.
for x = 1 to 10
ignore(x,5)
see x + nl
next
func ignore x,y
if x = y
loop
ok
22.7. Exit from two loops 222
Ring Documentation, Release 1.9
22.10 Short-circuit evaluation
The logical operators and/or follow the short-circuit evaluation.
If the first argument of the AND operator is zero, then there is no need to evaluate the second argument and the result
will be zero.
If the first argument of the OR operator is one, then there is no need to evaluate the second argument and the result
will be one.
Example:
/* output
** nice
** nice
** great
*/
x = 0 y = 10
if (x = 0 and nice()) and (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1
Example:
# No output
x = 0 y = 10
if (x = 1 and nice()) and (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1
Example:
/* output
** nice
** great
*/
x = 0 y = 10
if (x = 0 and nice()) or (y = 10 and nice())
see "great" + nl
ok
func nice see "nice" + nl return 1
22.11 Comments about evaluation
• True, False, nl & NULL are variables defined by the language
• True = 1
22.10. Short-circuit evaluation 223
Ring Documentation, Release 1.9
• False = 0
• nl = new line
• NULL = empty string = “”
• Everything evaluates to true except 0 (False).
Example:
# output = message from the if statement
if 5 # 5 evaluates to true because it's not zero (0).
see "message from the if statement" + nl
ok
22.11. Comments about evaluation 224
CHAPTER
TWENTYTHREE
CONTROL STRUCTURES - SECOND STYLE
In this chapter we are going to learn about the second style of control structures provided by the Ring programming
language.
23.1 Branching
• If Statement
Syntax:
if Expression
Block of statements
elseif Expression
Block of statements
else
Block of statements
end
Example:
put "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" get nOption
if nOption = 1 put "Enter your name : " get name put "Hello " + name + nl
elseif nOption = 2 put "Sample : using if statement" + nl
elseif nOption = 3 bye
else put "bad option..." + nl
end
• Switch Statement
Syntax:
switch Expression
case Expression
Block of statements
else
Block of statements
end
225
Ring Documentation, Release 1.9
Example:
Put "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Get nOption
Switch nOption
Case 1 Put "Enter your name : " Get name Put "Hello " + name + nl
Case 2 Put "Sample : using switch statement" + nl
Case 3 Bye
Else Put "bad option..." + nl
End
23.2 Looping
• While Loop
Syntax:
while Expression
Block of statements
end
Example:
While True
Put "
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
" Get nOption
Switch nOption
Case 1
Put "Enter your name : "
Get name
Put "Hello " + name + nl
Case 2
Put "Sample : using while loop" + nl
Case 3
Bye
Else
Put "bad option..." + nl
End
End
• For Loop
Syntax:
23.2. Looping 226
Ring Documentation, Release 1.9
for identifier=expression to expression [step expression]
Block of statements
end
Example:
# print numbers from 1 to 10
for x = 1 to 10 put x + nl end
Example:
# Dynamic loop
Put "Start : " get nStart
Put "End : " get nEnd
Put "Step : " get nStep
For x = nStart to nEnd Step nStep
Put x + nl
End
Example:
# print even numbers from 0 to 10
for x = 0 to 10 step 2
Put x + nl
end
Example:
# print even numbers from 10 to 0
for x = 10 to 0 step -2
put x + nl
end
• For in Loop
Syntax:
for identifier in List/String [step expression]
Block of statements
end
Example:
aList = 1:10 # create list contains numbers from 1 to 10
for x in aList put x + nl end # print numbers from 1 to 10
23.3 Exceptions
try
Block of statements
catch
Block of statements
end
23.3. Exceptions 227
CHAPTER
TWENTYFOUR
CONTROL STRUCTURES - THIRD STYLE
In this chapter we are going to learn about the third style of control structures provided by the Ring programming
language.
24.1 Branching
• If Statement
Syntax:
if Expression {
Block of statements
elseif Expression
Block of statements
else
Block of statements
}
Example:
Load "stdlib.ring"
print("
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
")
nOption = getnumber()
if nOption = 1 {
print("Enter your name : ")
name = getstring()
print("Hello #{name}n")
elseif nOption = 2
print("Sample : using if statementn")
elseif nOption = 3
bye
else
print("bad option...n")
}
228

More Related Content

What's hot (20)

PDF
The Ring programming language version 1.3 book - Part 12 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 19 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 10 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 9 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 12 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 20 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 13 of 84
Mahmoud Samir Fayed
 
PDF
Nesting of for loops using C++
prashant_sainii
 
PDF
The Ring programming language version 1.5 book - Part 4 of 31
Mahmoud Samir Fayed
 
PDF
Head First Java Chapter 1
Tom Henricksen
 
PPSX
Nested loops
Adnan Ferdous Ahmed
 
PPTX
Functions & Recursion
Nishant Munjal
 
PDF
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.3 book - Part 188 of 194
Mahmoud Samir Fayed
 
PDF
Pivorak Clojure by Dmytro Bignyak
Pivorak MeetUp
 
The Ring programming language version 1.3 book - Part 12 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 19 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 10 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 27 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 25 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 9 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 11 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 12 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 19 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 18 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 20 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 13 of 84
Mahmoud Samir Fayed
 
Nesting of for loops using C++
prashant_sainii
 
The Ring programming language version 1.5 book - Part 4 of 31
Mahmoud Samir Fayed
 
Head First Java Chapter 1
Tom Henricksen
 
Nested loops
Adnan Ferdous Ahmed
 
Functions & Recursion
Nishant Munjal
 
The Ring programming language version 1.2 book - Part 21 of 84
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 188 of 194
Mahmoud Samir Fayed
 
Pivorak Clojure by Dmytro Bignyak
Pivorak MeetUp
 

Similar to The Ring programming language version 1.9 book - Part 26 of 210 (20)

PDF
The Ring programming language version 1.5.1 book - Part 18 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 23 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 20 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.7 book - Part 22 of 196
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.4 book - Part 19 of 185
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 21 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 18 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 17 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 7 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 86 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 82 of 202
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 77 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 87 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 71 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 7 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.2 book - Part 6 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.5.1 book - Part 5 of 180
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 7 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 18 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 23 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 20 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 22 of 196
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 11 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 19 of 185
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 21 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 18 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 17 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 7 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 86 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 13 of 88
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 82 of 202
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 77 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 87 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 71 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 7 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 6 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 5 of 180
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 7 of 202
Mahmoud Samir Fayed
 
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 212 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 211 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 210 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 208 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 207 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 205 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 206 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 204 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 203 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 202 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 201 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 200 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 199 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 198 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 197 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 196 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 195 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 194 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 193 of 212
Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 192 of 212
Mahmoud Samir Fayed
 
Ad

Recently uploaded (20)

PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 

The Ring programming language version 1.9 book - Part 26 of 210

  • 1. Ring Documentation, Release 1.9 Example: See " Main Menu --------- (1) Say Hello (2) About (3) Exit " Give nOption Switch nOption On 1 See "Enter your name : " Give name See "Hello " + name + nl On 2 See "Sample : using switch statement" + nl On 3 Bye Other See "bad option..." + nl Off 22.2 Looping • While Loop Syntax: while Expression Block of statements end Example: While True See " Main Menu --------- (1) Say Hello (2) About (3) Exit " Give nOption Switch nOption On 1 See "Enter your name : " Give name See "Hello " + name + nl On 2 See "Sample : using while loop" + nl On 3 Bye Other See "bad option..." + nl Off End • For Loop Syntax: 22.2. Looping 219
  • 2. Ring Documentation, Release 1.9 for identifier=expression to expression [step expression] Block of statements next Example: # print numbers from 1 to 10 for x = 1 to 10 see x + nl next Example: # Dynamic loop See "Start : " give nStart See "End : " give nEnd See "Step : " give nStep For x = nStart to nEnd Step nStep see x + nl Next Example: # print even numbers from 0 to 10 for x = 0 to 10 step 2 see x + nl next Example: # print even numbers from 10 to 0 for x = 10 to 0 step -2 see x + nl next • For in Loop Syntax: for identifier in List/String [step expression] Block of statements next Example: aList = 1:10 # create list contains numbers from 1 to 10 for x in aList see x + nl next # print numbers from 1 to 10 22.3 Using The Step option with For in We can use the Step option with For in to skip number of items in each iteration Example: aList = 1:10 # create list contains numbers from 1 to 10 # print odd items inside the list for x in aList step 2 see x + nl next 22.3. Using The Step option with For in 220
  • 3. Ring Documentation, Release 1.9 22.4 Using For in to modify lists When we use (For in) we get items by reference. This means that we can read/edit items inside the loop. Example: aList = 1:5 # create list contains numbers from 1 to 5 # replace list numbers with strings for x in aList switch x on 1 x = "one" on 2 x = "two" on 3 x = "three" on 4 x = "four" on 5 x = "five" off next see aList # print the list items 22.5 Do Again Loop Syntax: do Block of statements again expression Example: x = 1 do see x + nl x++ again x <= 10 22.6 Exit Command Used to go outside one or more of loops. Syntax: exit [expression] # inside loop Example: for x = 1 to 10 see x + nl if x = 5 exit ok next 22.4. Using For in to modify lists 221
  • 4. Ring Documentation, Release 1.9 22.7 Exit from two loops The next example presents how to use the exit command to exit from two loops in one jump. Example: for x = 1 to 10 for y = 1 to 10 see "x=" + x + " y=" + y + nl if x = 3 and y = 5 exit 2 # exit from 2 loops ok next next 22.8 Loop Command Used to jump to the next iteration in the loop. Syntax: loop [expression] # inside loop Example: for x = 1 to 10 if x = 3 see "Number Three" + nl loop ok see x + nl next 22.9 Exit/Loop inside sub functions While we are inside a loop, we can call a function then use the exit and/or loop command inside that function and the command will work on the outer loop. Example: # print numbers from 1 to 10 except number 5. for x = 1 to 10 ignore(x,5) see x + nl next func ignore x,y if x = y loop ok 22.7. Exit from two loops 222
  • 5. Ring Documentation, Release 1.9 22.10 Short-circuit evaluation The logical operators and/or follow the short-circuit evaluation. If the first argument of the AND operator is zero, then there is no need to evaluate the second argument and the result will be zero. If the first argument of the OR operator is one, then there is no need to evaluate the second argument and the result will be one. Example: /* output ** nice ** nice ** great */ x = 0 y = 10 if (x = 0 and nice()) and (y = 10 and nice()) see "great" + nl ok func nice see "nice" + nl return 1 Example: # No output x = 0 y = 10 if (x = 1 and nice()) and (y = 10 and nice()) see "great" + nl ok func nice see "nice" + nl return 1 Example: /* output ** nice ** great */ x = 0 y = 10 if (x = 0 and nice()) or (y = 10 and nice()) see "great" + nl ok func nice see "nice" + nl return 1 22.11 Comments about evaluation • True, False, nl & NULL are variables defined by the language • True = 1 22.10. Short-circuit evaluation 223
  • 6. Ring Documentation, Release 1.9 • False = 0 • nl = new line • NULL = empty string = “” • Everything evaluates to true except 0 (False). Example: # output = message from the if statement if 5 # 5 evaluates to true because it's not zero (0). see "message from the if statement" + nl ok 22.11. Comments about evaluation 224
  • 7. CHAPTER TWENTYTHREE CONTROL STRUCTURES - SECOND STYLE In this chapter we are going to learn about the second style of control structures provided by the Ring programming language. 23.1 Branching • If Statement Syntax: if Expression Block of statements elseif Expression Block of statements else Block of statements end Example: put " Main Menu --------- (1) Say Hello (2) About (3) Exit " get nOption if nOption = 1 put "Enter your name : " get name put "Hello " + name + nl elseif nOption = 2 put "Sample : using if statement" + nl elseif nOption = 3 bye else put "bad option..." + nl end • Switch Statement Syntax: switch Expression case Expression Block of statements else Block of statements end 225
  • 8. Ring Documentation, Release 1.9 Example: Put " Main Menu --------- (1) Say Hello (2) About (3) Exit " Get nOption Switch nOption Case 1 Put "Enter your name : " Get name Put "Hello " + name + nl Case 2 Put "Sample : using switch statement" + nl Case 3 Bye Else Put "bad option..." + nl End 23.2 Looping • While Loop Syntax: while Expression Block of statements end Example: While True Put " Main Menu --------- (1) Say Hello (2) About (3) Exit " Get nOption Switch nOption Case 1 Put "Enter your name : " Get name Put "Hello " + name + nl Case 2 Put "Sample : using while loop" + nl Case 3 Bye Else Put "bad option..." + nl End End • For Loop Syntax: 23.2. Looping 226
  • 9. Ring Documentation, Release 1.9 for identifier=expression to expression [step expression] Block of statements end Example: # print numbers from 1 to 10 for x = 1 to 10 put x + nl end Example: # Dynamic loop Put "Start : " get nStart Put "End : " get nEnd Put "Step : " get nStep For x = nStart to nEnd Step nStep Put x + nl End Example: # print even numbers from 0 to 10 for x = 0 to 10 step 2 Put x + nl end Example: # print even numbers from 10 to 0 for x = 10 to 0 step -2 put x + nl end • For in Loop Syntax: for identifier in List/String [step expression] Block of statements end Example: aList = 1:10 # create list contains numbers from 1 to 10 for x in aList put x + nl end # print numbers from 1 to 10 23.3 Exceptions try Block of statements catch Block of statements end 23.3. Exceptions 227
  • 10. CHAPTER TWENTYFOUR CONTROL STRUCTURES - THIRD STYLE In this chapter we are going to learn about the third style of control structures provided by the Ring programming language. 24.1 Branching • If Statement Syntax: if Expression { Block of statements elseif Expression Block of statements else Block of statements } Example: Load "stdlib.ring" print(" Main Menu --------- (1) Say Hello (2) About (3) Exit ") nOption = getnumber() if nOption = 1 { print("Enter your name : ") name = getstring() print("Hello #{name}n") elseif nOption = 2 print("Sample : using if statementn") elseif nOption = 3 bye else print("bad option...n") } 228