Difference between revisions of "Ruby/C2/Control-Statements/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 2: Line 2:
  
 
{| border=1
 
{| border=1
|| ''Time'''
+
|| '''Time'''
 
|| '''Narration'''
 
|| '''Narration'''
  
 
|-
 
|-
|  00.01
+
|  00:01
 
| Welcome to the spoken tutorial on Control Statements in ''' Ruby.'''  
 
| Welcome to the spoken tutorial on Control Statements in ''' Ruby.'''  
  
 
|-
 
|-
| 00.06
+
| 00:06
 
| In this tutorial we will learn to use
 
| In this tutorial we will learn to use
  
 
|-
 
|-
| 00.08
+
| 00:08
 
| ''' if statement '''
 
| ''' if statement '''
  
 
|-
 
|-
| 00.09
+
| 00:09
 
|''' elsif statement '''
 
|''' elsif statement '''
  
 
|-
 
|-
| 00.11
+
| 00:11
 
| ''' else'''
 
| ''' else'''
  
 
|-
 
|-
|  00.12
+
|  00:12
 
|''' case statements'''
 
|''' case statements'''
  
 
|-
 
|-
|  00.14
+
|  00:14
 
|Here we are using  
 
|Here we are using  
  
 
|-
 
|-
|  00.15
+
|  00:15
 
|  ''' Ubuntu''' version 12.04  
 
|  ''' Ubuntu''' version 12.04  
  
 
|-
 
|-
| 00.18
+
| 00:18
 
|''' Ruby '''1.9.3  
 
|''' Ruby '''1.9.3  
  
 
|-
 
|-
| 00.21
+
| 00:21
 
|To follow this tutorial, you must have ''' Internet''' Connection.   
 
|To follow this tutorial, you must have ''' Internet''' Connection.   
 
    
 
    
  
 
|-
 
|-
|  00.24
+
|  00:24
 
| You must also have knowledge of '''Linux'''  commands, ''' Terminal''' and ''' Text-editor.'''
 
| You must also have knowledge of '''Linux'''  commands, ''' Terminal''' and ''' Text-editor.'''
  
Line 53: Line 53:
  
 
|-
 
|-
| 00.30
+
| 00:30
 
|If not, for relevant tutorials, please visit our website.  
 
|If not, for relevant tutorials, please visit our website.  
  
 
|-
 
|-
|  00.34
+
|  00:34
 
|  Before we begin, recall that we had created ''' “ttt” ''' directory earlier.  
 
|  Before we begin, recall that we had created ''' “ttt” ''' directory earlier.  
  
 
|-
 
|-
| 00.38
+
| 00:38
 
|Let's go to that directory.  
 
|Let's go to that directory.  
  
 
|-
 
|-
| 00.41
+
| 00:41
 
|Then to ''' ruby hyphen tutorial'''  ''' control hyphen statements'''  
 
|Then to ''' ruby hyphen tutorial'''  ''' control hyphen statements'''  
  
 
|-
 
|-
| 00.47
+
| 00:47
 
|Now that we are in that folder, let’s move ahead.  
 
|Now that we are in that folder, let’s move ahead.  
  
 
|-
 
|-
| 00.52
+
| 00:52
 
| The syntax of the ''' if statement''' in '''Ruby''' is as follows:  
 
| The syntax of the ''' if statement''' in '''Ruby''' is as follows:  
  
 
|-
 
|-
| 00.56
+
| 00:56
 
|''' if “condition” '''
 
|''' if “condition” '''
  
 
|-
 
|-
|  00.58
+
|  00:58
 
|  ''' ruby code'''  
 
|  ''' ruby code'''  
  
 
|-
 
|-
| 00.59
+
| 00:59
 
|'''end'''  
 
|'''end'''  
  
 
|-
 
|-
| 01.01
+
| 01:01
 
| Let us look at an example.  
 
| Let us look at an example.  
  
 
|-
 
|-
| 01.03
+
| 01:03
 
|Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.   
 
|Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.   
  
 
|-
 
|-
| 01.08
+
| 01:08
 
|Name it ''' if hyphen statement dot rb'''
 
|Name it ''' if hyphen statement dot rb'''
  
 
|-
 
|-
| 01.12
+
| 01:12
 
|I have a working example of the ''' if statement.'''  
 
|I have a working example of the ''' if statement.'''  
  
  
 
|-
 
|-
|  01.15
+
|  01:15
 
|  You can pause the tutorial, and type the code as we go through it.  
 
|  You can pause the tutorial, and type the code as we go through it.  
  
 
|-
 
|-
|  01.19
+
|  01:19
 
|I have declared an ''' if statement''' in this example.  
 
|I have declared an ''' if statement''' in this example.  
  
 
|-
 
|-
|  01.23
+
|  01:23
 
| First, I declare a local variable ''' my_num''' and assign the value 2345 to it.  
 
| First, I declare a local variable ''' my_num''' and assign the value 2345 to it.  
  
 
|-
 
|-
| 01.31
+
| 01:31
 
| Then I declare an ''' if''' statement.  
 
| Then I declare an ''' if''' statement.  
  
 
|-
 
|-
|  01.34
+
|  01:34
 
| The '''puts''' method declared within the '''if''' statement will display the output.  
 
| The '''puts''' method declared within the '''if''' statement will display the output.  
  
 
|-
 
|-
| 01.39
+
| 01:39
 
|The '''if statement''' will check  the value of ''' my_num''' is greater than 0.  
 
|The '''if statement''' will check  the value of ''' my_num''' is greater than 0.  
  
 
|-
 
|-
| 01.43
+
| 01:43
 
|If it is, it will print out the specified ''' string.'''  
 
|If it is, it will print out the specified ''' string.'''  
  
 
|-
 
|-
| 01.47
+
| 01:47
 
|Now, let us switch to the '''terminal''' and type  
 
|Now, let us switch to the '''terminal''' and type  
  
 
|-
 
|-
|  01.51
+
|  01:51
 
|  ''' ruby space  if hyphen statement dot rb'''  
 
|  ''' ruby space  if hyphen statement dot rb'''  
  
 
|-
 
|-
| 01.57
+
| 01:57
 
|  The output will display ''' “The value of my_num is greater than 0”.'''  
 
|  The output will display ''' “The value of my_num is greater than 0”.'''  
  
 
|-
 
|-
| 02.02
+
| 02:02
 
| This output proves that the '''if''' condition returned '''true.'''  
 
| This output proves that the '''if''' condition returned '''true.'''  
  
 
|-
 
|-
|  02.07
+
|  02:07
 
|You should now be able to write your own ''' if statement''' in '''Ruby.'''  
 
|You should now be able to write your own ''' if statement''' in '''Ruby.'''  
  
 
|-
 
|-
|  02.12
+
|  02:12
 
| Let's look at the ''' if-else statement''' next.  
 
| Let's look at the ''' if-else statement''' next.  
  
 
|-
 
|-
|  02.16
+
|  02:16
 
| The syntax for using '''else''' is:  
 
| The syntax for using '''else''' is:  
  
  
 
|-
 
|-
| 02.18
+
| 02:18
 
|''' if “condition” '''
 
|''' if “condition” '''
  
 
|-
 
|-
|  02.19
+
|  02:19
 
| ''' ruby code'''  
 
| ''' ruby code'''  
  
 
|-
 
|-
| 02.20
+
| 02:20
 
|'''else'''
 
|'''else'''
  
 
|-
 
|-
|  02.21
+
|  02:21
 
| '''ruby code '''
 
| '''ruby code '''
  
 
|-
 
|-
| 02.22
+
| 02:22
 
| ''' end'''
 
| ''' end'''
  
 
|-
 
|-
| 02.24
+
| 02:24
 
| Let us look at an example.  
 
| Let us look at an example.  
  
 
|-
 
|-
| 02.26
+
| 02:26
 
|Create a new file in ''' gedit''' as shown in the basic level ''' Ruby''' tutorials.   
 
|Create a new file in ''' gedit''' as shown in the basic level ''' Ruby''' tutorials.   
  
  
 
|-
 
|-
| 02.30
+
| 02:30
 
|Name it ''' if hyphen else hyphen statement dot rb'''
 
|Name it ''' if hyphen else hyphen statement dot rb'''
  
 
|-
 
|-
| 02.37
+
| 02:37
 
|I have a working example of the ''' if-else statement.'''  
 
|I have a working example of the ''' if-else statement.'''  
  
 
|-
 
|-
|  02.40
+
|  02:40
 
|  You can pause the tutorial, and type the code as we go through it.  
 
|  You can pause the tutorial, and type the code as we go through it.  
  
 
|-
 
|-
| 02.44
+
| 02:44
 
|I have declared an ''' if-else statement''' in this example.  
 
|I have declared an ''' if-else statement''' in this example.  
  
 
|-
 
|-
| 02.48
+
| 02:48
 
| First I declare a local variable ''' my_num''' and assign the value of -1 to it.  
 
| First I declare a local variable ''' my_num''' and assign the value of -1 to it.  
  
 
|-
 
|-
| 02.55
+
| 02:55
 
|Then I declare an '''if''' statement.  
 
|Then I declare an '''if''' statement.  
  
 
|-
 
|-
| 02.58
+
| 02:58
 
|The '''if''' statement will check if the value of ''' my_num''' is greater than 0.  
 
|The '''if''' statement will check if the value of ''' my_num''' is greater than 0.  
  
  
 
|-
 
|-
| 03.03
+
| 03:03
 
| If it is, it will print out the specified '''string.'''
 
| If it is, it will print out the specified '''string.'''
  
 
|-
 
|-
| 03.06
+
| 03:06
 
|If not, it will go to the '''else''' statement.  
 
|If not, it will go to the '''else''' statement.  
  
 
|-
 
|-
|  03.10
+
|  03:10
 
|  And it will print out the ''' string''' that is specified there.  
 
|  And it will print out the ''' string''' that is specified there.  
  
 
|-
 
|-
|  03.13
+
|  03:13
 
|  Now, let us switch to the '''terminal''' and type  
 
|  Now, let us switch to the '''terminal''' and type  
  
 
|-
 
|-
|  03.18
+
|  03:18
 
|'''ruby space if hyphen else hyphen statement dot rb'''  
 
|'''ruby space if hyphen else hyphen statement dot rb'''  
  
  
 
|-
 
|-
|  03.26
+
|  03:26
 
|  and see the output.   
 
|  and see the output.   
  
 
|-
 
|-
| 03.27
+
| 03:27
 
|The output will display ''' “The value of my_num is lesser than 0”.'''  
 
|The output will display ''' “The value of my_num is lesser than 0”.'''  
  
 
|-
 
|-
|  03.32
+
|  03:32
 
| This shows that the ''' else statement''' was executed.  
 
| This shows that the ''' else statement''' was executed.  
  
 
|-
 
|-
|  03.35
+
|  03:35
 
| You should now be able to write your own ''' if-else statement''' in '''Ruby.'''  
 
| You should now be able to write your own ''' if-else statement''' in '''Ruby.'''  
  
 
|-
 
|-
| 03.41
+
| 03:41
 
|Let's look at the ''' if-elsif statement''' next.  
 
|Let's look at the ''' if-elsif statement''' next.  
  
 
|-
 
|-
|  03.45
+
|  03:45
 
|  The syntax for using ''' elsif''' is:  
 
|  The syntax for using ''' elsif''' is:  
  
 
|-
 
|-
| 03.48
+
| 03:48
 
|''' if “condition” '''  ''' ruby code'''  
 
|''' if “condition” '''  ''' ruby code'''  
  
  
 
|-
 
|-
| 03.50
+
| 03:50
 
| ''' elsif “condition” ''' ''' ruby code'''  
 
| ''' elsif “condition” ''' ''' ruby code'''  
 
   
 
   
  
 
|-
 
|-
| 03.52
+
| 03:52
 
|'''else''' ''' ruby code'''
 
|'''else''' ''' ruby code'''
  
 
|-
 
|-
| 03.54
+
| 03:54
 
|'''end '''
 
|'''end '''
  
 
|-
 
|-
| 03.55
+
| 03:55
 
|Let us look at an example.  
 
|Let us look at an example.  
  
 
|-
 
|-
|  03.58
+
|  03:58
 
| Create a new file in '''gedit''' as shown in the basic level '''Ruby''' tutorials.  
 
| Create a new file in '''gedit''' as shown in the basic level '''Ruby''' tutorials.  
  
  
 
|-
 
|-
| 04.01
+
| 04:01
 
|Name it ''' if hyphen elsif hyphen statement dot rb'''
 
|Name it ''' if hyphen elsif hyphen statement dot rb'''
  
  
 
|-
 
|-
|  04.07
+
|  04:07
 
| I have a working example of the ''' if-elsif- statement.'''  
 
| I have a working example of the ''' if-elsif- statement.'''  
  
  
 
|-
 
|-
|  04.10
+
|  04:10
 
|You can pause the tutorial, and type the code as we go through it.  
 
|You can pause the tutorial, and type the code as we go through it.  
  
 
|-
 
|-
| 04.14
+
| 04:14
 
| I have declared an '''if-elsif statement''' in this example.  
 
| I have declared an '''if-elsif statement''' in this example.  
  
 
|-
 
|-
| 04.19
+
| 04:19
 
|Here also, I have declare a local variable '''my_num''' and assign the value -1 to it.  
 
|Here also, I have declare a local variable '''my_num''' and assign the value -1 to it.  
  
 
|-
 
|-
| 04.25
+
| 04:25
 
|Then I declare an ''' if statement. '''
 
|Then I declare an ''' if statement. '''
  
 
|-
 
|-
| 04.28
+
| 04:28
 
| The ''' if statement ''' will check if the value of '''my_num''' is greater than 0.  
 
| The ''' if statement ''' will check if the value of '''my_num''' is greater than 0.  
  
 
|-
 
|-
| 04.32
+
| 04:32
 
| If it is, it will print out the specified string.  
 
| If it is, it will print out the specified string.  
  
  
 
|-
 
|-
| 04.35
+
| 04:35
 
| If this is not ''' true''' , it will go into the '''elsif''' section.  
 
| If this is not ''' true''' , it will go into the '''elsif''' section.  
  
 
|-
 
|-
| 04.39
+
| 04:39
 
| It will now check if the value of ''' my_num''' is equal to -1.  
 
| It will now check if the value of ''' my_num''' is equal to -1.  
  
 
|-
 
|-
| 04.43
+
| 04:43
 
|If it is true, it will print out the string that is specified there.  
 
|If it is true, it will print out the string that is specified there.  
  
 
|-
 
|-
| 04.46
+
| 04:46
 
| The value of '''my_num''' is neither greater than 0 nor equal to -1 it will go into the '''else''' section.  
 
| The value of '''my_num''' is neither greater than 0 nor equal to -1 it will go into the '''else''' section.  
  
  
 
|-
 
|-
| 04.54
+
| 04:54
 
|But since the value of '''my_num = -1''' it will not proceed to the else block.  
 
|But since the value of '''my_num = -1''' it will not proceed to the else block.  
  
 
|-
 
|-
| 05.00
+
| 05:00
 
|And it will exit the ''' conditional statement.'''  
 
|And it will exit the ''' conditional statement.'''  
  
 
|-
 
|-
| 05.03
+
| 05:03
 
|Now, let us switch to the ''' terminal''' and type  
 
|Now, let us switch to the ''' terminal''' and type  
  
 
|-
 
|-
| 05.07
+
| 05:07
 
|''' ruby space if hyphen elsif hyphen statement dot rb'''  
 
|''' ruby space if hyphen elsif hyphen statement dot rb'''  
  
 
|-
 
|-
| 05.15
+
| 05:15
 
| and see the output.  
 
| and see the output.  
  
 
|-
 
|-
|05.17
+
|05:17
 
| The output will display ''' “The value of my_num is -1 and is lesser than 0”.'''  
 
| The output will display ''' “The value of my_num is -1 and is lesser than 0”.'''  
  
 
|-
 
|-
| 05.23
+
| 05:23
 
|Let's go back to our file and change the value of '''my_num''' to 5.  
 
|Let's go back to our file and change the value of '''my_num''' to 5.  
  
 
|-
 
|-
| 05.29
+
| 05:29
 
|Let's save the code and execute it on the terminal.  
 
|Let's save the code and execute it on the terminal.  
  
 
|-
 
|-
| 05.35
+
| 05:35
 
|So, now it fulfills the if condition and the specified string is printed.  
 
|So, now it fulfills the if condition and the specified string is printed.  
  
 
|-
 
|-
|  05.42
+
|  05:42
 
| The value of ''' my_num''' is greater than 0
 
| The value of ''' my_num''' is greater than 0
  
 
|-
 
|-
| 05.45
+
| 05:45
 
|Let's go back to our file and change the value of  ''' my_num''' to -5
 
|Let's go back to our file and change the value of  ''' my_num''' to -5
  
 
|-
 
|-
| 05.50
+
| 05:50
 
|Lets save the code and execute it on the terminal
 
|Lets save the code and execute it on the terminal
  
 
|-
 
|-
|  05.55
+
|  05:55
 
|  In this case it fulfills the ''' else''' condition and the puts statement within the else block gets executed.  
 
|  In this case it fulfills the ''' else''' condition and the puts statement within the else block gets executed.  
 
   
 
   
 
|-
 
|-
| 06.03
+
| 06:03
 
|You should now be able to write your own ''' if- elsif''' statement in ''' Ruby. '''
 
|You should now be able to write your own ''' if- elsif''' statement in ''' Ruby. '''
  
 
|-
 
|-
| 06.08
+
| 06:08
 
|Let us look at the ''' case statement''' next.  
 
|Let us look at the ''' case statement''' next.  
  
 
|-
 
|-
| 06.12
+
| 06:12
 
| The ''' case statement''' is a ''' control flow statement''' based on a particular selection.  
 
| The ''' case statement''' is a ''' control flow statement''' based on a particular selection.  
  
 
|-
 
|-
| 06.17
+
| 06:17
 
| Let us look at the syntax of the '''case statement''' in order to understand this statement.  
 
| Let us look at the syntax of the '''case statement''' in order to understand this statement.  
  
 
|-
 
|-
| 06.22
+
| 06:22
 
|The syntax for using '''case''' is:  
 
|The syntax for using '''case''' is:  
  
 
|-
 
|-
| 06.24
+
| 06:24
 
|''' case variable'''
 
|''' case variable'''
  
 
|-
 
|-
| 06.26
+
| 06:26
 
|''' when “value 1” '''
 
|''' when “value 1” '''
  
 
|-
 
|-
| 06.28
+
| 06:28
 
|'''ruby code '''
 
|'''ruby code '''
  
 
|-
 
|-
| 06.29
+
| 06:29
 
|''' when “value 2” '''
 
|''' when “value 2” '''
  
 
|-
 
|-
| 06.30
+
| 06:30
 
| '''ruby code '''
 
| '''ruby code '''
  
 
|-
 
|-
| 06.31
+
| 06:31
 
| '''else'''
 
| '''else'''
  
 
|-
 
|-
| 06.32
+
| 06:32
 
| ''' ruby code '''
 
| ''' ruby code '''
  
  
 
|-
 
|-
| 06.34
+
| 06:34
 
|'''end'''
 
|'''end'''
  
 
|-
 
|-
| 06.35
+
| 06:35
 
|Let us look at an example.  
 
|Let us look at an example.  
  
 
|-
 
|-
| 06.37
+
| 06:37
 
|Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.  
 
|Create a new file in ''' gedit''' as shown in the basic level '''Ruby''' tutorials.  
  
 
|-
 
|-
| 06.41
+
| 06:41
 
|Name it '''case hyphen statement dot rb'''
 
|Name it '''case hyphen statement dot rb'''
  
 
|-
 
|-
| 06.44
+
| 06:44
 
|I have a working example of the ''' case statement.'''  
 
|I have a working example of the ''' case statement.'''  
  
 
|-
 
|-
| 06.48
+
| 06:48
 
|You can pause the tutorial, and type the code as we go through it.  
 
|You can pause the tutorial, and type the code as we go through it.  
  
 
|-
 
|-
| 06.52
+
| 06:52
 
|I have declared an ''' case statement''' in this example.  
 
|I have declared an ''' case statement''' in this example.  
  
 
|-
 
|-
| 06.55
+
| 06:55
 
|Here I have a '''print statement,''' which will print a question on the terminal.  
 
|Here I have a '''print statement,''' which will print a question on the terminal.  
  
 
|-
 
|-
| 07.01
+
| 07:01
 
|Then I call a '''gets''', which will accept a single line of data from the standard input.  
 
|Then I call a '''gets''', which will accept a single line of data from the standard input.  
  
 
|-
 
|-
| 07.09
+
| 07:09
 
|Then I strip the input data of any '''new line characters''' using '''chomp.'''  
 
|Then I strip the input data of any '''new line characters''' using '''chomp.'''  
  
 
|-
 
|-
| 07.15
+
| 07:15
 
|I assign the result to a variable named '''domain.'''
 
|I assign the result to a variable named '''domain.'''
  
  
 
|-
 
|-
| 07.18
+
| 07:18
 
|Then I declare a case statement.  
 
|Then I declare a case statement.  
  
 
|-
 
|-
| 07.22
+
| 07:22
 
|Within that I declare a '''when statement '''.  
 
|Within that I declare a '''when statement '''.  
  
  
 
|-
 
|-
| 07.25
+
| 07:25
 
|This checks whether the specified string matches value of ''' domain.'''  
 
|This checks whether the specified string matches value of ''' domain.'''  
  
 
|-
 
|-
| 07.30
+
| 07:30
 
|First it checks whether the value of '''domain''' is ''' “UP”. '''
 
|First it checks whether the value of '''domain''' is ''' “UP”. '''
  
 
|-
 
|-
| 07.34
+
| 07:34
 
|If it is so, it will print out ''' “Uttar Pradesh” ''' and exit the '''case statement.'''  
 
|If it is so, it will print out ''' “Uttar Pradesh” ''' and exit the '''case statement.'''  
  
 
|-
 
|-
| 07.39
+
| 07:39
 
|If '''domain''' is not ''' “UP”''', it checks whether the value of ''' domain''' is ''' “MP”.'''
 
|If '''domain''' is not ''' “UP”''', it checks whether the value of ''' domain''' is ''' “MP”.'''
  
 
|-
 
|-
| 07.44
+
| 07:44
 
|If it is so, it will print out ''' “Madhya Pradesh” ''' and so on.
 
|If it is so, it will print out ''' “Madhya Pradesh” ''' and so on.
  
 
|-
 
|-
| 07.48
+
| 07:48
 
|It will continue checking the value of ''' domain''' if no match was found so far.  
 
|It will continue checking the value of ''' domain''' if no match was found so far.  
  
 
|-
 
|-
| 07.53
+
| 07:53
 
|At this point it will encounter the ''' else statement'''  
 
|At this point it will encounter the ''' else statement'''  
  
 
|-
 
|-
| 07.56
+
| 07:56
 
|as none of the above conditions were true.  
 
|as none of the above conditions were true.  
  
 
|-
 
|-
| 07.59
+
| 07:59
 
|It will subsequently execute the ''' ruby code''' that follows the '''else''' declaration.  
 
|It will subsequently execute the ''' ruby code''' that follows the '''else''' declaration.  
  
 
|-
 
|-
| 08.03
+
| 08:03
 
|It will print ''' “Unknown”''' as per our example.  
 
|It will print ''' “Unknown”''' as per our example.  
  
 
|-
 
|-
| 08.07
+
| 08:07
 
|Now, save the file, switch to the ''' terminal''' and type  
 
|Now, save the file, switch to the ''' terminal''' and type  
  
  
 
|-
 
|-
| 08.11
+
| 08:11
 
|''' ruby space case hyphen statement dot rb.'''  
 
|''' ruby space case hyphen statement dot rb.'''  
  
 
|-
 
|-
| 08.18
+
| 08:18
 
|''' “Enter the state you live in:”''' will be displayed on the '''terminal.'''
 
|''' “Enter the state you live in:”''' will be displayed on the '''terminal.'''
  
 
|-
 
|-
| 08.22
+
| 08:22
 
|Type in ''' “UP”''' and see the output.  
 
|Type in ''' “UP”''' and see the output.  
  
 
|-
 
|-
| 08.25
+
| 08:25
 
|The output will display ''' “Uttar Pradesh”.'''
 
|The output will display ''' “Uttar Pradesh”.'''
  
 
|-
 
|-
| 08.28
+
| 08:28
 
|Next execute the ''' Ruby''' file again, like before.  
 
|Next execute the ''' Ruby''' file again, like before.  
  
  
 
|-
 
|-
| 08.31
+
| 08:31
 
|This time at the prompt type ''' “KL”''' and see the output.  
 
|This time at the prompt type ''' “KL”''' and see the output.  
  
 
|-
 
|-
| 08.36
+
| 08:36
 
|It will print ''' “Kerala”.'''  
 
|It will print ''' “Kerala”.'''  
  
 
|-
 
|-
| 08.38
+
| 08:38
 
|Next execute the file one more time.  
 
|Next execute the file one more time.  
  
 
|-
 
|-
| 08.41
+
| 08:41
 
|This time at the prompt type in ''' “TN”''' and see the output.  
 
|This time at the prompt type in ''' “TN”''' and see the output.  
  
 
|-
 
|-
| 08.47
+
| 08:47
 
|It will print ''' “Unknown”.'''  
 
|It will print ''' “Unknown”.'''  
  
 
|-
 
|-
| 08.50
+
| 08:50
 
|This is because none of the ''' cases''' were satisfied. So, the default '''else statement''' is executed.  
 
|This is because none of the ''' cases''' were satisfied. So, the default '''else statement''' is executed.  
  
 
|-
 
|-
| 08.58
+
| 08:58
 
|You should now be able to write your own ''' case-statements''' in '''Ruby. '''
 
|You should now be able to write your own ''' case-statements''' in '''Ruby. '''
  
 
|-
 
|-
| 09.03
+
| 09:03
 
|This brings us to the end of this Spoken Tutorial.  
 
|This brings us to the end of this Spoken Tutorial.  
  
  
 
|-
 
|-
| 09.07
+
| 09:07
 
|Let's summarize.  
 
|Let's summarize.  
  
 
|-
 
|-
| 09.08
+
| 09:08
 
|In this tutorial we have learnt to use  
 
|In this tutorial we have learnt to use  
  
  
 
|-
 
|-
| 09.10
+
| 09:10
 
|''' if statement'''  
 
|''' if statement'''  
  
 
|-
 
|-
| 09.12
+
| 09:12
 
|''' else''' construct  
 
|''' else''' construct  
  
 
|-
 
|-
| 09.13
+
| 09:13
 
|'''if-elsif''' and  
 
|'''if-elsif''' and  
  
 
|-
 
|-
| 09.15
+
| 09:15
 
|''' case statements'''  
 
|''' case statements'''  
  
 
|-
 
|-
| 09.17
+
| 09:17
 
|As an assignment :  
 
|As an assignment :  
  
 
|-
 
|-
| 09.18
+
| 09:18
 
|Write a '''Ruby''' program :  
 
|Write a '''Ruby''' program :  
  
  
 
|-
 
|-
| 09.20
+
| 09:20
 
|that prompts a user to enter a number  
 
|that prompts a user to enter a number  
  
  
 
|-
 
|-
| 09.23
+
| 09:23
 
|then use the appropriate control-statement  
 
|then use the appropriate control-statement  
  
 
|-
 
|-
| 09.26
+
| 09:26
 
|to check if the number is a multiple of 2  
 
|to check if the number is a multiple of 2  
  
 
|-
 
|-
|09.29
+
|09:29
 
|if it is, then print “The number entered is a multiple of 2”  
 
|if it is, then print “The number entered is a multiple of 2”  
  
 
|-
 
|-
| 09.35
+
| 09:35
 
|if not, it should check if it is a multiple of 3  
 
|if not, it should check if it is a multiple of 3  
  
 
|-
 
|-
| 09.38
+
| 09:38
 
|if it is, then print “The number entered is a multiple of 3”  
 
|if it is, then print “The number entered is a multiple of 3”  
  
 
|-
 
|-
| 09.43
+
| 09:43
 
|if not, it should check if it a multiple of 4  
 
|if not, it should check if it a multiple of 4  
  
 
|-
 
|-
| 09.47
+
| 09:47
 
|if it is, then print “The number entered is a multiple of 4”  
 
|if it is, then print “The number entered is a multiple of 4”  
  
 
|-
 
|-
| 09.51
+
| 09:51
 
|if not, it should print “The number is not a multple of 2, 3 or 4”  
 
|if not, it should print “The number is not a multple of 2, 3 or 4”  
  
 
|-
 
|-
|  09.56
+
|  09:56
 
|  Watch the video available at the following link.
 
|  Watch the video available at the following link.
  
 
|-
 
|-
| 10.00
+
| 10:00
 
|It summarises the Spoken Tutorial project.
 
|It summarises the Spoken Tutorial project.
  
 
|-
 
|-
| 10.03
+
| 10:03
 
|If you do not have good bandwidth, you can download and  watch it.  
 
|If you do not have good bandwidth, you can download and  watch it.  
  
 
|-
 
|-
|  10.07
+
|  10:07
 
|  The Spoken Tutorial Project Team :
 
|  The Spoken Tutorial Project Team :
  
 
|-   
 
|-   
| 10.09
+
| 10:09
 
|Conducts workshops using spoken tutorials  
 
|Conducts workshops using spoken tutorials  
  
 
|-
 
|-
|10.13
+
|10:13
 
|Gives certificates to those who pass an online test  
 
|Gives certificates to those who pass an online test  
  
 
|-
 
|-
| 10.16
+
| 10:16
 
|For more details, please write to contact@spoken-tutorial.org
 
|For more details, please write to contact@spoken-tutorial.org
  
 
|-
 
|-
|  10.21
+
|  10:21
 
|  Spoken Tutorial Project is a part of the Talk to a Teacher project.
 
|  Spoken Tutorial Project is a part of the Talk to a Teacher project.
  
 
|-
 
|-
| 10.26
+
| 10:26
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 
|-
 
|-
| 10.32
+
| 10:32
 
|More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
 
|More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
  
 
|-
 
|-
| 10.41
+
| 10:41
 
|This is Anjani Nair signing off. Thanks for watching.
 
|This is Anjani Nair signing off. Thanks for watching.
  

Revision as of 15:48, 10 July 2014


Time Narration
00:01 Welcome to the spoken tutorial on Control Statements in Ruby.
00:06 In this tutorial we will learn to use
00:08 if statement
00:09 elsif statement
00:11 else
00:12 case statements
00:14 Here we are using
00:15 Ubuntu version 12.04
00:18 Ruby 1.9.3
00:21 To follow this tutorial, you must have Internet Connection.


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


00:30 If not, for relevant tutorials, please visit our website.
00:34 Before we begin, recall that we had created “ttt” directory earlier.
00:38 Let's go to that directory.
00:41 Then to ruby hyphen tutorial control hyphen statements
00:47 Now that we are in that folder, let’s move ahead.
00:52 The syntax of the if statement in Ruby is as follows:
00:56 if “condition”
00:58 ruby code
00:59 end
01:01 Let us look at an example.
01:03 Create a new file in gedit as shown in the basic level Ruby tutorials.
01:08 Name it if hyphen statement dot rb
01:12 I have a working example of the if statement.


01:15 You can pause the tutorial, and type the code as we go through it.
01:19 I have declared an if statement in this example.
01:23 First, I declare a local variable my_num and assign the value 2345 to it.
01:31 Then I declare an if statement.
01:34 The puts method declared within the if statement will display the output.
01:39 The if statement will check the value of my_num is greater than 0.
01:43 If it is, it will print out the specified string.
01:47 Now, let us switch to the terminal and type
01:51 ruby space if hyphen statement dot rb
01:57 The output will display “The value of my_num is greater than 0”.
02:02 This output proves that the if condition returned true.
02:07 You should now be able to write your own if statement in Ruby.
02:12 Let's look at the if-else statement next.
02:16 The syntax for using else is:


02:18 if “condition”
02:19 ruby code
02:20 else
02:21 ruby code
02:22 end
02:24 Let us look at an example.
02:26 Create a new file in gedit as shown in the basic level Ruby tutorials.


02:30 Name it if hyphen else hyphen statement dot rb
02:37 I have a working example of the if-else statement.
02:40 You can pause the tutorial, and type the code as we go through it.
02:44 I have declared an if-else statement in this example.
02:48 First I declare a local variable my_num and assign the value of -1 to it.
02:55 Then I declare an if statement.
02:58 The if statement will check if the value of my_num is greater than 0.


03:03 If it is, it will print out the specified string.
03:06 If not, it will go to the else statement.
03:10 And it will print out the string that is specified there.
03:13 Now, let us switch to the terminal and type
03:18 ruby space if hyphen else hyphen statement dot rb


03:26 and see the output.
03:27 The output will display “The value of my_num is lesser than 0”.
03:32 This shows that the else statement was executed.
03:35 You should now be able to write your own if-else statement in Ruby.
03:41 Let's look at the if-elsif statement next.
03:45 The syntax for using elsif is:
03:48 if “condition” ruby code


03:50 elsif “condition” ruby code


03:52 else ruby code
03:54 end
03:55 Let us look at an example.
03:58 Create a new file in gedit as shown in the basic level Ruby tutorials.


04:01 Name it if hyphen elsif hyphen statement dot rb


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


04:10 You can pause the tutorial, and type the code as we go through it.
04:14 I have declared an if-elsif statement in this example.
04:19 Here also, I have declare a local variable my_num and assign the value -1 to it.
04:25 Then I declare an if statement.
04:28 The if statement will check if the value of my_num is greater than 0.
04:32 If it is, it will print out the specified string.


04:35 If this is not true , it will go into the elsif section.
04:39 It will now check if the value of my_num is equal to -1.
04:43 If it is true, it will print out the string that is specified there.
04:46 The value of my_num is neither greater than 0 nor equal to -1 it will go into the else section.


04:54 But since the value of my_num = -1 it will not proceed to the else block.
05:00 And it will exit the conditional statement.
05:03 Now, let us switch to the terminal and type
05:07 ruby space if hyphen elsif hyphen statement dot rb
05:15 and see the output.
05:17 The output will display “The value of my_num is -1 and is lesser than 0”.
05:23 Let's go back to our file and change the value of my_num to 5.
05:29 Let's save the code and execute it on the terminal.
05:35 So, now it fulfills the if condition and the specified string is printed.
05:42 The value of my_num is greater than 0
05:45 Let's go back to our file and change the value of my_num to -5
05:50 Lets save the code and execute it on the terminal
05:55 In this case it fulfills the else condition and the puts statement within the else block gets executed.
06:03 You should now be able to write your own if- elsif statement in Ruby.
06:08 Let us look at the case statement next.
06:12 The case statement is a control flow statement based on a particular selection.
06:17 Let us look at the syntax of the case statement in order to understand this statement.
06:22 The syntax for using case is:
06:24 case variable
06:26 when “value 1”
06:28 ruby code
06:29 when “value 2”
06:30 ruby code
06:31 else
06:32 ruby code


06:34 end
06:35 Let us look at an example.
06:37 Create a new file in gedit as shown in the basic level Ruby tutorials.
06:41 Name it case hyphen statement dot rb
06:44 I have a working example of the case statement.
06:48 You can pause the tutorial, and type the code as we go through it.
06:52 I have declared an case statement in this example.
06:55 Here I have a print statement, which will print a question on the terminal.
07:01 Then I call a gets, which will accept a single line of data from the standard input.
07:09 Then I strip the input data of any new line characters using chomp.
07:15 I assign the result to a variable named domain.


07:18 Then I declare a case statement.
07:22 Within that I declare a when statement .


07:25 This checks whether the specified string matches value of domain.
07:30 First it checks whether the value of domain is “UP”.
07:34 If it is so, it will print out “Uttar Pradesh” and exit the case statement.
07:39 If domain is not “UP”, it checks whether the value of domain is “MP”.
07:44 If it is so, it will print out “Madhya Pradesh” and so on.
07:48 It will continue checking the value of domain if no match was found so far.
07:53 At this point it will encounter the else statement
07:56 as none of the above conditions were true.
07:59 It will subsequently execute the ruby code that follows the else declaration.
08:03 It will print “Unknown” as per our example.
08:07 Now, save the file, switch to the terminal and type


08:11 ruby space case hyphen statement dot rb.
08:18 “Enter the state you live in:” will be displayed on the terminal.
08:22 Type in “UP” and see the output.
08:25 The output will display “Uttar Pradesh”.
08:28 Next execute the Ruby file again, like before.


08:31 This time at the prompt type “KL” and see the output.
08:36 It will print “Kerala”.
08:38 Next execute the file one more time.
08:41 This time at the prompt type in “TN” and see the output.
08:47 It will print “Unknown”.
08:50 This is because none of the cases were satisfied. So, the default else statement is executed.
08:58 You should now be able to write your own case-statements in Ruby.
09:03 This brings us to the end of this Spoken Tutorial.


09:07 Let's summarize.
09:08 In this tutorial we have learnt to use


09:10 if statement
09:12 else construct
09:13 if-elsif and
09:15 case statements
09:17 As an assignment :
09:18 Write a Ruby program :


09:20 that prompts a user to enter a number


09:23 then use the appropriate control-statement
09:26 to check if the number is a multiple of 2
09:29 if it is, then print “The number entered is a multiple of 2”
09:35 if not, it should check if it is a multiple of 3
09:38 if it is, then print “The number entered is a multiple of 3”
09:43 if not, it should check if it a multiple of 4
09:47 if it is, then print “The number entered is a multiple of 4”
09:51 if not, it should print “The number is not a multple of 2, 3 or 4”
09:56 Watch the video available at the following link.
10:00 It summarises the Spoken Tutorial project.
10:03 If you do not have good bandwidth, you can download and watch it.
10:07 The Spoken Tutorial Project Team :
10:09 Conducts workshops using spoken tutorials
10:13 Gives certificates to those who pass an online test
10:16 For more details, please write to contact@spoken-tutorial.org
10:21 Spoken Tutorial Project is a part of the Talk to a Teacher project.
10:26 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:32 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
10:41 This is Anjani Nair signing off. Thanks for watching.



Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14