Ruby/C2/Control-Statements/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Control Statements

Author: Anjana Nair


Keywords: if, elsif, else, case, statements loop, Ruby, video tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Control Statements in Ruby.
Slide 2 In this tutorial we will learn to use-
  • if statement
  • elsif
  • else
  • case statements



Slide 3

System Requirements

Here we are using
  • Ubuntu version 12.04
  • Ruby1.9.3


Slide 4

Pre-requisites

To follow this tutorial, you must have Internet Connection.


You must also have knowledge of Linux commands, Terminal and Text-editor.


If not, for relevant tutorials, please visit our website. <<PAUSE>>

Before we begin, recall that we had created “ttt” directory earlier.


Let's go to that directory.

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:


if “condition”

ruby code

end


Let us look at an example.

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.


Name it if-statement.rb

control-statements I have a working example of the if statement.


You can pause the tutorial, and type the code as we go through it.

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


and see the output.

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.


Let's look at the if-else statement next.

Slide 6

The “if-else statement”

The syntax for using else is:


if “condition”

ruby code

else

ruby code

end


Let us look at an example.

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.


Name it if-else-statement.rb

I have a working example of the if-else statement.


You can pause the tutorial, and type the code as we go through it.

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.


And it will print out the string that is specified there.

Now, let us switch to the terminal and type

ruby if-else-statement.rb


and see the output.

The output will display “The value of my_num is lesser than 0”.


This shows that the else statement was executed.

You should now be able to write your own if-else statement in Ruby.


Let's look at the if-elsif statement next.

Slide 7

The “if-elsif” statement

The syntax for using elsif is:


if “condition”

ruby code

elsif “condition”

ruby code

else

ruby code

end


Let us look at an example.

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.


Name it if-elsif-statement.rb

I have a working example of the if-elsif- statement.


You can pause the tutorial, and type the code as we go through it.

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


and see the output.

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.


Let's look at the case statement next.

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:


case variable

when “value 1”

ruby code

when “value 2”

ruby code

when “value 3”

ruby code

else

ruby code

end


Let us look at an example.

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.


Name it case-statement.rb

I have a working example of the case statement.


You can pause the tutorial, and type the code as we go through it.

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.


It will print “Unknown” as per our example.

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.


The output will display “Uttar Pradesh”.

Next execute the Ruby file again, like before.
Type “KL” This time at the prompt type in “KL” and see the output.


It will print “Kerala”.

Next execute the file one more time.
Type “TN” This time at the prompt type in “TN” and see the output.


It will print “Unknown”.

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

  • if statement
  • else construct
  • if-elsif and
  • case statements


Slide 11

Assignment

As an assignment :

Write a Ruby program :

  • that prompts a user to enter a number
  • then use the appropriate control-statement
  • to check if the number is a multiple of 2
  • if it is, then print “The number entered is a multiple of 2”
  • if not, it should check if it is a multiple of 3
  • if it is, then print “The number entered is a multiple of 3”
  • if not, it should check if it a multiple of 4
  • if it is, then print “The number entered is a multiple of 4”
  • if not, it should print “The number is not a multple of 2, 3 or 4”


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 :


Conducts workshops using spoken tutorials

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 ....)

Contributors and Content Editors

Anjana