Difference between revisions of "Java/C2/while-loop/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 7: | Line 7: | ||
|- | |- | ||
| 00:02 | | 00:02 | ||
− | | Welcome to the spoken tutorial on''' While Loop '''in java | + | | Welcome to the spoken tutorial on '''While Loop''' in java. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial, you will learn | + | | In this tutorial, you will learn: * about the '''while loop''' |
− | + | * how to use it. | |
− | + | ||
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | For this tutorial we are using | + | | For this tutorial, we are using: |
− | + | ||
'''Ubuntu 11.10''', | '''Ubuntu 11.10''', | ||
− | |||
'''JDK 1.6''' and | '''JDK 1.6''' and | ||
− | |||
'''Eclipse 3.7''' | '''Eclipse 3.7''' | ||
|- | |- | ||
| 00:21 | | 00:21 | ||
− | | To follow this tutorial, you must have knowledge of '''relational operators in Java''' | + | | To follow this tutorial, you must have knowledge of '''relational operators in Java'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:26 | | 00:26 | ||
− | | If not, for relevant tutorials please visit our website as shown. [http://spoken-tutorial. | + | | If not, for relevant tutorials, please visit our website as shown. [http://spoken-tutorial.org] |
− | + | ||
|- | |- | ||
| 00:36 | | 00:36 | ||
− | | Here is the structure of a while loop. | + | | Here is the '''structure''' of a '''while loop'''. |
|- | |- | ||
Line 47: | Line 40: | ||
| 00:41 | | 00:41 | ||
| One is the '''loop running condition''' and the second is the '''loop variable.''' | | One is the '''loop running condition''' and the second is the '''loop variable.''' | ||
− | |||
|- | |- | ||
| 00:48 | | 00:48 | ||
− | | Let us now | + | | Let us now look at an example. Switch to '''Eclipse'''. |
|- | |- | ||
| 00:55 | | 00:55 | ||
− | | | + | | Here, we have the '''eclipse IDE''' and the skeleton required for rest of the code. |
− | + | ||
|- | |- | ||
| 01:00 | | 01:00 | ||
− | | | + | | I have created a '''class WhileDemo''' and added the '''main method''' to it. |
|- | |- | ||
| 01:05 | | 01:05 | ||
− | | | We shall print numbers from 1 to 10 using a while loop | + | | | We shall print numbers from '''1''' to '''10''' using a '''while loop'''. Type: '''int n = 1'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 01:15 | | 01:15 | ||
− | | This variable n is going to be our loop variable | + | | This variable 'n' is going to be our '''loop variable'''. |
|- | |- | ||
| 01:21 | | 01:21 | ||
− | | | + | | Type: '''while''' in parenthesis '''n''' less than or equal to '''10''' open and close braces. |
|- | |- | ||
| 01:33 | | 01:33 | ||
− | | This | + | | This condition is called '''looping running condition'''. |
− | + | ||
|- | |- | ||
| 01:37 | | 01:37 | ||
− | | It means the loop will run as long as this condition is true. | + | | It means the '''loop''' will '''run''' as long as this condition is true. |
− | + | ||
|- | |- | ||
| 01:42 | | 01:42 | ||
− | | In our case, it will run as long as the value of | + | | In our case, it will run as long as the value of 'n' is less than or equal to 10. |
− | + | ||
|- | |- | ||
| 01:47 | | 01:47 | ||
− | | And it will stop only when the value of | + | | And it will stop only when the value of 'n' becomes greater than 10. |
|- | |- | ||
| 01:53 | | 01:53 | ||
− | | | + | | Inside the '''loop''', we shall print the value of 'n'. |
|- | |- | ||
| 01:58 | | 01:58 | ||
− | | | + | | '''System.out.println(n);''' and then increment '''n = n + 1;''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 02:12 | | 02:12 | ||
− | | This way, first 1 is printed and then the value of n becomes 2. | + | | This way, first 1 is printed and then the value of 'n' becomes 2. |
|- | |- | ||
| 02:18 | | 02:18 | ||
− | | Then the loop condition is checked. | + | | Then the '''loop condition''' is checked. |
− | + | ||
|- | |- | ||
| 02:21 | | 02:21 | ||
− | | Since it is true | + | | Since it is true, 2 is printed and n becomes 3. |
|- | |- | ||
| 02:25 | | 02:25 | ||
− | | And so on loop progresses | + | | And so on.. loop progresses until 10 is printed. After that 'n' becomes 11 and the condition is not true and the '''loop''' will stop. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 02:37 | | 02:37 | ||
− | | So | + | | So, let us see the code in action. |
|- | |- | ||
| 02:39 | | 02:39 | ||
− | | Save and run. | + | | '''Save''' and '''run'''. |
|- | |- | ||
| 02:47 | | 02:47 | ||
| As we can see, the numbers from 1 to 10 are printed. | | As we can see, the numbers from 1 to 10 are printed. | ||
− | |||
|- | |- | ||
| 02:52 | | 02:52 | ||
− | | Now we shall print numbers from '''50''' to '''40''' | + | | Now we shall print numbers from '''50''' to '''40'''. |
|- | |- | ||
| 02:58 | | 02:58 | ||
− | | So | + | | So, we start with 50. Change '''n = 1''' to '''n = 50'''. |
|- | |- | ||
| 03:03 | | 03:03 | ||
| And We go till 40. | | And We go till 40. | ||
− | |||
|- | |- | ||
| 03:05 | | 03:05 | ||
− | | In other words as long as n | + | | In other words, as long as 'n' is greater than or equal to 40. So, change the condition to n greater than or equal to 40. |
− | + | ||
− | + | ||
|- | |- | ||
| 03:16 | | 03:16 | ||
− | | And | + | | And since we are looping from a bigger number to a smaller number, we have decremented the loop variable. |
− | + | ||
|- | |- | ||
| 03:22 | | 03:22 | ||
− | | | + | | So Change '''n=n+1''' to '''n=n-1'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 03:27 | | 03:27 | ||
− | | | + | | '''Save''' and '''run'''. As we can see, the numbers 50 to 40 have been printed. |
|- | |- | ||
Line 179: | Line 151: | ||
|- | |- | ||
| 03:48 | | 03:48 | ||
− | | To do that, we start with 7 | + | | To do that, we start with 7. |
− | + | ||
|- | |- | ||
| 03:50 | | 03:50 | ||
− | | So change '''n = 50''' to '''n = 7'''and then end with 70. | + | | So, change '''n = 50''' to '''n = 7''' and then end with 70. |
− | + | ||
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | Change the condition to | + | | Change the condition to '''n''' less than equal to '''70'''. |
− | + | ||
|- | |- | ||
| 04:03 | | 04:03 | ||
− | | This way, we make sure the loop stops at 70. | + | | This way, we make sure the '''loop''' stops at 70. |
|- | |- | ||
| 04:07 | | 04:07 | ||
− | | | + | | To get the multiples, we shall increment the '''loop variable''' by 7. |
− | + | ||
|- | |- | ||
| 04:12 | | 04:12 | ||
− | | So, change ''' n=n - 1''' to '''n=n + 7''' | + | |So, change '''n=n-1''' to '''n= n+7'''. |
− | + | ||
|- | |- | ||
| 04:18 | | 04:18 | ||
− | | This way first 7 is printed and then n becomes 14, 14 is printed and so on | + | | This way, first 7 is printed and then 'n' becomes 14, 14 is printed and so on until 70. '''Save''' and '''run'''. |
|- | |- | ||
| 04:33 | | 04:33 | ||
− | | | As we can see, | + | | | As we can see, the first 10 multiples of 7 are printed. |
− | + | ||
|- | |- | ||
| 04:43 | | 04:43 | ||
− | | We can also use a while loop to get the sum of digits of a number. | + | | We can also use a '''while loop''' to get the sum of digits of a number. |
− | + | ||
|- | |- | ||
Line 226: | Line 191: | ||
|- | |- | ||
| 04:49 | | 04:49 | ||
− | | First clear the main method. | + | | First, clear the '''main method'''. |
− | + | |- | |
− | + | ||
| 04:54 | | 04:54 | ||
− | | | + | | '''int n''' equal to '''13876'''. This is the number. |
|- | |- | ||
| 05:02 | | 05:02 | ||
− | | | + | | Then '''int dSum''' equal to '''0''' The variable '''dsum''' with symbolic for digit sum will contain the sum of digits. |
− | + | ||
− | + | ||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Type while | + | | Type: '''while n''' greater than '''0''' open close parenthesis. |
− | + | ||
|- | |- | ||
Line 253: | Line 214: | ||
|- | |- | ||
| 05:36 | | 05:36 | ||
− | | To do that we use modulo operator. | + | | To do that, we use '''modulo operator'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:40 | | 05:40 | ||
− | | Type '''dSum = dSum + (n % 10)''' So we get the unit digit and add it to dsum | + | | Type: '''dSum = dSum + (n % 10)''' So, we get the unit digit and add it to '''dsum'''. |
|- | |- | ||
| 05:52 | | 05:52 | ||
− | |After that we remove the digit by dividing | + | |After that we remove the digit by dividing by 10. n = n / 10 |
|- | |- | ||
| 06:08 | | 06:08 | ||
− | | | + | | So when the '''loop''' is '''run''' for the first time, '''dSum''' will be 6 and 'n' will be 1387. |
|- | |- | ||
| 06:15 | | 06:15 | ||
− | | | + | | And when the loop is '''run''' for the second time, '''dSum''' will be sum of 7 and 6 which is 13 and 'n' will become 138. |
− | + | ||
|- | |- | ||
| 06:22 | | 06:22 | ||
− | | So on | + | | So on as the loop progresses, the digits will be removed from '''n''' and finally |
|- | |- | ||
| 06:28 | | 06:28 | ||
− | | | + | | '''n''' becomes zero. After that the condition 'n greater than 0' will be false and the loop will stop. |
|- | |- | ||
| 06:36 | | 06:36 | ||
− | | | + | | So, let us now add a print statement: |
|- | |- | ||
| 06:42 | | 06:42 | ||
− | | | + | | '''System.out.println(dSum)''' |
|- | |- | ||
| 06:51 | | 06:51 | ||
− | | Let us see the code and action. Save and run | + | | Let us see the code and action. '''Save''' and '''run'''. |
|- | |- | ||
| 06:59 | | 06:59 | ||
− | | | + | | As we can see, the sum of digit which is 25 has been printed. |
|- | |- | ||
| 07:06 | | 07:06 | ||
− | | This way, a while loop | + | | This way, a '''while loop''' which is one of the most fundamental '''constructs''' in programming, can be used. |
|- | |- | ||
| 07:16 | | 07:16 | ||
− | | This brings | + | | This brings us to the end of the tutorial. |
− | + | ||
|- | |- | ||
| 07:20 | | 07:20 | ||
− | | In this tutorial we have learnt | + | | In this tutorial, we have learnt: |
− | + | ||
* About while loop | * About while loop | ||
− | * | + | * How to use it. |
− | + | ||
− | + | ||
|- | |- | ||
|07:26 | |07:26 | ||
− | | As an assignment for this tutorial solve the following problem. | + | | As an assignment for this tutorial, solve the following problem. |
− | + | ||
|- | |- | ||
Line 326: | Line 278: | ||
|- | |- | ||
|07:37 | |07:37 | ||
− | |To know more about the | + | |To know more about the Spoken Tutorial project, watch the video available at the following link, that summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 334: | Line 286: | ||
|- | |- | ||
| 07:50 | | 07:50 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. Gives certificates for those who pass an online test. |
|- | |- | ||
Line 342: | Line 294: | ||
|- | |- | ||
| 08:03 | | 08:03 | ||
− | | | + | | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
+ | |||
|- | |- | ||
| 08:07 | | 08:07 | ||
− | | It is supported by the | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
+ | |||
|- | |- | ||
| 08:12 | | 08:12 | ||
− | | | + | | More information on this mission is available at the following link. |
|- | |- | ||
| 08:17 | | 08:17 | ||
| This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | | This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | ||
− | |||
− | |||
− | |||
|} | |} |
Revision as of 23:17, 25 March 2015
Time | Narration |
00:02 | Welcome to the spoken tutorial on While Loop in java. |
00:06 | In this tutorial, you will learn: * about the while loop
|
00:12 | For this tutorial, we are using:
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7 |
00:21 | To follow this tutorial, you must have knowledge of relational operators in Java. |
00:26 | If not, for relevant tutorials, please visit our website as shown. [1] |
00:36 | Here is the structure of a while loop. |
00:39 | It has two parts. |
00:41 | One is the loop running condition and the second is the loop variable. |
00:48 | Let us now look at an example. Switch to Eclipse. |
00:55 | Here, we have the eclipse IDE and the skeleton required for rest of the code. |
01:00 | I have created a class WhileDemo and added the main method to it. |
01:05 | We shall print numbers from 1 to 10 using a while loop. Type: int n = 1. |
01:15 | This variable 'n' is going to be our loop variable. |
01:21 | Type: while in parenthesis n less than or equal to 10 open and close braces. |
01:33 | This condition is called looping running condition. |
01:37 | It means the loop will run as long as this condition is true. |
01:42 | In our case, it will run as long as the value of 'n' is less than or equal to 10. |
01:47 | And it will stop only when the value of 'n' becomes greater than 10. |
01:53 | Inside the loop, we shall print the value of 'n'. |
01:58 | System.out.println(n); and then increment n = n + 1; |
02:12 | This way, first 1 is printed and then the value of 'n' becomes 2. |
02:18 | Then the loop condition is checked. |
02:21 | Since it is true, 2 is printed and n becomes 3. |
02:25 | And so on.. loop progresses until 10 is printed. After that 'n' becomes 11 and the condition is not true and the loop will stop. |
02:37 | So, let us see the code in action. |
02:39 | Save and run. |
02:47 | As we can see, the numbers from 1 to 10 are printed. |
02:52 | Now we shall print numbers from 50 to 40. |
02:58 | So, we start with 50. Change n = 1 to n = 50. |
03:03 | And We go till 40. |
03:05 | In other words, as long as 'n' is greater than or equal to 40. So, change the condition to n greater than or equal to 40. |
03:16 | And since we are looping from a bigger number to a smaller number, we have decremented the loop variable. |
03:22 | So Change n=n+1 to n=n-1. |
03:27 | Save and run. As we can see, the numbers 50 to 40 have been printed. |
03:42 | Now we shall print the first 10 multiples of 7. |
03:48 | To do that, we start with 7. |
03:50 | So, change n = 50 to n = 7 and then end with 70. |
03:57 | Change the condition to n less than equal to 70. |
04:03 | This way, we make sure the loop stops at 70. |
04:07 | To get the multiples, we shall increment the loop variable by 7. |
04:12 | So, change n=n-1 to n= n+7. |
04:18 | This way, first 7 is printed and then 'n' becomes 14, 14 is printed and so on until 70. Save and run. |
04:33 | As we can see, the first 10 multiples of 7 are printed. |
04:43 | We can also use a while loop to get the sum of digits of a number. |
04:47 | Let us see how to do so. |
04:49 | First, clear the main method. |
04:54 | int n equal to 13876. This is the number. |
05:02 | Then int dSum equal to 0 The variable dsum with symbolic for digit sum will contain the sum of digits. |
05:18 | Type: while n greater than 0 open close parenthesis. |
05:27 | The reason for using this condition will be evident in a while. |
05:32 | To get the sum of digits, we must first get the digits. |
05:36 | To do that, we use modulo operator. |
05:40 | Type: dSum = dSum + (n % 10) So, we get the unit digit and add it to dsum. |
05:52 | After that we remove the digit by dividing by 10. n = n / 10 |
06:08 | So when the loop is run for the first time, dSum will be 6 and 'n' will be 1387. |
06:15 | And when the loop is run for the second time, dSum will be sum of 7 and 6 which is 13 and 'n' will become 138. |
06:22 | So on as the loop progresses, the digits will be removed from n and finally |
06:28 | n becomes zero. After that the condition 'n greater than 0' will be false and the loop will stop. |
06:36 | So, let us now add a print statement: |
06:42 | System.out.println(dSum) |
06:51 | Let us see the code and action. Save and run. |
06:59 | As we can see, the sum of digit which is 25 has been printed. |
07:06 | This way, a while loop which is one of the most fundamental constructs in programming, can be used. |
07:16 | This brings us to the end of the tutorial. |
07:20 | In this tutorial, we have learnt:
|
07:26 | As an assignment for this tutorial, solve the following problem. |
07:29 | Given a number, compute its reverse by using a while loop. Example: 19435 => 53491 |
07:37 | To know more about the Spoken Tutorial project, watch the video available at the following link, that summarizes the Spoken Tutorial project. |
07:45 | If you do not have good bandwidth, you can download and watch it. |
07:50 | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. Gives certificates for those who pass an online test. |
07:57 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
08:03 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
08:07 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
08:12 | More information on this mission is available at the following link. |
08:17 | This tutorial has been contributed by TalentSprint. Thanks for joining. |