Difference between revisions of "Java/C2/do-while/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to the spoken tutorial on''' do-while Loop '''in java | + | | Welcome to the spoken tutorial on '''do-while Loop''' in java. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | | In this tutorial, you will learn about | + | | | In this tutorial, you will learn about: |
− | + | * '''do-while loop''' and | |
− | * '' | + | |
* '''how to use it''' | * '''how to use it''' | ||
− | |||
− | |||
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | | + | | For this tutorial, we are using: |
− | + | *'''Ubuntu 11.10''' | |
− | '''Ubuntu 11.10''' | + | *'''JDK 1.6''' and |
− | + | *'''Eclipse 3.7''' | |
− | '''JDK 1.6''' and | + | |
− | + | ||
− | '''Eclipse 3.7''' | + | |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | | + | | To follow this tutorial, you must have knowledge of '''while loop''' in java. |
− | + | ||
|- | |- | ||
| 00:25 | | 00:25 | ||
− | |If not, for relevant tutorials please visit our website as shown. | + | |If not, for relevant tutorials, please visit our website as shown. |
− | + | ||
|- | |- | ||
| 00:32 | | 00:32 | ||
− | | | + | | Here is the '''structure''' for '''do-while loop'''. |
− | + | ||
|- | |- | ||
| 00:37 | | 00:37 | ||
− | |Notice that it is similar to a while loop. | + | |Notice that it is similar to a '''while loop'''. |
− | + | ||
|- | |- | ||
| 00:40 | | 00:40 | ||
|It has two parts. | |It has two parts. | ||
− | |||
|- | |- | ||
| 00:42 | | 00:42 | ||
− | |First is the loop running condition. And the second is the loop variable | + | |First is the '''loop''' running '''condition'''. And the second is the '''loop variable'''. |
|- | |- | ||
| 00:51 | | 00:51 | ||
| The only difference is that the condition is written after the '''do''' block. | | The only difference is that the condition is written after the '''do''' block. | ||
− | |||
|- | |- | ||
| 00:58 | | 00:58 | ||
− | |And so the condition is checked after the execution of the statements inside the do block | + | |And so the condition is checked after the execution of the statements inside the '''do''' block. |
− | + | ||
|- | |- | ||
| 01:05 | | 01:05 | ||
− | |Now let us see an example. | + | |Now, let us see an example. |
|- | |- | ||
| 01:07 | | 01:07 | ||
− | | Switch to eclipse | + | | Switch to '''eclipse'''. |
|- | |- | ||
| 01:11 | | 01:11 | ||
− | | | + | | Here we have '''Eclipse IDE''' and the skeleton required for the rest of the code. |
− | + | ||
|- | |- | ||
| 01:17 | | 01:17 | ||
− | |We have created a class '''DoWhileDemo''' and added the main method to it. | + | |We have created a '''class''' '''DoWhileDemo''' and added the '''main method''' to it. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 01:22 | | 01:22 | ||
− | | We are going to print the numbers from 1 to 10 using a do-while loop. | + | | We are going to print the numbers from 1 to 10 using a '''do-while''' loop. |
− | + | ||
|- | |- | ||
| 01:27 | | 01:27 | ||
− | |Type | + | |Type: |
− | + | ||
|- | |- | ||
| 01:29 | | 01:29 | ||
− | |'''int n '' | + | |'''int n''' equal to '''1'''. |
− | + | ||
|- | |- | ||
| 01:32 | | 01:32 | ||
− | |n is going to be | + | |'n' is going to be '''loop variable'''. |
|- | |- | ||
| 01:36 | | 01:36 | ||
− | | | + | | Then type: '''do ''' |
|- | |- | ||
| 01:40 | | 01:40 | ||
− | | | + | |open and close braces |
|- | |- | ||
| 01:44 | | 01:44 | ||
− | |Inside the | + | |Inside the braces '''System.out.println(n);''' |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | |We shall print the value of n and then increment it. '''n '' | + | |We shall print the value of 'n' and then increment it. '''n''' equal to '''n''' plus '''1;''' |
|- | |- | ||
| 02:05 | | 02:05 | ||
| And we do this as long as | | And we do this as long as | ||
− | |||
|- | |- | ||
| 02:08 | | 02:08 | ||
− | |'''n | + | |'''n''' is less than or equal to '''10'''. |
− | + | ||
|- | |- | ||
| 02:10 | | 02:10 | ||
− | | Outside the braces | + | | Outside the braces, type: '''while''' in parentheses ('''n''' less than equal to '''10''') |
|- | |- | ||
| 02:20 | | 02:20 | ||
− | | | + | | and close the '''do-while''' using a semi-colon. |
|- | |- | ||
| 02:25 | | 02:25 | ||
| Let us see the code in action. | | Let us see the code in action. | ||
− | |||
|- | |- | ||
| 02:28 | | 02:28 | ||
− | |Save and Run. | + | |'''Save''' and '''Run'''. |
|- | |- | ||
| 02:37 | | 02:37 | ||
− | | | We see that | + | | | We see that the numbers from 1 to 10 are printed. |
|- | |- | ||
Line 156: | Line 133: | ||
|- | |- | ||
| 02:47 | | 02:47 | ||
− | | First, the value 1 is printed and then n becomes 2. | + | | First, the value 1 is printed and then 'n' becomes 2. |
− | + | ||
|- | |- | ||
| 02:52 | | 02:52 | ||
|And then, the condition is checked. | |And then, the condition is checked. | ||
− | |||
|- | |- | ||
| 02:55 | | 02:55 | ||
− | |since it is true, again 2 is printed and n becomes 3. | + | |since it is true, again 2 is printed and 'n' becomes 3. |
− | + | ||
|- | |- | ||
| 03:00 | | 03:00 | ||
− | |And so on till all the 10 numbers are printed and the value of n becomes 11. | + | |And so on till all the 10 numbers are printed and the value of 'n' becomes 11. |
− | + | ||
|- | |- | ||
| 03:06 | | 03:06 | ||
|When n = 11, the condition fails and the loop stops. | |When n = 11, the condition fails and the loop stops. | ||
− | |||
|- | |- | ||
| 03:11 | | 03:11 | ||
− | | Now | + | | Now, let us print numbers from 50 to 40 in decreasing order. |
|- | |- | ||
| 03:17 | | 03:17 | ||
− | | | + | | So, we start with 50. |
|- | |- | ||
Line 193: | Line 165: | ||
|- | |- | ||
| 03:23 | | 03:23 | ||
− | | Since we are looping from a bigger number to a smaller number, we decrement the loop variable. | + | | Since we are looping from a bigger number to a smaller number, we decrement the '''loop variable'''. |
− | + | ||
|- | |- | ||
| 03:29 | | 03:29 | ||
− | |So Change '''n = n + 1''' to '''n = n - 1''' | + | |So Change '''n = n + 1''' to '''n = n - 1'''. |
|- | |- | ||
| 03:34 | | 03:34 | ||
− | | We loop as long as n is greater than or equal to 40. | + | | We '''loop''' as long as 'n' is greater than or equal to 40. |
− | + | ||
|- | |- | ||
| 03:40 | | 03:40 | ||
− | | So | + | | So, change the condition to '''n >= 40'''. |
|- | |- | ||
Line 215: | Line 185: | ||
|- | |- | ||
| 03:50 | | 03:50 | ||
− | |Save and Run. | + | |'''Save''' and '''Run'''. |
|- | |- | ||
Line 223: | Line 193: | ||
|- | |- | ||
| 04:02 | | 04:02 | ||
− | | Now let us try a different logic using the do-while loop. | + | | Now let us try a different logic using the '''do-while''' loop. |
|- | |- | ||
Line 231: | Line 201: | ||
|- | |- | ||
| 04:15 | | 04:15 | ||
− | | |First | + | | |First clear the '''main method'''. |
|- | |- | ||
| 04:19 | | 04:19 | ||
− | | | + | |Then, type '''int n = 25;''' |
|- | |- | ||
| 04:25 | | 04:25 | ||
− | | | + | | We shall see if the value in 'n' is a perfect square or not. |
|- | |- | ||
| 04:32 | | 04:32 | ||
− | | | + | | Then type '''int x = 0;''' |
− | + | ||
|- | |- | ||
| 04:37 | | 04:37 | ||
− | |We shall use x to store the square root of the number if it | + | |We shall use 'x' to store the square root of the number if it is a perfect square. |
|- | |- | ||
| 04:44 | | 04:44 | ||
− | | | + | | Then type '''do''' |
− | + | ||
|- | |- | ||
| 04:46 | | 04:46 | ||
− | |Open and | + | |Open and close braces. |
|- | |- | ||
| 04:49 | | 04:49 | ||
− | |Inside the braces'''x | + | |Inside the braces, '''x''' equal to '''x ''' plus ''' 1''' |
|- | |- | ||
| 04:55 | | 04:55 | ||
− | | | + | | and outside the braces |
− | + | ||
|- | |- | ||
| 04:58 | | 04:58 | ||
− | |'''while in paranthesis | + | |'''while''' in paranthesis '''(x into x < n)''' |
− | + | ||
|- | |- | ||
| 05:06 | | 05:06 | ||
− | |And | + | |And close the '''do-while''' using a semi-colon. |
|- | |- | ||
|05:10 | |05:10 | ||
− | | | As long | + | | | As long as '''x''' into '''x''' is less than '''n''', we keep incrementing the value of 'x'. |
|- | |- | ||
| 05:16 | | 05:16 | ||
− | | So when the loop stops, the reverse of this condition will be true. | + | | So, when the loop stops, the reverse of this condition will be true. |
|- | |- | ||
| 05:22 | | 05:22 | ||
− | | Which means either x into x must be equal to n | + | | Which means, either 'x into x' must be equal to n |
− | + | ||
|- | |- | ||
| 05:26 | | 05:26 | ||
− | | | + | |or it must be greater than 'n'. |
|- | |- | ||
| 05:28 | | 05:28 | ||
− | | If '''x | + | | If '''x''' into '''x''' is equal to '''n''', the number is a perfect square. |
− | + | ||
|- | |- | ||
| 05:32 | | 05:32 | ||
− | |If it is not equal to n, the number is not a perfect square. | + | |If it is not equal to 'n', the number is not a perfect square. |
|- | |- | ||
Line 317: | Line 281: | ||
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | | + | | '''Save''' and '''Run'''. As we can see, the output is '''true'''. |
− | + | ||
|- | |- | ||
| 06:07 | | 06:07 | ||
− | |Let us try | + | |Let us try with other perfect square. |
|- | |- | ||
| 06:10 | | 06:10 | ||
− | | | + | | Change n = 25 to n = 49. |
|- | |- | ||
| 06:15 | | 06:15 | ||
− | | | + | | '''Save''' and '''Run'''. |
|- | |- | ||
| 06:20 | | 06:20 | ||
− | |We | + | |We see that we get the '''true''' again . |
|- | |- | ||
Line 342: | Line 305: | ||
|- | |- | ||
| 06:26 | | 06:26 | ||
− | | Change 49 to 23. Save and Run and | + | | Change 49 to 23. '''Save''' and '''Run''' and |
− | + | ||
|- | |- | ||
| 06:34 | | 06:34 | ||
− | | | + | |we get a '''false''' as expected. |
− | + | ||
|- | |- | ||
| 06:37 | | 06:37 | ||
− | |Now let us see what happens when the value of | + | |Now, let us see what happens when the value of 'n' is 0. |
|- | |- | ||
| 06:42 | | 06:42 | ||
− | | | + | | Change n = 23 to n = 0. Since 0 is not a natural number, we must get a '''false'''. |
− | + | ||
|- | |- | ||
| 06:52 | | 06:52 | ||
− | |Let us run the code. | + | |Let us '''run''' the code. |
|- | |- | ||
| 06:54 | | 06:54 | ||
− | |Save and Run. | + | |'''Save''' and '''Run'''. |
|- | |- | ||
| 07:00 | | 07:00 | ||
− | | | + | | We see that we get '''false''' as expected. |
|- | |- | ||
| 07:05 | | 07:05 | ||
− | | | + | | This happens because even before the condition |
|- | |- | ||
| 07:08 | | 07:08 | ||
− | |'''x | + | |'''x''' into '''x''' is less than '''n ''' is checked, the value of 'x' is incremented and it becomes 1. |
− | + | ||
|- | |- | ||
| 07:16 | | 07:16 | ||
− | |The loop condition fails and loop does not run. | + | |The '''loop condition''' fails and loop does not run. |
|- | |- | ||
| 07:20 | | 07:20 | ||
− | | This way, by using a do-while loop, we make sure that 0 is not considered as a perfect square. | + | | This way, by using a '''do-while''' loop, we make sure that '''0''' is not considered as a perfect square. |
|- | |- | ||
| 07:26 | | 07:26 | ||
− | | | + | | This way, a '''do-while''' loop is used for solving a range of problems. |
|- | |- | ||
| 07:31 | | 07:31 | ||
− | |Specially, when the loop must run at least once. | + | |Specially, when the '''loop''' must '''run''' at least once. |
|- | |- | ||
| 07:37 | | 07:37 | ||
− | | | + | | This brings us to the end of the tutorial. |
− | + | ||
|- | |- | ||
Line 407: | Line 365: | ||
|- | |- | ||
| 07:42 | | 07:42 | ||
− | | | + | | the '''do-while''' '''loop''' and how to use it. |
|- | |- | ||
| 07:46 | | 07:46 | ||
| As an assignment for this tutorial, solve the following problem. | | As an assignment for this tutorial, solve the following problem. | ||
− | |||
|- | |- | ||
| 07:50 | | 07:50 | ||
− | |Given a binary number, find out its decimal equivalent. Example: '''11010 => 26''' | + | |Given a '''binary''' number, find out its decimal equivalent. Example: '''11010 => 26 ''' |
|- | |- | ||
| 07:56 | | 07:56 | ||
− | | To know more about the | + | | To know more about the Spoken Tutorial project, watch the video available at the following link. |
|- | |- | ||
| 08:01 | | 08:01 | ||
Line 427: | Line 384: | ||
|- | |- | ||
| 08:06 | | 08:06 | ||
− | | | + | | The Spoken Tutorial project team. |
|- | |- | ||
| 08:10 | | 08:10 | ||
− | | Conducts workshops using | + | | Conducts workshops using spoken tutorials, gives certificates for those who pass an online test. |
|- | |- | ||
| 08:16 | | 08:16 | ||
Line 437: | Line 394: | ||
|- | |- | ||
| 08:22 | | 08:22 | ||
− | | The | + | | The 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. |
|- | |- | ||
| 08:32 | | 08:32 | ||
− | | | + | | More information on this mission is available at the following link. |
|- | |- | ||
| 08:36 | | 08:36 | ||
| This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | | This tutorial has been contributed by '''TalentSprint'''. Thanks for joining. | ||
− | |||
− | |||
− | |||
|} | |} |
Revision as of 18:35, 28 March 2015
Time | Narration |
00:01 | Welcome to the spoken tutorial on do-while Loop in java. |
00:06 | In this tutorial, you will learn about:
|
00:12 | For this tutorial, we are using:
|
00:20 | To follow this tutorial, you must have knowledge of while loop in java. |
00:25 | If not, for relevant tutorials, please visit our website as shown. |
00:32 | Here is the structure for do-while loop. |
00:37 | Notice that it is similar to a while loop. |
00:40 | It has two parts. |
00:42 | First is the loop running condition. And the second is the loop variable. |
00:51 | The only difference is that the condition is written after the do block. |
00:58 | And so the condition is checked after the execution of the statements inside the do block. |
01:05 | Now, let us see an example. |
01:07 | Switch to eclipse. |
01:11 | Here we have Eclipse IDE and the skeleton required for the rest of the code. |
01:17 | We have created a class DoWhileDemo and added the main method to it. |
01:22 | We are going to print the numbers from 1 to 10 using a do-while loop. |
01:27 | Type: |
01:29 | int n equal to 1. |
01:32 | 'n' is going to be loop variable. |
01:36 | Then type: do |
01:40 | open and close braces |
01:44 | Inside the braces System.out.println(n); |
01:55 | We shall print the value of 'n' and then increment it. n equal to n plus 1; |
02:05 | And we do this as long as |
02:08 | n is less than or equal to 10. |
02:10 | Outside the braces, type: while in parentheses (n less than equal to 10) |
02:20 | and close the do-while using a semi-colon. |
02:25 | Let us see the code in action. |
02:28 | Save and Run. |
02:37 | We see that the numbers from 1 to 10 are printed. |
02:42 | now Let us understand how the code is executed. |
02:47 | First, the value 1 is printed and then 'n' becomes 2. |
02:52 | And then, the condition is checked. |
02:55 | since it is true, again 2 is printed and 'n' becomes 3. |
03:00 | And so on till all the 10 numbers are printed and the value of 'n' becomes 11. |
03:06 | When n = 11, the condition fails and the loop stops. |
03:11 | Now, let us print numbers from 50 to 40 in decreasing order. |
03:17 | So, we start with 50. |
03:19 | Change n = 1 to n = 50. |
03:23 | Since we are looping from a bigger number to a smaller number, we decrement the loop variable. |
03:29 | So Change n = n + 1 to n = n - 1. |
03:34 | We loop as long as 'n' is greater than or equal to 40. |
03:40 | So, change the condition to n >= 40. |
03:48 | Let us look at the output. |
03:50 | Save and Run. |
03:57 | As we can see, the numbers from 50 to 40 are printed. |
04:02 | Now let us try a different logic using the do-while loop. |
04:10 | Given a number, we shall find out if it is a perfect square or not. |
04:15 | First clear the main method. |
04:19 | Then, type int n = 25; |
04:25 | We shall see if the value in 'n' is a perfect square or not. |
04:32 | Then type int x = 0; |
04:37 | We shall use 'x' to store the square root of the number if it is a perfect square. |
04:44 | Then type do |
04:46 | Open and close braces. |
04:49 | Inside the braces, x equal to x plus 1 |
04:55 | and outside the braces |
04:58 | while in paranthesis (x into x < n) |
05:06 | And close the do-while using a semi-colon. |
05:10 | As long as x into x is less than n, we keep incrementing the value of 'x'. |
05:16 | So, when the loop stops, the reverse of this condition will be true. |
05:22 | Which means, either 'x into x' must be equal to n |
05:26 | or it must be greater than 'n'. |
05:28 | If x into x is equal to n, the number is a perfect square. |
05:32 | If it is not equal to 'n', the number is not a perfect square. |
05:37 | So finally, we print the condition. |
05:47 | System.out.println(x * x == n); |
05:55 | Let us see the code in action. |
05:59 | Save and Run. As we can see, the output is true. |
06:07 | Let us try with other perfect square. |
06:10 | Change n = 25 to n = 49. |
06:15 | Save and Run. |
06:20 | We see that we get the true again . |
06:23 | Let us try with a number which is not a perfect square. |
06:26 | Change 49 to 23. Save and Run and |
06:34 | we get a false as expected. |
06:37 | Now, let us see what happens when the value of 'n' is 0. |
06:42 | Change n = 23 to n = 0. Since 0 is not a natural number, we must get a false. |
06:52 | Let us run the code. |
06:54 | Save and Run. |
07:00 | We see that we get false as expected. |
07:05 | This happens because even before the condition |
07:08 | x into x is less than n is checked, the value of 'x' is incremented and it becomes 1. |
07:16 | The loop condition fails and loop does not run. |
07:20 | This way, by using a do-while loop, we make sure that 0 is not considered as a perfect square. |
07:26 | This way, a do-while loop is used for solving a range of problems. |
07:31 | Specially, when the loop must run at least once. |
07:37 | This brings us to the end of the tutorial. |
07:40 | In this tutorial, we learnt about the |
07:42 | the do-while loop and how to use it. |
07:46 | As an assignment for this tutorial, solve the following problem. |
07:50 | Given a binary number, find out its decimal equivalent. Example: 11010 => 26 |
07:56 | To know more about the Spoken Tutorial project, watch the video available at the following link. |
08:01 | It summarizes the spoken-tutorial project.If you do not have good bandwidth, you can download and watch it. |
08:06 | The Spoken Tutorial project team. |
08:10 | Conducts workshops using spoken tutorials, gives certificates for those who pass an online test. |
08:16 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
08:22 | The 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. |
08:32 | More information on this mission is available at the following link. |
08:36 | This tutorial has been contributed by TalentSprint. Thanks for joining. |