SlideShare a Scribd company logo
Ring Documentation, Release 1.5.2
18.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
18.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
18.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
18.7. Exit from two loops 155
Ring Documentation, Release 1.5.2
18.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
18.11 Comments about evaluation
• True, False, nl & NULL are variables defined by the language
• True = 1
18.10. Short-circuit evaluation 156
Ring Documentation, Release 1.5.2
• 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
18.11. Comments about evaluation 157
CHAPTER
NINETEEN
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.
19.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
158
Ring Documentation, Release 1.5.2
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
19.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:
19.2. Looping 159
Ring Documentation, Release 1.5.2
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
19.3 Exceptions
try
Block of statements
catch
Block of statements
end
19.3. Exceptions 160
CHAPTER
TWENTY
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.
20.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")
}
161
Ring Documentation, Release 1.5.2
• Switch Statement
Syntax:
switch Expression {
case Expression
Block of statements
else
Block of statements
}
Example:
Load "stdlib.ring"
print("
Main Menu
---------
(1) Say Hello
(2) About
(3) Exit
")
nOption = GetString()
switch nOption {
case 1
print("Enter your name : ")
name = getstring()
print("Hello #{name}n")
case 2
print("Sample : using switch statementn")
case 3
Bye
else
print("bad option...n")
}
20.2 Looping
• While Loop
Syntax:
while Expression {
Block of statements
}
Example:
Load "stdlib.ring"
While True {
print("
Main Menu
---------
20.2. Looping 162
Ring Documentation, Release 1.5.2
(1) Say Hello
(2) About
(3) Exit
")
nOption = GetString()
switch nOption {
case 1
print("Enter your name : ")
name = getstring()
print("Hello #{name}n")
case 2
print("Sample : using switch statementn")
case 3
Bye
else
print("bad option...n")
}
}
• For Loop
Syntax:
for identifier=expression to expression [step expression] {
Block of statements
}
Example:
# print numbers from 1 to 10
load "stdlib.ring"
for x = 1 to 10 {
print("#{x}n")
}
Example:
load "stdlib.ring"
# Dynamic loop
print("Start : ") nStart = getnumber()
print("End : ") nEnd = getnumber()
print("Step : ") nStep = getnumber()
for x = nStart to nEnd step nStep {
print("#{x}n")
}
Example:
load "stdlib.ring"
# print even numbers from 0 to 10
for x = 0 to 10 step 2 {
print("#{x}n")
}
20.2. Looping 163
Ring Documentation, Release 1.5.2
Example:
load "stdlib.ring"
# print even numbers from 10 to 0
for x = 10 to 0 step -2 {
print("#{x}n")
}
• For in Loop
Syntax:
for identifier in List/String [step expression] {
Block of statements
}
Example:
load "stdlib.ring"
aList = 1:10 # create list contains numbers from 1 to 10
for x in aList { print("#{x}n") } # print numbers from 1 to 10
Example:
load "stdlib.ring"
aList = 1:10 # create list contains numbers from 1 to 10
# print odd items inside the list
for x in aList step 2 {
print("#{x}n")
}
When we use (For in) we get items by reference.
This means that we can read/edit items inside the loop.
Example:
load "stdlib.ring"
aList = 1:5 # create list contains numbers from 1 to 5
# replace list numbers with strings
for x in aList {
switch x {
case 1 x = "one"
case 2 x = "two"
case 3 x = "three"
case 4 x = "four"
case 5 x = "five"
}
}
print(aList) # print the list items
20.3 Exceptions
20.3. Exceptions 164

More Related Content

PDF
The Ring programming language version 1.7 book - Part 23 of 196
PDF
The Ring programming language version 1.3 book - Part 12 of 88
PDF
The Ring programming language version 1.5.4 book - Part 20 of 185
PDF
The Ring programming language version 1.6 book - Part 22 of 189
PDF
The Ring programming language version 1.4.1 book - Part 5 of 31
PDF
The Ring programming language version 1.5.3 book - Part 20 of 184
PDF
The Ring programming language version 1.10 book - Part 28 of 212
PDF
The Ring programming language version 1.5.1 book - Part 18 of 180
The Ring programming language version 1.7 book - Part 23 of 196
The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.5.4 book - Part 20 of 185
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.4.1 book - Part 5 of 31
The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.10 book - Part 28 of 212
The Ring programming language version 1.5.1 book - Part 18 of 180

What's hot (20)

PDF
The Ring programming language version 1.3 book - Part 18 of 88
PDF
The Ring programming language version 1.5.4 book - Part 27 of 185
PDF
The Ring programming language version 1.9 book - Part 34 of 210
PDF
The Ring programming language version 1.5.2 book - Part 26 of 181
PDF
The Ring programming language version 1.2 book - Part 13 of 84
DOCX
Mcq cpup
PDF
The Ring programming language version 1.10 book - Part 101 of 212
DOCX
Clase de matlab
PDF
The Ring programming language version 1.4 book - Part 7 of 30
PDF
The Ring programming language version 1.6 book - Part 29 of 189
PDF
The Ring programming language version 1.6 book - Part 34 of 189
PPTX
Dts x dicoding #2 memulai pemrograman kotlin
PDF
The Ring programming language version 1.2 book - Part 16 of 84
PPT
12 lec 12 loop
PDF
The Ring programming language version 1.5.4 book - Part 25 of 185
PDF
Are we ready to Go?
PDF
The Ring programming language version 1.6 book - Part 25 of 189
PPTX
Linked list without animation
PDF
The Ring programming language version 1.9 book - Part 31 of 210
PDF
The Ring programming language version 1.7 book - Part 30 of 196
The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.5.4 book - Part 27 of 185
The Ring programming language version 1.9 book - Part 34 of 210
The Ring programming language version 1.5.2 book - Part 26 of 181
The Ring programming language version 1.2 book - Part 13 of 84
Mcq cpup
The Ring programming language version 1.10 book - Part 101 of 212
Clase de matlab
The Ring programming language version 1.4 book - Part 7 of 30
The Ring programming language version 1.6 book - Part 29 of 189
The Ring programming language version 1.6 book - Part 34 of 189
Dts x dicoding #2 memulai pemrograman kotlin
The Ring programming language version 1.2 book - Part 16 of 84
12 lec 12 loop
The Ring programming language version 1.5.4 book - Part 25 of 185
Are we ready to Go?
The Ring programming language version 1.6 book - Part 25 of 189
Linked list without animation
The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.7 book - Part 30 of 196
Ad

Similar to The Ring programming language version 1.5.2 book - Part 19 of 181 (20)

PDF
The Ring programming language version 1.4 book - Part 5 of 30
PDF
The Ring programming language version 1.2 book - Part 10 of 84
PDF
The Ring programming language version 1.8 book - Part 24 of 202
PDF
The Ring programming language version 1.9 book - Part 26 of 210
PDF
The Ring programming language version 1.5.3 book - Part 19 of 184
PDF
The Ring programming language version 1.10 book - Part 27 of 212
PDF
The Ring programming language version 1.6 book - Part 21 of 189
PDF
The Ring programming language version 1.3 book - Part 11 of 88
PDF
The Ring programming language version 1.7 book - Part 22 of 196
PDF
The Ring programming language version 1.5.4 book - Part 19 of 185
PDF
The Ring programming language version 1.9 book - Part 27 of 210
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
The Ring programming language version 1.5.1 book - Part 71 of 180
PDF
The Ring programming language version 1.5.2 book - Part 18 of 181
PDF
The Ring programming language version 1.5.2 book - Part 72 of 181
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
PDF
The Ring programming language version 1.5.1 book - Part 17 of 180
PDF
The Ring programming language version 1.9 book - Part 86 of 210
PDF
The Ring programming language version 1.10 book - Part 31 of 212
PDF
The Ring programming language version 1.4 book - Part 29 of 30
The Ring programming language version 1.4 book - Part 5 of 30
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.8 book - Part 24 of 202
The Ring programming language version 1.9 book - Part 26 of 210
The Ring programming language version 1.5.3 book - Part 19 of 184
The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.7 book - Part 22 of 196
The Ring programming language version 1.5.4 book - Part 19 of 185
The Ring programming language version 1.9 book - Part 27 of 210
The Ring programming language version 1.8 book - Part 25 of 202
The Ring programming language version 1.5.1 book - Part 71 of 180
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.9 book - Part 86 of 210
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.4 book - Part 29 of 30
Ad

More from Mahmoud Samir Fayed (20)

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

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Advanced IT Governance
PDF
KodekX | Application Modernization Development
PDF
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Monthly Chronicles - July 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Advanced IT Governance
KodekX | Application Modernization Development
Unlocking AI with Model Context Protocol (MCP)

The Ring programming language version 1.5.2 book - Part 19 of 181

  • 1. Ring Documentation, Release 1.5.2 18.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 18.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 18.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 18.7. Exit from two loops 155
  • 2. Ring Documentation, Release 1.5.2 18.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 18.11 Comments about evaluation • True, False, nl & NULL are variables defined by the language • True = 1 18.10. Short-circuit evaluation 156
  • 3. Ring Documentation, Release 1.5.2 • 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 18.11. Comments about evaluation 157
  • 4. CHAPTER NINETEEN 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. 19.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 158
  • 5. Ring Documentation, Release 1.5.2 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 19.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: 19.2. Looping 159
  • 6. Ring Documentation, Release 1.5.2 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 19.3 Exceptions try Block of statements catch Block of statements end 19.3. Exceptions 160
  • 7. CHAPTER TWENTY 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. 20.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") } 161
  • 8. Ring Documentation, Release 1.5.2 • Switch Statement Syntax: switch Expression { case Expression Block of statements else Block of statements } Example: Load "stdlib.ring" print(" Main Menu --------- (1) Say Hello (2) About (3) Exit ") nOption = GetString() switch nOption { case 1 print("Enter your name : ") name = getstring() print("Hello #{name}n") case 2 print("Sample : using switch statementn") case 3 Bye else print("bad option...n") } 20.2 Looping • While Loop Syntax: while Expression { Block of statements } Example: Load "stdlib.ring" While True { print(" Main Menu --------- 20.2. Looping 162
  • 9. Ring Documentation, Release 1.5.2 (1) Say Hello (2) About (3) Exit ") nOption = GetString() switch nOption { case 1 print("Enter your name : ") name = getstring() print("Hello #{name}n") case 2 print("Sample : using switch statementn") case 3 Bye else print("bad option...n") } } • For Loop Syntax: for identifier=expression to expression [step expression] { Block of statements } Example: # print numbers from 1 to 10 load "stdlib.ring" for x = 1 to 10 { print("#{x}n") } Example: load "stdlib.ring" # Dynamic loop print("Start : ") nStart = getnumber() print("End : ") nEnd = getnumber() print("Step : ") nStep = getnumber() for x = nStart to nEnd step nStep { print("#{x}n") } Example: load "stdlib.ring" # print even numbers from 0 to 10 for x = 0 to 10 step 2 { print("#{x}n") } 20.2. Looping 163
  • 10. Ring Documentation, Release 1.5.2 Example: load "stdlib.ring" # print even numbers from 10 to 0 for x = 10 to 0 step -2 { print("#{x}n") } • For in Loop Syntax: for identifier in List/String [step expression] { Block of statements } Example: load "stdlib.ring" aList = 1:10 # create list contains numbers from 1 to 10 for x in aList { print("#{x}n") } # print numbers from 1 to 10 Example: load "stdlib.ring" aList = 1:10 # create list contains numbers from 1 to 10 # print odd items inside the list for x in aList step 2 { print("#{x}n") } When we use (For in) we get items by reference. This means that we can read/edit items inside the loop. Example: load "stdlib.ring" aList = 1:5 # create list contains numbers from 1 to 5 # replace list numbers with strings for x in aList { switch x { case 1 x = "one" case 2 x = "two" case 3 x = "three" case 4 x = "four" case 5 x = "five" } } print(aList) # print the list items 20.3 Exceptions 20.3. Exceptions 164