Difference between revisions of "Ruby/C3/while-and-until-Looping-Statements/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 2: | Line 2: | ||
{| border=1 | {| border=1 | ||
− | + | | '''Time''' | |
− | + | | '''Narration''' | |
|- | |- | ||
− | | 00 | + | | 00:01 |
− | | Welcome to the tutorial on '''while''' | + | | Welcome to the tutorial on '''while and until loops''' in '''Ruby'''. |
|- | |- | ||
− | | 00 | + | | 00:06 |
− | | In this tutorial we will learn to use- | + | | In this tutorial, we will learn to use- |
|- | |- | ||
− | | 00 | + | | 00:09 |
− | | '''while loop ''' | + | | ''''while' loop ''', ''''until' loop ''' |
|- | |- | ||
− | | 00 | + | | 00:11 |
− | |''' | + | | '''redo''' and '''break'''. |
|- | |- | ||
− | | 00 | + | | 00:13 |
− | | ''' | + | | We are using: ''' Ubuntu''' version '''12.04''' |
|- | |- | ||
− | | | + | | 00:17 |
− | | | + | | '''Ruby 1.9.3''' |
|- | |- | ||
− | | | + | | 00:20 |
− | + | | To follow this tutorial, you must have ''' Internet''' connection. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |To follow this tutorial, you must have ''' Internet''' | + | |
− | |||
|- | |- | ||
− | | 00 | + | | 00:25 |
| You must also have knowledge of '''Linux''' commands, ''' Terminal''' and ''' Text-editor.''' | | You must also have knowledge of '''Linux''' commands, ''' Terminal''' and ''' Text-editor.''' | ||
− | |||
− | |||
|- | |- | ||
− | | 00 | + | | 00:29 |
|If not, for relevant tutorials, please visit our website. | |If not, for relevant tutorials, please visit our website. | ||
|- | |- | ||
− | | 00 | + | | 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 | + | | 00:38 |
|Let's go to that directory. | |Let's go to that directory. | ||
|- | |- | ||
− | | 00 | + | | 00:41 |
− | |Then to ''' ruby hyphen tutorial''' and '''looping hyphen statements''' | + | |Then to ''' ruby hyphen tutorial''' and '''looping hyphen statements''' directories. |
|- | |- | ||
− | | 00 | + | | 00:46 |
|Now that we are in that folder, let’s move ahead. | |Now that we are in that folder, let’s move ahead. | ||
|- | |- | ||
− | | 00 | + | | 00:50 |
| The syntax of the '''while''' loop in ''' Ruby''' is as follows: | | The syntax of the '''while''' loop in ''' Ruby''' is as follows: | ||
|- | |- | ||
− | | 00 | + | | 00:54 |
|''' while “boolean expression” ''' | |''' while “boolean expression” ''' | ||
|- | |- | ||
− | | 00 | + | | 00:56 |
− | |''' ruby code ''' | + | |''' ruby code ''', '''end''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:58 |
| Let us look at an example. | | Let us look at an example. | ||
|- | |- | ||
− | | 01 | + | | 01:01 |
− | | Create a new file in '''gedit''' as shown in the basic level ''' Ruby''' | + | | Create a new file in '''gedit''' as shown in the basic level '''Ruby tutorials'''. |
|- | |- | ||
− | | 01 | + | | 01:05 |
− | | Name it ''' while hyphen loop dot rb ''' | + | | Name it '''while hyphen loop dot rb.''' |
|- | |- | ||
− | | 01 | + | | 01:09 |
− | |I have a working example of the ''' while | + | |I have a working example of the ''' 'while' loop'''. |
|- | |- | ||
− | | 01 | + | | 01:13 |
− | |Now let us switch to the terminal and type ''' gedit space while hyphen loop dot rb space &''' (ampersand) | + | |Now, let us switch to the '''terminal''' and type: ''' gedit space while hyphen loop dot rb space &''' (ampersand) |
|- | |- | ||
− | | 01 | + | | 01:24 |
|You can pause the tutorial, type the code as we go through it. | |You can pause the tutorial, type the code as we go through it. | ||
|- | |- | ||
− | | 01 | + | | 01:28 |
|I have declared a ''' while''' loop in this example. | |I have declared a ''' while''' loop in this example. | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:32 |
− | | First, I declared a local variable | + | | First, I declared a local variable 'i' and initialized it with value 0. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:38 |
− | |Then I declare a ''' while loop. ''' | + | |Then I declare a ''''while' loop.''' |
|- | |- | ||
− | | 01 | + | | 01:41 |
− | | This loop will execute as long as the variable | + | | This loop will execute as long as the variable 'i' is greater than -10. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:46 |
− | | The '''puts''' method | + | | The '''puts''' '''method''', declared within the ''' while''' loop, will display the output. |
|- | |- | ||
− | | 01 | + | | 01:51 |
− | | After the output is displayed, we decrement the value of i by 1. | + | | After the output is displayed, we decrement the value of 'i' by 1. |
|- | |- | ||
− | | 01 | + | | 01:56 |
− | | | + | |'i' will adopt this decremented value before the next iteration. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:01 |
− | |The variable | + | |The variable 'i' gets decremented in every iteration. |
|- | |- | ||
− | | 02 | + | | 02:04 |
− | |This goes on till | + | |This goes on till 'i' reaches the value -10. |
|- | |- | ||
− | | 02 | + | | 02:09 |
− | | At this point the '''while condition''' fails. | + | | At this point, the ''''while' condition''' fails. |
|- | |- | ||
− | | 02 | + | | 02:12 |
| It subsequently breaks out of the loop and stops printing the output. | | It subsequently breaks out of the loop and stops printing the output. | ||
|- | |- | ||
− | | 02 | + | | 02:16 |
− | | Now, let us switch to the '''terminal''' and type '''ruby space while hyphen loop dot rb''' and see the output. | + | | Now, let us switch to the '''terminal''' and type: '''ruby space while hyphen loop dot rb''' and see the output. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:30 |
|The output will consist of a list of numbers 0 through -9. | |The output will consist of a list of numbers 0 through -9. | ||
|- | |- | ||
− | | 02 | + | | 02:35 |
− | | You should now be able to write your own '''while''' loop in | + | | You should now be able to write your own '''while''' loop in Ruby. |
|- | |- | ||
− | | 02 | + | | 02:40 |
| Let's look at the '''until''' loop next. | | Let's look at the '''until''' loop next. | ||
|- | |- | ||
− | | 02 | + | | 02:43 |
|The syntax for the '''until''' loop in '''Ruby''' is - | |The syntax for the '''until''' loop in '''Ruby''' is - | ||
|- | |- | ||
− | | 02 | + | | 02:45 |
|''' until “boolean expression”''' | |''' until “boolean expression”''' | ||
|- | |- | ||
− | | 02 | + | | 02:47 |
− | |''' ruby code ''' | + | |''' ruby code ''', '''end''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:50 |
| Let us look at an example. | | Let us look at an example. | ||
|- | |- | ||
− | | 02 | + | | 02:52 |
− | | Now let us switch to | + | | Now,let us switch to the '''terminal''' and type: ''' gedit space until hyphen loop dot rb space &''' (ampersand) |
|- | |- | ||
− | | 03 | + | | 03:03 |
− | |You can pause the tutorial | + | |You can pause the tutorial and type the code as we go through it. |
|- | |- | ||
− | | 03 | + | | 03:07 |
| I have declared an ''' until''' loop in this example. | | I have declared an ''' until''' loop in this example. | ||
|- | |- | ||
− | | 03 | + | | 03:12 |
− | |We had declared a local variable | + | |We had declared a local variable 'i' and initialized it to 0. |
|- | |- | ||
− | | 03 | + | | 03:16 |
| Then we declare an ''' until''' loop. | | Then we declare an ''' until''' loop. | ||
|- | |- | ||
− | | 03 | + | | 03:18 |
− | |This loop will execute as long as the variable | + | |This loop will execute as long as the variable 'i' is greater than -10. |
|- | |- | ||
− | | 03 | + | | 03:23 |
− | |The '''puts''' | + | |The ''''puts' method''' will display the output. |
|- | |- | ||
− | | 03 | + | | 03:27 |
− | | After the output is displayed, value of | + | | After the output is displayed, value of 'i' is decremented by 1. |
|- | |- | ||
− | | 03 | + | | 03:32 |
− | | | + | |'i' will adopt this decremented value before the next iteration. |
|- | |- | ||
− | | 03 | + | | 03:36 |
− | | The variable | + | | The variable 'i' gets decremented during every iteration. |
|- | |- | ||
− | | 03 | + | | 03:40 |
− | | This goes on till | + | | This goes on till 'i' reaches the value -11. |
|- | |- | ||
− | | 03 | + | | 03:43 |
− | |At this point the '''until condition''' fails. | + | |At this point, the ''''until' condition''' fails. |
|- | |- | ||
− | | 03 | + | | 03:46 |
| Subsequently, it breaks out of the loop and stops printing the output. | | Subsequently, it breaks out of the loop and stops printing the output. | ||
|- | |- | ||
− | | 03 | + | | 03:51 |
− | |Now switch to the ''' terminal''' and type ''' ruby space until hyphen loop dot rb''' and see the output. | + | |Now, switch to the ''' terminal''' and type: ''' ruby space until hyphen loop dot rb''' and see the output. |
|- | |- | ||
− | | 04 | + | | 04:03 |
| The output will consist of a list of numbers 0 through -10. | | The output will consist of a list of numbers 0 through -10. | ||
|- | |- | ||
− | | 04 | + | | 04:08 |
|You should now be able to write your own '''until''' loop in '''Ruby.''' | |You should now be able to write your own '''until''' loop in '''Ruby.''' | ||
|- | |- | ||
− | | 04 | + | | 04:13 |
|Let's now move on to the '''redo''' construct. | |Let's now move on to the '''redo''' construct. | ||
|- | |- | ||
− | | 04 | + | | 04:16 |
− | | The syntax for '''redo''' in | + | | The syntax for '''redo''' in Ruby is as follows: |
|- | |- | ||
− | | 04 | + | | 04:20 |
− | |''' a collection of objects.each do item ''' | + | |''' a collection of objects. each do item''' |
|- | |- | ||
− | | 04 | + | | 04:25 |
− | | '''a conditional statement ''' | + | | '''a conditional statement''' |
|- | |- | ||
− | | 04 | + | | 04:27 |
− | |'''ruby code ''' | + | |'''ruby code ''', '''redo''' |
|- | |- | ||
− | | 04 | + | | 04:29 |
− | |''' | + | |'''end conditional''', '''end''' loop |
|- | |- | ||
− | + | | 04:32 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | | 04 | + | |
|I have a working example of the '''redo''' loop. | |I have a working example of the '''redo''' loop. | ||
|- | |- | ||
− | | 04 | + | | 04:35 |
− | |Now let us switch to the terminal and type '''gedit space redo hyphen loop dot rb space &'''(ampersand ) | + | |Now, let us switch to the '''terminal''' and type: '''gedit space redo hyphen loop dot rb space &'''(ampersand ) |
|- | |- | ||
− | | 04 | + | | 04:48 |
− | | You can pause the tutorial | + | | You can pause the tutorial and type the code as we go through it. |
|- | |- | ||
− | | 04 | + | | 04:52 |
|I have declared an '''each''' loop in this example. | |I have declared an '''each''' loop in this example. | ||
|- | |- | ||
− | | 04 | + | | 04:55 |
| We have declared an '''each''' loop to iterate through numbers 10 to 20. | | We have declared an '''each''' loop to iterate through numbers 10 to 20. | ||
|- | |- | ||
− | | 05 | + | | 05:00 |
− | | Then, we define an ''' if''' conditional statement. | + | | Then, we define an '''if''' conditional statement. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:04 |
| The loop will execute for every number between 10 to 20. | | The loop will execute for every number between 10 to 20. | ||
|- | |- | ||
− | | 05 | + | | 05:08 |
− | | It will enter the inner conditional ''' if''' conditional block only if the value of i is equal to 20. | + | | It will enter the inner conditional ''' if''' conditional block only if the value of 'i' is equal to 20. |
|- | |- | ||
− | | 05 | + | | 05:15 |
− | |The '''puts''' | + | |The ''''puts' method''', declared within the '''each''' loop, displays the output. |
|- | |- | ||
− | | 05 | + | | 05:20 |
− | | Once the program enters the | + | | Once the program enters the 'if' conditional block, it will first print the output. |
|- | |- | ||
− | | 05 | + | | 05:24 |
− | |Then it will execute '''redo. ''' | + | |Then it will execute '''redo.''' |
|- | |- | ||
− | | 05 | + | | 05:28 |
|'''redo''' will execute the iteration of the most internal loop. | |'''redo''' will execute the iteration of the most internal loop. | ||
|- | |- | ||
− | | 05 | + | | 05:31 |
− | |It will do so without checking the loop condition. | + | |It will do so, without checking the '''loop condition'''. |
|- | |- | ||
− | | 05 | + | | 05:34 |
− | |Our condition being ''' if i == 20 | + | |Our condition being '''if i == 20''', |
|- | |- | ||
− | | 05 | + | | 05:38 |
− | | | + | | the result will be an infinite loop, since the value of 'i' will not change from 20. |
|- | |- | ||
− | |05 | + | |05:43 |
− | |Let's switch to the '''terminal''' and type '''ruby space redo hyphen loop dot rb''' | + | |Let's switch to the '''terminal''' and type: '''ruby space redo hyphen loop dot rb''' |
|- | |- | ||
− | | 05 | + | | 05:52 |
− | |and see the output. | + | |and see the output. The output will consist of an '''infinite loop''' that never ends. |
|- | |- | ||
− | | 05 | + | | 05:58 |
− | | | + | |Press '''Ctrl + C''' to terminate the infinite loop. |
|- | |- | ||
− | + | | 06:03 | |
− | + | ||
− | + | ||
− | + | ||
− | | 06 | + | |
| Now, let us look at the '''break''' statement. | | Now, let us look at the '''break''' statement. | ||
|- | |- | ||
− | | 06 | + | | 06:06 |
− | |The syntax for the '''break''' statement in | + | |The syntax for the '''break''' statement in Ruby is - |
|- | |- | ||
− | | 06 | + | | 06:10 |
− | |'''a looping statement ''' | + | | '''a looping statement''' |
|- | |- | ||
− | | 06 | + | | 06:12 |
− | | | + | | '''a conditional statement ''', '''break ''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:14 |
|'''end conditional ''' | |'''end conditional ''' | ||
|- | |- | ||
− | | 06 | + | | 06:16 |
− | |'''ruby code''' | + | |'''ruby code''', '''end loop ''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
|- | |- | ||
− | | 06 | + | | 06:18 |
| Let us look at an example. | | Let us look at an example. | ||
|- | |- | ||
− | | 06 | + | | 06:21 |
− | |Now let us switch to the '''terminal''' and type '''gedit space break hyphen loop dot rb space ampersand.''' | + | |Now, let us switch to the '''terminal''' and type: '''gedit space break hyphen loop dot rb space ampersand.''' |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:33 |
− | |You can pause the tutorial | + | |You can pause the tutorial and type the code as we go through this example. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:38 |
|I have declared an '''each''' loop in this example. | |I have declared an '''each''' loop in this example. | ||
|- | |- | ||
− | | 06 | + | | 06:41 |
|It is similar to the one we used earlier. | |It is similar to the one we used earlier. | ||
|- | |- | ||
− | | 06 | + | | 06:43 |
− | |The '''puts''' | + | |The ''''puts' method''' here, will display the output for numbers 11 to 19. |
|- | |- | ||
− | | 06 | + | | 06:49 |
− | |Once the value becomes 20, the program enters the conditional | + | |Once the value becomes 20, the program enters the conditional 'if' block. |
|- | |- | ||
− | | 06 | + | | 06:54 |
− | |At this point, it will encounter the '''break''' statement and | + | |At this point, it will encounter the '''break''' statement and breaks out of the loop. |
|- | |- | ||
− | | 06 | + | | 06:59 |
− | |Now open the '''terminal''' and type | + | |Now, open the '''terminal''' and type: |
|- | |- | ||
− | | 07 | + | | 07:02 |
|'''ruby space break hyphen loop dot rb ''' | |'''ruby space break hyphen loop dot rb ''' | ||
|- | |- | ||
− | | 07 | + | | 07:05 |
|and see the output. | |and see the output. | ||
|- | |- | ||
− | | 07 | + | | 07:08 |
|The output will consist of numbers 10 through 19. | |The output will consist of numbers 10 through 19. | ||
|- | |- | ||
− | | 07 | + | | 07:13 |
− | |Now you should be able to create your own '''break''' construct. | + | |Now, you should be able to create your own '''break''' construct. |
|- | |- | ||
− | | 07 | + | | 07:17 |
|This brings us to the end of this Spoken Tutorial. | |This brings us to the end of this Spoken Tutorial. | ||
|- | |- | ||
− | | 07 | + | | 07:20 |
|Let's summarize. | |Let's summarize. | ||
|- | |- | ||
− | | 07 | + | | 07:22 |
− | |In this tutorial we have learnt to use | + | |In this tutorial, we have learnt to use: |
− | + | ||
|- | |- | ||
− | | 07 | + | | 07:24 |
− | |'''while''' loop | + | |'''while''' loop, '''until''' construct |
|- | |- | ||
− | | 07 | + | | 07:26 |
− | |''' | + | | '''redo''', '''break''' construct. |
|- | |- | ||
− | | 07 | + | | 07:29 |
− | | | + | |As an assignment- |
|- | |- | ||
− | | 07 | + | | 07:31 |
− | | | + | |Consider a range of numbers 100 to 115(inclusive), represented as Fahrenheit. |
|- | |- | ||
− | | 07 | + | | 07:38 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|Write a Ruby program using | |Write a Ruby program using | ||
|- | |- | ||
− | | 07 | + | | 07:40 |
|the appropriate loop construct | |the appropriate loop construct | ||
|- | |- | ||
− | | 07 | + | | 07:42 |
|that uses the Fahrenheit to Celsius conversion formula | |that uses the Fahrenheit to Celsius conversion formula | ||
|- | |- | ||
− | | 07 | + | | 07:46 |
− | |against the given range of numbers | + | |against the given range of numbers. |
|- | |- | ||
− | | 07 | + | | 07:49 |
− | | | + | |to display the output: ''' “The temperature has reached a certain degree Celsius and has become unbearable”''' |
+ | |||
|- | |- | ||
− | | 07 | + | | 07:55 |
− | |when the temperature in | + | |when the temperature in Celsius is above 32 degree Celsius. |
|- | |- | ||
− | | 08 | + | | 08:00 |
| Watch the video available at the following link. | | Watch the video available at the following link. | ||
|- | |- | ||
− | | 08 | + | | 08:03 |
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
− | | 08 | + | | 08:07 |
|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. | ||
|- | |- | ||
− | | 08 | + | | 08:10 |
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
− | | 08 | + | | 08:13 |
− | |Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
− | |08 | + | |08:15 |
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
− | | 08 | + | | 08:19 |
− | |For more details, please write to contact@spoken-tutorial.org | + | |For more details, please write to: '''contact@spoken-tutorial.org''' |
|- | |- | ||
− | | 08 | + | | 08:25 |
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
− | | 08 | + | | 08:29 |
|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. | ||
|- | |- | ||
− | | 08 | + | | 08:35 |
− | |More information on this | + | |More information on this mission is available at: '''spoken hyphen tutorial dot org slash NMEICT hyphen Intro'''. |
|- | |- | ||
− | | 08 | + | | 08:44 |
− | | This is Anjana Nair signing off. Thank you | + | | This is Anjana Nair, signing off. Thank you. |
|} | |} |
Latest revision as of 12:34, 10 March 2017
Time | Narration |
00:01 | Welcome to the tutorial on while and until loops in Ruby. |
00:06 | In this tutorial, we will learn to use- |
00:09 | 'while' loop , 'until' loop |
00:11 | redo and break. |
00:13 | We are using: Ubuntu version 12.04 |
00:17 | Ruby 1.9.3 |
00:20 | To follow this tutorial, you must have Internet connection. |
00:25 | You must also have knowledge of Linux commands, Terminal and Text-editor. |
00:29 | 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 and looping hyphen statements directories. |
00:46 | Now that we are in that folder, let’s move ahead. |
00:50 | The syntax of the while loop in Ruby is as follows: |
00:54 | while “boolean expression” |
00:56 | ruby code , end |
00:58 | Let us look at an example. |
01:01 | Create a new file in gedit as shown in the basic level Ruby tutorials. |
01:05 | Name it while hyphen loop dot rb. |
01:09 | I have a working example of the 'while' loop. |
01:13 | Now, let us switch to the terminal and type: gedit space while hyphen loop dot rb space & (ampersand) |
01:24 | You can pause the tutorial, type the code as we go through it. |
01:28 | I have declared a while loop in this example. |
01:32 | First, I declared a local variable 'i' and initialized it with value 0. |
01:38 | Then I declare a 'while' loop. |
01:41 | This loop will execute as long as the variable 'i' is greater than -10. |
01:46 | The puts method, declared within the while loop, will display the output. |
01:51 | After the output is displayed, we decrement the value of 'i' by 1. |
01:56 | 'i' will adopt this decremented value before the next iteration. |
02:01 | The variable 'i' gets decremented in every iteration. |
02:04 | This goes on till 'i' reaches the value -10. |
02:09 | At this point, the 'while' condition fails. |
02:12 | It subsequently breaks out of the loop and stops printing the output. |
02:16 | Now, let us switch to the terminal and type: ruby space while hyphen loop dot rb and see the output. |
02:30 | The output will consist of a list of numbers 0 through -9. |
02:35 | You should now be able to write your own while loop in Ruby. |
02:40 | Let's look at the until loop next. |
02:43 | The syntax for the until loop in Ruby is - |
02:45 | until “boolean expression” |
02:47 | ruby code , end |
02:50 | Let us look at an example. |
02:52 | Now,let us switch to the terminal and type: gedit space until hyphen loop dot rb space & (ampersand) |
03:03 | You can pause the tutorial and type the code as we go through it. |
03:07 | I have declared an until loop in this example. |
03:12 | We had declared a local variable 'i' and initialized it to 0. |
03:16 | Then we declare an until loop. |
03:18 | This loop will execute as long as the variable 'i' is greater than -10. |
03:23 | The 'puts' method will display the output. |
03:27 | After the output is displayed, value of 'i' is decremented by 1. |
03:32 | 'i' will adopt this decremented value before the next iteration. |
03:36 | The variable 'i' gets decremented during every iteration. |
03:40 | This goes on till 'i' reaches the value -11. |
03:43 | At this point, the 'until' condition fails. |
03:46 | Subsequently, it breaks out of the loop and stops printing the output. |
03:51 | Now, switch to the terminal and type: ruby space until hyphen loop dot rb and see the output. |
04:03 | The output will consist of a list of numbers 0 through -10. |
04:08 | You should now be able to write your own until loop in Ruby. |
04:13 | Let's now move on to the redo construct. |
04:16 | The syntax for redo in Ruby is as follows: |
04:20 | a collection of objects. each do item |
04:25 | a conditional statement |
04:27 | ruby code , redo |
04:29 | end conditional, end loop |
04:32 | I have a working example of the redo loop. |
04:35 | Now, let us switch to the terminal and type: gedit space redo hyphen loop dot rb space &(ampersand ) |
04:48 | You can pause the tutorial and type the code as we go through it. |
04:52 | I have declared an each loop in this example. |
04:55 | We have declared an each loop to iterate through numbers 10 to 20. |
05:00 | Then, we define an if conditional statement. |
05:04 | The loop will execute for every number between 10 to 20. |
05:08 | It will enter the inner conditional if conditional block only if the value of 'i' is equal to 20. |
05:15 | The 'puts' method, declared within the each loop, displays the output. |
05:20 | Once the program enters the 'if' conditional block, it will first print the output. |
05:24 | Then it will execute redo. |
05:28 | redo will execute the iteration of the most internal loop. |
05:31 | It will do so, without checking the loop condition. |
05:34 | Our condition being if i == 20, |
05:38 | the result will be an infinite loop, since the value of 'i' will not change from 20. |
05:43 | Let's switch to the terminal and type: ruby space redo hyphen loop dot rb |
05:52 | and see the output. The output will consist of an infinite loop that never ends. |
05:58 | Press Ctrl + C to terminate the infinite loop. |
06:03 | Now, let us look at the break statement. |
06:06 | The syntax for the break statement in Ruby is - |
06:10 | a looping statement |
06:12 | a conditional statement , break |
06:14 | end conditional |
06:16 | ruby code, end loop |
06:18 | Let us look at an example. |
06:21 | Now, let us switch to the terminal and type: gedit space break hyphen loop dot rb space ampersand. |
06:33 | You can pause the tutorial and type the code as we go through this example. |
06:38 | I have declared an each loop in this example. |
06:41 | It is similar to the one we used earlier. |
06:43 | The 'puts' method here, will display the output for numbers 11 to 19. |
06:49 | Once the value becomes 20, the program enters the conditional 'if' block. |
06:54 | At this point, it will encounter the break statement and breaks out of the loop. |
06:59 | Now, open the terminal and type: |
07:02 | ruby space break hyphen loop dot rb |
07:05 | and see the output. |
07:08 | The output will consist of numbers 10 through 19. |
07:13 | Now, you should be able to create your own break construct. |
07:17 | This brings us to the end of this Spoken Tutorial. |
07:20 | Let's summarize. |
07:22 | In this tutorial, we have learnt to use: |
07:24 | while loop, until construct |
07:26 | redo, break construct. |
07:29 | As an assignment- |
07:31 | Consider a range of numbers 100 to 115(inclusive), represented as Fahrenheit. |
07:38 | Write a Ruby program using |
07:40 | the appropriate loop construct |
07:42 | that uses the Fahrenheit to Celsius conversion formula |
07:46 | against the given range of numbers. |
07:49 | to display the output: “The temperature has reached a certain degree Celsius and has become unbearable” |
07:55 | when the temperature in Celsius is above 32 degree Celsius. |
08:00 | Watch the video available at the following link. |
08:03 | It summarizes the Spoken Tutorial project. |
08:07 | If you do not have good bandwidth, you can download and watch it. |
08:10 | The Spoken Tutorial project team: |
08:13 | Conducts workshops using spoken tutorials. |
08:15 | Gives certificates to those who pass an online test. |
08:19 | For more details, please write to: contact@spoken-tutorial.org |
08:25 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
08:29 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
08:35 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
08:44 | This is Anjana Nair, signing off. Thank you. |