SlideShare a Scribd company logo
Ring Documentation, Release 1.2
15.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
15.5 Do Again Loop
Syntax:
do
Block of statements
again expression
Example:
x = 1
do
see x + nl
x++
again x <= 10
15.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
15.4. Using For in to modify lists 70
Ring Documentation, Release 1.2
15.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
• 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
15.8 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
15.9 Short-circuit evaluation
The logical operators and/or follow the short-circuit evaluation.
15.7. Exit from two loops 71
Ring Documentation, Release 1.2
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
15.10 Comments about evaluation
• True, False, nl & NULL are variables defined by the language
• True = 1
• False = 0
• nl = new line
• NULL = empty string = “”
15.10. Comments about evaluation 72
Ring Documentation, Release 1.2
• 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
15.10. Comments about evaluation 73
CHAPTER
SIXTEEN
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.
16.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
74
Ring Documentation, Release 1.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
16.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:
16.2. Looping 75
Ring Documentation, Release 1.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
16.3 Exceptions
try
Block of statements
catch
Block of statements
end
16.3. Exceptions 76
CHAPTER
SEVENTEEN
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.
17.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")
}
77
Ring Documentation, Release 1.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")
}
17.2 Looping
• While Loop
Syntax:
while Expression {
Block of statements
}
Example:
Load "stdlib.ring"
While True {
print("
Main Menu
---------
17.2. Looping 78
Ring Documentation, Release 1.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")
}
17.2. Looping 79

More Related Content

PDF
The Ring programming language version 1.2 book - Part 9 of 84
PDF
The Ring programming language version 1.2 book - Part 11 of 84
PDF
The Ring programming language version 1.2 book - Part 12 of 84
PDF
The Ring programming language version 1.3 book - Part 13 of 88
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
The Ring programming language version 1.3 book - Part 11 of 88
PDF
The Ring programming language version 1.5.2 book - Part 18 of 181
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 11 of 84
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.8 book - Part 25 of 202
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.1 book - Part 19 of 180

What's hot (20)

PDF
The Ring programming language version 1.9 book - Part 27 of 210
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
PPTX
Dictionary
PDF
The Ring programming language version 1.8 book - Part 24 of 202
PPTX
Iteration
PPTX
String Manipulation in Python
PDF
The Ring programming language version 1.4 book - Part 5 of 30
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 19 of 184
PDF
The Ring programming language version 1.6 book - Part 22 of 189
PPTX
Functions & Recursion
PDF
The Ring programming language version 1.5.2 book - Part 31 of 181
PDF
The Ring programming language version 1.5.1 book - Part 17 of 180
PDF
The Ring programming language version 1.10 book - Part 27 of 212
PDF
The Ring programming language version 1.10 book - Part 31 of 212
PDF
The Ring programming language version 1.2 book - Part 21 of 84
PDF
Swift the implicit parts
PDF
The Ring programming language version 1.9 book - Part 26 of 210
The Ring programming language version 1.9 book - Part 27 of 210
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
Dictionary
The Ring programming language version 1.8 book - Part 24 of 202
Iteration
String Manipulation in Python
The Ring programming language version 1.4 book - Part 5 of 30
The Ring programming language version 1.4.1 book - Part 5 of 31
The Ring programming language version 1.5.3 book - Part 19 of 184
The Ring programming language version 1.6 book - Part 22 of 189
Functions & Recursion
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.10 book - Part 27 of 212
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.2 book - Part 21 of 84
Swift the implicit parts
The Ring programming language version 1.9 book - Part 26 of 210
Ad

Viewers also liked (20)

PDF
Guillermo Dumrauf - Calculo Financiero Aplicado (Parte 2 de 3)
PDF
Nk 03 2017_lr_englisch
PDF
The Ring programming language version 1.2 book - Part 8 of 84
PPTX
V. cholerae
PDF
人気プログラム 一覧
PPTX
Gpl1 8-artikel-tutorial 1
PDF
High Temperature Conveyor Belts
PDF
The Ring programming language version 1.2 book - Part 7 of 84
PDF
Ambientação ADM - dicas de navegação - 1º semestre 2017
PDF
The Ring programming language version 1.2 book - Part 14 of 84
PDF
The Ring programming language version 1.2 book - Part 27 of 84
PDF
The Ring programming language version 1.2 book - Part 36 of 84
PDF
The Ring programming language version 1.2 book - Part 13 of 84
PDF
The Ring programming language version 1.2 book - Part 38 of 84
PDF
The Ring programming language version 1.2 book - Part 29 of 84
PDF
The Ring programming language version 1.2 book - Part 28 of 84
PDF
The Ring programming language version 1.2 book - Part 32 of 84
PDF
The Ring programming language version 1.2 book - Part 30 of 84
PDF
The Ring programming language version 1.2 book - Part 35 of 84
PDF
The Ring programming language version 1.2 book - Part 39 of 84
Guillermo Dumrauf - Calculo Financiero Aplicado (Parte 2 de 3)
Nk 03 2017_lr_englisch
The Ring programming language version 1.2 book - Part 8 of 84
V. cholerae
人気プログラム 一覧
Gpl1 8-artikel-tutorial 1
High Temperature Conveyor Belts
The Ring programming language version 1.2 book - Part 7 of 84
Ambientação ADM - dicas de navegação - 1º semestre 2017
The Ring programming language version 1.2 book - Part 14 of 84
The Ring programming language version 1.2 book - Part 27 of 84
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 13 of 84
The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 29 of 84
The Ring programming language version 1.2 book - Part 28 of 84
The Ring programming language version 1.2 book - Part 32 of 84
The Ring programming language version 1.2 book - Part 30 of 84
The Ring programming language version 1.2 book - Part 35 of 84
The Ring programming language version 1.2 book - Part 39 of 84
Ad

Similar to The Ring programming language version 1.2 book - Part 10 of 84 (20)

PDF
The Ring programming language version 1.5.1 book - Part 18 of 180
PDF
The Ring programming language version 1.5.2 book - Part 19 of 181
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.7 book - Part 23 of 196
PDF
The Ring programming language version 1.5.3 book - Part 20 of 184
PDF
The Ring programming language version 1.6 book - Part 21 of 189
PDF
The Ring programming language version 1.10 book - Part 28 of 212
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 72 of 181
PDF
The Ring programming language version 1.9 book - Part 86 of 210
PDF
The Ring programming language version 1.5.3 book - Part 84 of 184
PPTX
Chapter 2-Python and control flow statement.pptx
PDF
The Ring programming language version 1.8 book - Part 82 of 202
PDF
The Ring programming language version 1.3 book - Part 57 of 88
PDF
The Ring programming language version 1.3 book - Part 18 of 88
PPTX
“Python” or “CPython” is written in C/C+
PDF
The Ring programming language version 1.7 book - Part 79 of 196
PPTX
Python programing
PDF
The Ring programming language version 1.2 book - Part 54 of 84
The Ring programming language version 1.5.1 book - Part 18 of 180
The Ring programming language version 1.5.2 book - Part 19 of 181
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.7 book - Part 23 of 196
The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.10 book - Part 28 of 212
The Ring programming language version 1.5.1 book - Part 71 of 180
The Ring programming language version 1.5.2 book - Part 72 of 181
The Ring programming language version 1.9 book - Part 86 of 210
The Ring programming language version 1.5.3 book - Part 84 of 184
Chapter 2-Python and control flow statement.pptx
The Ring programming language version 1.8 book - Part 82 of 202
The Ring programming language version 1.3 book - Part 57 of 88
The Ring programming language version 1.3 book - Part 18 of 88
“Python” or “CPython” is written in C/C+
The Ring programming language version 1.7 book - Part 79 of 196
Python programing
The Ring programming language version 1.2 book - Part 54 of 84

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
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
medical staffing services at VALiNTRY
PDF
Nekopoi APK 2025 free lastest update
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administraation Chapter 3
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
System and Network Administration Chapter 2
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms II-SECS-1021-03
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Which alternative to Crystal Reports is best for small or large businesses.pdf
Odoo POS Development Services by CandidRoot Solutions
wealthsignaloriginal-com-DS-text-... (1).pdf
medical staffing services at VALiNTRY
Nekopoi APK 2025 free lastest update
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administraation Chapter 3
How to Migrate SBCGlobal Email to Yahoo Easily
Wondershare Filmora 15 Crack With Activation Key [2025
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
How Creative Agencies Leverage Project Management Software.pdf
L1 - Introduction to python Backend.pptx
Softaken Excel to vCard Converter Software.pdf
System and Network Administration Chapter 2
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
VVF-Customer-Presentation2025-Ver1.9.pptx

The Ring programming language version 1.2 book - Part 10 of 84

  • 1. Ring Documentation, Release 1.2 15.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 15.5 Do Again Loop Syntax: do Block of statements again expression Example: x = 1 do see x + nl x++ again x <= 10 15.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 15.4. Using For in to modify lists 70
  • 2. Ring Documentation, Release 1.2 15.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 • 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 15.8 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 15.9 Short-circuit evaluation The logical operators and/or follow the short-circuit evaluation. 15.7. Exit from two loops 71
  • 3. Ring Documentation, Release 1.2 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 15.10 Comments about evaluation • True, False, nl & NULL are variables defined by the language • True = 1 • False = 0 • nl = new line • NULL = empty string = “” 15.10. Comments about evaluation 72
  • 4. Ring Documentation, Release 1.2 • 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 15.10. Comments about evaluation 73
  • 5. CHAPTER SIXTEEN 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. 16.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 74
  • 6. Ring Documentation, Release 1.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 16.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: 16.2. Looping 75
  • 7. Ring Documentation, Release 1.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 16.3 Exceptions try Block of statements catch Block of statements end 16.3. Exceptions 76
  • 8. CHAPTER SEVENTEEN 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. 17.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") } 77
  • 9. Ring Documentation, Release 1.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") } 17.2 Looping • While Loop Syntax: while Expression { Block of statements } Example: Load "stdlib.ring" While True { print(" Main Menu --------- 17.2. Looping 78
  • 10. Ring Documentation, Release 1.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") } 17.2. Looping 79