SlideShare a Scribd company logo
Ring Documentation, Release 1.7
• 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
20.11. Comments about evaluation 192
CHAPTER
TWENTYONE
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.
21.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
193
Ring Documentation, Release 1.7
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
21.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:
21.2. Looping 194
Ring Documentation, Release 1.7
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
21.3 Exceptions
try
Block of statements
catch
Block of statements
end
21.3. Exceptions 195
CHAPTER
TWENTYTWO
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.
22.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")
}
196
Ring Documentation, Release 1.7
• 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")
}
22.2 Looping
• While Loop
Syntax:
while Expression {
Block of statements
}
Example:
Load "stdlib.ring"
While True {
print("
Main Menu
---------
22.2. Looping 197
Ring Documentation, Release 1.7
(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")
}
22.2. Looping 198
Ring Documentation, Release 1.7
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
22.3 Exceptions
22.3. Exceptions 199
Ring Documentation, Release 1.7
try {
Block of statements
catch
Block of statements
}
22.3. Exceptions 200
CHAPTER
TWENTYTHREE
GETTING INPUT
We can get input from the keyboard using
• The Give Command
• The GetChar() Function
• The Input() Function
23.1 Give Command
Syntax:
Give VariableName
Example:
See "Enter the first number : " Give nNum1
See "Enter the second number : " Give nNum2
See "Sum : " + ( 0 + nNum1 + nNum2 )
Output:
Enter the first number : 3
Enter the second number : 4
Sum : 7
23.2 GetChar() Function
We can get one character from the standard input using the GetChar() function
Syntax:
GetChar() ---> Character
Example:
While True
See "
Main Menu
(1) Say Hello
(2) Exit
"
Option = GetChar()
201

More Related Content

PDF
The Ring programming language version 1.5.4 book - Part 20 of 185
PDF
The Ring programming language version 1.5.3 book - Part 20 of 184
PDF
The Ring programming language version 1.6 book - Part 22 of 189
PDF
The Ring programming language version 1.3 book - Part 12 of 88
PDF
The Ring programming language version 1.10 book - Part 28 of 212
PDF
The Ring programming language version 1.5.2 book - Part 19 of 181
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
The Ring programming language version 1.5.4 book - Part 20 of 185
The Ring programming language version 1.5.3 book - Part 20 of 184
The Ring programming language version 1.6 book - Part 22 of 189
The Ring programming language version 1.3 book - Part 12 of 88
The Ring programming language version 1.10 book - Part 28 of 212
The Ring programming language version 1.5.2 book - Part 19 of 181
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

What's hot (20)

PDF
The Ring programming language version 1.8 book - Part 24 of 202
PDF
The Ring programming language version 1.2 book - Part 10 of 84
PDF
The Ring programming language version 1.5.1 book - Part 18 of 180
PDF
The Ring programming language version 1.2 book - Part 9 of 84
PDF
The Ring programming language version 1.10 book - Part 27 of 212
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.5.4 book - Part 27 of 185
PDF
The Ring programming language version 1.2 book - Part 13 of 84
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
Generics and Inference
KEY
Java Fx Tutorial01
PPT
Common Pitfalls Experienced in Java
PDF
The Ring programming language version 1.4 book - Part 7 of 30
PPTX
Looping e
DOC
oop Lecture 4
PPTX
Linux basic1&2
PDF
04 2 오버플로 상수 매크로
PDF
The Ring programming language version 1.9 book - Part 18 of 210
DOCX
X1x2 enter
The Ring programming language version 1.8 book - Part 24 of 202
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.5.1 book - Part 18 of 180
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.10 book - Part 27 of 212
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.5.4 book - Part 27 of 185
The Ring programming language version 1.2 book - Part 13 of 84
The Ring programming language version 1.8 book - Part 25 of 202
Generics and Inference
Java Fx Tutorial01
Common Pitfalls Experienced in Java
The Ring programming language version 1.4 book - Part 7 of 30
Looping e
oop Lecture 4
Linux basic1&2
04 2 오버플로 상수 매크로
The Ring programming language version 1.9 book - Part 18 of 210
X1x2 enter
Ad

Similar to The Ring programming language version 1.7 book - Part 23 of 196 (20)

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.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
PDF
The Ring programming language version 1.5.2 book - Part 18 of 181
PDF
The Ring programming language version 1.6 book - Part 21 of 189
PDF
The Ring programming language version 1.2 book - Part 54 of 84
PDF
The Ring programming language version 1.3 book - Part 11 of 88
PDF
The Ring programming language version 1.10 book - Part 87 of 212
PDF
The Ring programming language version 1.7 book - Part 79 of 196
PDF
The Ring programming language version 1.6 book - Part 77 of 189
PDF
The Ring programming language version 1.3 book - Part 13 of 88
PDF
The Ring programming language version 1.9 book - Part 7 of 210
PDF
The Ring programming language version 1.3 book - Part 57 of 88
PDF
The Ring programming language version 1.9 book - Part 86 of 210
PDF
The Ring programming language version 1.5.1 book - Part 17 of 180
PDF
The Ring programming language version 1.5.1 book - Part 71 of 180
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
PDF
The Ring programming language version 1.5.3 book - Part 84 of 184
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.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
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.6 book - Part 21 of 189
The Ring programming language version 1.2 book - Part 54 of 84
The Ring programming language version 1.3 book - Part 11 of 88
The Ring programming language version 1.10 book - Part 87 of 212
The Ring programming language version 1.7 book - Part 79 of 196
The Ring programming language version 1.6 book - Part 77 of 189
The Ring programming language version 1.3 book - Part 13 of 88
The Ring programming language version 1.9 book - Part 7 of 210
The Ring programming language version 1.3 book - Part 57 of 88
The Ring programming language version 1.9 book - Part 86 of 210
The Ring programming language version 1.5.1 book - Part 17 of 180
The Ring programming language version 1.5.1 book - Part 71 of 180
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.5.3 book - Part 84 of 184
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
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology
Per capita expenditure prediction using model stacking based on satellite ima...
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25 Week I
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx

The Ring programming language version 1.7 book - Part 23 of 196

  • 1. Ring Documentation, Release 1.7 • 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 20.11. Comments about evaluation 192
  • 2. CHAPTER TWENTYONE 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. 21.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 193
  • 3. Ring Documentation, Release 1.7 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 21.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: 21.2. Looping 194
  • 4. Ring Documentation, Release 1.7 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 21.3 Exceptions try Block of statements catch Block of statements end 21.3. Exceptions 195
  • 5. CHAPTER TWENTYTWO 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. 22.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") } 196
  • 6. Ring Documentation, Release 1.7 • 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") } 22.2 Looping • While Loop Syntax: while Expression { Block of statements } Example: Load "stdlib.ring" While True { print(" Main Menu --------- 22.2. Looping 197
  • 7. Ring Documentation, Release 1.7 (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") } 22.2. Looping 198
  • 8. Ring Documentation, Release 1.7 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 22.3 Exceptions 22.3. Exceptions 199
  • 9. Ring Documentation, Release 1.7 try { Block of statements catch Block of statements } 22.3. Exceptions 200
  • 10. CHAPTER TWENTYTHREE GETTING INPUT We can get input from the keyboard using • The Give Command • The GetChar() Function • The Input() Function 23.1 Give Command Syntax: Give VariableName Example: See "Enter the first number : " Give nNum1 See "Enter the second number : " Give nNum2 See "Sum : " + ( 0 + nNum1 + nNum2 ) Output: Enter the first number : 3 Enter the second number : 4 Sum : 7 23.2 GetChar() Function We can get one character from the standard input using the GetChar() function Syntax: GetChar() ---> Character Example: While True See " Main Menu (1) Say Hello (2) Exit " Option = GetChar() 201