Difference between revisions of "Java/C2/do-while/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by one other user not shown)
Line 10: Line 10:
 
|-
 
|-
 
|  00:06
 
|  00:06
| | In this tutorial, you will learn about:
+
| In this tutorial, you will learn about:
* '''do-while loop''' and
+
'''do-while loop''' and
* '''how to use it'''
+
'''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'''
  
 
|-
 
|-
Line 43: Line 43:
 
|-
 
|-
 
| 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'''.
  
 
|-
 
|-
Line 51: Line 51:
 
|-
 
|-
 
| 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.
  
 
|-
 
|-
Line 79: Line 79:
 
|-
 
|-
 
| 01:29
 
| 01:29
|'''int n''' equal to '''1'''.
+
|'''int n''' equal to '''1;'''.
  
 
|-
 
|-
 
| 01:32
 
| 01:32
|'n' is going to be '''loop variable'''.
+
|'n' is going to be the '''loop variable'''.
  
 
|-
 
|-
Line 110: Line 110:
 
|-
 
|-
 
| 02:10
 
| 02:10
| Outside the braces, type: '''while''' in parentheses ('''n''' less than equal to '''10''')
+
| So, outside the braces, type: '''while''' in parentheses '''n''' less than or equal to '''10'''.
 
|-
 
|-
 
| 02:20
 
| 02:20
|  and close the '''do-while''' using a semi-colon.
+
|  and close the '''do-while''' using a semicolon.
  
 
|-
 
|-
Line 125: Line 125:
 
|-
 
|-
 
| 02:37
 
| 02:37
| | We see that the numbers from 1 to 10 are printed.
+
| We see that the numbers from 1 to 10 are printed.
  
 
|-
 
|-
 
| 02:42
 
| 02:42
now  Let us understand how the code is executed.
+
Now, let us understand how the code is executed.
  
 
|-
 
|-
Line 161: Line 161:
 
|-
 
|-
 
| 03:19
 
| 03:19
|Change n = 1 to n = 50.
+
|So, change n = 1 to n = 50.
  
 
|-
 
|-
Line 169: Line 169:
 
|-
 
|-
 
| 03:29
 
| 03:29
|So Change '''n = n + 1''' to '''n = n - 1'''.
+
|So, change '''n = n + 1''' to '''n = n - 1'''.
  
 
|-
 
|-
Line 201: Line 201:
 
|-
 
|-
 
|  04:15
 
|  04:15
|First clear the '''main method'''.
+
| First clear the '''main method'''.
  
 
|-
 
|-
Line 229: Line 229:
 
|-
 
|-
 
| 04:49
 
| 04:49
|Inside the braces, '''x''' equal to '''x ''' plus ''' 1'''
+
|Inside the braces, '''x''' equal to '''x ''' plus ''' 1;'''
  
 
|-
 
|-
Line 237: Line 237:
 
|-
 
|-
 
| 04:58
 
| 04:58
|'''while''' in paranthesis '''(x into x < n)'''
+
|'''while''' in parentheses '''x into x is less than n'''
  
 
|-
 
|-
Line 245: Line 245:
 
|-
 
|-
 
|05:10  
 
|05:10  
| As long as '''x''' into '''x''' is less than '''n''', we keep incrementing the value of 'x'.
+
| 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'
  
 
|-
 
|-
Line 273: Line 273:
 
|-
 
|-
 
| 05:47
 
| 05:47
| '''System.out.println(x * x == n);'''
+
| '''System.out.println (x * x == n);'''
  
 
|-
 
|-
Line 285: Line 285:
 
|-
 
|-
 
| 06:07
 
| 06:07
|Let us try with other perfect square.
+
|Let us try with another perfect square.
  
 
|-
 
|-
Line 297: Line 297:
 
|-
 
|-
 
| 06:20
 
| 06:20
|We see that we get the '''true''' again .
+
|We see that we get a '''true''' again .
  
 
|-
 
|-
Line 341: Line 341:
 
|-
 
|-
 
| 07:16
 
| 07:16
|The '''loop condition''' fails and loop does not run.
+
|The '''loop condition''' fails and the loop does not run.
  
 
|-
 
|-
Line 361: Line 361:
 
|-
 
|-
 
| 07:40
 
| 07:40
|In this tutorial, we learnt about the
+
|In this tutorial, we have learnt about
  
 
|-
 
|-
Line 380: Line 380:
 
|-
 
|-
 
| 08:01
 
| 08:01
| It summarizes the spoken-tutorial project.If you do not have good bandwidth, you can download and watch it.
+
| It summarizes the spoken-tutorial project. If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
 
|  08:06
 
|  08:06
| The Spoken Tutorial project team.
+
| The Spoken Tutorial project team:
 
|-
 
|-
 
| 08:10
 
| 08:10
| Conducts workshops using spoken tutorials, gives certificates for those who pass an online test.  
+
| Conducts workshops using spoken tutorials, gives certificates to those who pass an online test.  
 
|-
 
|-
 
| 08:16
 
| 08:16

Latest revision as of 19:27, 20 February 2017

Time Narration
00:01 Welcome to the spoken tutorial on do-while Loop in java.
00:06 In this tutorial, you will learn about:

do-while loop and how to use it

00:12 For this tutorial, we are using:

Ubuntu 11.10 JDK 1.6 and Eclipse 3.7

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 the 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 So, outside the braces, type: while in parentheses n less than or equal to 10.
02:20 and close the do-while using a semicolon.
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 So, 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 parentheses x into x is less than 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 another perfect square.
06:10 Change n = 25 to n = 49.
06:15 Save and Run.
06:20 We see that we get a 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 the 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 have learnt about
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 to 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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14, Sneha