Ruby/C2/Control-Statements/English
Title of script: Control Statements
Author: Anjana Nair
Keywords: if, elsif, else, case, statements loop, Ruby, video tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Control Statements in Ruby. |
Slide 2 | In this tutorial we will learn to use-
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Pre-requisites |
To follow this tutorial, you must have Internet Connection.
|
Before we begin, recall that we had created “ttt” directory earlier.
| |
Switch to the terminal window which has all the commands for creating the directories and the prompt should be in control-statements directory | Then to ruby-tutorial and control-statements directory.
|
Now that we are in that folder, let’s move ahead.
| |
Slide 5
The “if-statement” |
The syntax of the if statement in Ruby is as follows:
ruby code end
|
Switch to gedit where you have already opened the file “if-statement.rb” with the if statement code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
control-statements | I have a working example of the if statement.
|
Highlight “if” statement | I have declared an if statement in this example. |
Highlight “my_num” | First, I declare a local variable my_num and assign the value 2345 to it. |
Highlight the if condition.
Then highlight the string to be displayed as output. |
Then I declare an if statement.
The puts method declared within the if statement will display the output.
|
The if statement will check if the value of my_num is greater than 0.
If it is, it will print out the specified string. | |
Now, let us switch to the terminal and type
ruby if-statement.rb
| |
The output will display “The value of my_num is greater than 0”.
This output proves that the if condition returned true. | |
You should now be able to write your own if statement in Ruby.
| |
Slide 6
The “if-else statement” |
The syntax for using else is:
ruby code else ruby code end
|
Switch to gedit where you have already opened the file “if-else-statement.rb” with the if statement code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the if-else statement.
| |
Highlight “if” statement | I have declared an if-else statement in this example. |
Highlight “my_num” | First I declare a local variable my_num and assign the value of -1 to it. |
Highlight the if condition.
Then highlight the string to be displayed as output. |
Then I declare an if statement.
The if statement will check if the value of my_num is greater than 0.
|
If it is, it will print out the specified string. | |
Highlight the else condition.
Then highlight the string to be displayed as output. |
If not, it will go to the else statement.
|
Now, let us switch to the terminal and type
ruby if-else-statement.rb
| |
The output will display “The value of my_num is lesser than 0”.
| |
You should now be able to write your own if-else statement in Ruby.
| |
Slide 7
The “if-elsif” statement |
The syntax for using elsif is:
ruby code elsif “condition” ruby code else ruby code end
|
Switch to gedit where you have already opened the file “if-elsif-statement.rb” with the if statement code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the if-elsif- statement.
| |
Highlight “if” statement | I have declared an if-elsif statement in this example. |
Highlight “my_num” | Here also, I have declare a local variable my_num and assign the value -1 to it. |
Highlight the if condition.
Then highlight the string to be displayed as output. |
Then I declare an if statement.
The if statement will check if the value of my_num is greater than 0. If it is, it will print out the specified string. |
If this is not true, it will go into the elsif section.
It will now check if the value of my_num is equal to -1. If it is true, it will print out the string that is specified there. If the value of my_num is neither greater than 0 nor equal to -1 it will go into the else section. But since the value of my_num = -1 it will not proceed to the else block. | |
And it will exit the conditional statement.
| |
Now, let us switch to the terminal and type
ruby if-elsif-statement.rb
| |
The output will display “The value of my_num is -1 and is lesser than 0”. | |
Let's go back to our file and change the value of my_num to 5. | |
Let's save the code and execute it on the terminal. | |
So, now it fulfills the if condition and the specified string is printed. | |
Let's go back to our file and change the value of my_num to -5. | |
Let's save the code and execute it on the terminal. | |
In this case it fulfills the else condition and the puts statement within the else block gets executed. | |
You should now be able to write your own if- elsif statement in Ruby.
| |
Slide 8
The “case statement” |
The case statement is a control flow statement based on a particular selection.
Let us look at the syntax of the case statement in order to understand this statement. The syntax for using case is:
when “value 1” ruby code when “value 2” ruby code when “value 3” ruby code else ruby code end
|
Switch to gedit where you have already opened the file “case-statement.rb” with the case-statement code typed inside. | Create a new file in gedit as shown in the basic level Ruby tutorials.
|
I have a working example of the case statement.
| |
Highlight “case” statement | I have declared an case statement in this example. |
Highlight the “print” statement | Here I have a print statement, which will print a question on the terminal. |
Highlight “gets”. | Then I call a gets, which will accept a single line of data from the standard input. |
Then highlight “gets.chomp”. | Then I strip the input data of any new line characters using chomp. |
Then highlight “domain”. | I assign the result to a variable named domain. |
Highlight “when” | Then I declare a case statment.
Within that I declare a when statement . This checks whether the specified string matches value of domain. |
Highlight when "UP" | First it checks whether the value of domain is “UP”.
If it is so, it will print out “Uttar Pradesh” and exit the case statement. |
Highlight when "MP" | If domain is not “UP”, it checks whether the value of domain is “MP”. |
Highlight puts "Madhya Pradesh" | If it is so, it will print out “Madhya Pradesh” and so on... |
Highlight the when statements below the statements mentioned above. | It will continue checking the value of domain if no match was found so far.
At this point it will encounter the else statement as none of the above conditions were true. |
Highlight else statement | It will subsequently execute the ruby code that follows the else declaration.
|
Switch to terminal | Now, save the file, switch to the terminal and type
ruby case-statement.rb. |
“Enter the state you live in:” will be displayed on the terminal. | |
Type “UP” | Type in “UP” and see the output.
|
Next execute the Ruby file again, like before. | |
Type “KL” | This time at the prompt type in “KL” and see the output.
|
Next execute the file one more time. | |
Type “TN” | This time at the prompt type in “TN” and see the output.
|
This is because none of the cases were satisfied. So, the default else statement is executed. | |
You should now be able to write your own case-statements in Ruby. | |
Slide 9 | <<Pause>>
This brings us to the end of this Spoken Tutorial. |
Slide 10
Summary |
Let's summarize.
In this tutorial we have learnt to use
|
Slide 11
Assignment |
As an assignment :
Write a Ruby program :
|
lide 12
About the Spoken Tutorial Project |
Watch the video available at the following link.
It summarizes the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Slide 13 | The Spoken Tutorial Project Team :
Gives certificates to those who pass an online test For more details, please write to contact at spoken hyphen tutorial dot org |
Slide 14
Acknowledgements |
Spoken Tutorial Project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
This is Anjana Nair signing off. Thanks for watching (or joining or whatever else ....) |