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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
 
{| border=1
 
{| border=1
 
|| '''Time'''
 
|| '''Time'''
Line 7: Line 6:
 
|-
 
|-
 
| 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''' and How to use it.
* 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 '''Eclipse 3.7'''.
'''JDK 1.6''' and  
+
'''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'''.  
  
 
|-
 
|-
Line 56: Line 51:
 
|-
 
|-
 
|  01:05
 
|  01:05
| We shall print numbers from '''1''' to '''10''' using a '''while loop'''. Type: '''int n = 1'''.
+
|  We shall print the numbers from '''1''' to '''10''' using a '''while loop'''. Type: '''int n = 1;'''.
  
 
|-
 
|-
Line 68: Line 63:
 
|-
 
|-
 
|  01:33
 
|  01:33
| This condition is called  '''looping running condition'''.
+
| This condition is called  '''loop running condition'''.
  
 
|-
 
|-
Line 88: Line 83:
 
|-
 
|-
 
|  01:58
 
|  01:58
| '''System.out.println(n);''' and then increment '''n = n + 1;'''
+
| '''System.out.println(n);''' and then increment it '''n = n + 1;'''
  
 
|-
 
|-
Line 120: Line 115:
 
|-
 
|-
 
|  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, we start with 50.   Change '''n = 1''' to '''n = 50'''.
+
|  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' is greater than or equal to 40. So, change the condition to  n  greater than or equal to 40.
+
| 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.
  
 
|-
 
|-
Line 144: Line 139:
 
|-
 
|-
 
| 03:27
 
| 03:27
| '''Save''' and '''run'''. As we can see, the numbers 50 to 40 have been printed.
+
| '''Save''' and '''run'''. As we can see, the numbers from 50 to 40 have been printed.
  
 
|-
 
|-
Line 156: Line 151:
 
|-
 
|-
 
|  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 we end with 70.
  
 
|-
 
|-
 
|  03:57
 
|  03:57
| Change the condition to '''n''' less than equal to '''70'''.
+
| Change the condition to '''n''' less than or equal to '''70'''.
  
 
|-
 
|-
 
|  04:03
 
|  04:03
| This way, we make sure the '''loop''' stops at 70.
+
| This way, we make sure that the '''loop''' stops at 70.
  
 
|-
 
|-
Line 172: Line 167:
 
|-
 
|-
 
|  04:12
 
|  04:12
|So, change '''n=n-1''' to '''n= n+7'''.
+
|So, change '''n=n-1''' to '''n= n+7;'''
  
 
|-
 
|-
Line 196: Line 191:
 
|-
 
|-
 
|  04:54
 
|  04:54
| '''int n''' equal to '''13876'''. This is the number.
+
| '''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.
+
| Then '''int dSum''' equal to  '''0'''. The variable '''dsum''' which is symbolic for digit sum will contain the sum of digits.
  
 
|-
 
|-
 
|  05:18
 
|  05:18
|  Type: '''while n''' greater than '''0''' open close parenthesis.
+
|  Type: '''while n''' greater than '''0''' open close parentheses.
  
 
|-
 
|-
Line 215: Line 210:
 
|-
 
|-
 
|  05:36
 
|  05:36
| To do that, we use '''modulo operator'''.  
+
| To do that, we use the '''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's digit and add it to '''dsum'''.
  
 
|-
 
|-
Line 227: Line 222:
 
|-
 
|-
 
| 06:08
 
| 06:08
| So when the '''loop''' is  '''run'''  for the first time, '''dSum''' will be 6 and 'n' will be 1387.
+
| So when the '''loop''' is  '''run'''  for the first time, '''dSum''' will be 6 and 'n' will become 1387.
  
 
|-
 
|-
Line 235: Line 230:
 
|-
 
|-
 
|  06:22
 
|  06:22
| So on as the loop progresses, the digits will be  removed from '''n''' and finally
+
| And so on as the loop progresses, the digits will be  removed from '''n''' and finally
  
 
|-
 
|-
Line 250: Line 245:
 
|-
 
|-
 
|  06:51
 
|  06:51
| Let us see the code and action. '''Save''' and '''run'''.
+
| Let us see the code in action. '''Save''' and '''run'''.
 
|-
 
|-
 
|  06:59
 
|  06:59
| As we can see, the sum of digit which is 25 has been printed.
+
| As we can see, the sum of digits which is 25 has been printed.
 
   
 
   
 
|-
 
|-
 
| 07:06  
 
| 07:06  
| This way, a '''while loop''' which is one of the most fundamental '''constructs''' in programming, can be used.
+
| This way a '''while loop''' which is one of the most fundamental '''constructs''' in programming can be used.
  
 
|-  
 
|-  
Line 265: Line 260:
 
|-
 
|-
 
|  07:20
 
|  07:20
| In this tutorial, we have learnt:
+
| In this tutorial, we have learned: About while loop.How to use it.
* About while loop
+
* How to use it.
+
  
 
|-
 
|-
Line 279: Line 272:
 
|-
 
|-
 
|07:37
 
|07:37
|To know more about the Spoken Tutorial project, watch the video available at the following link, that summarizes the Spoken Tutorial project.
+
|To know more about the Spoken Tutorial project, watch the video available at the following link. It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
Line 287: Line 280:
 
|-
 
|-
 
| 07:50  
 
| 07:50  
|  The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. Gives certificates for those who pass an online test.
+
|  The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. Gives certificates to those who pass an online test.
  
 
|-
 
|-
 
|  07:57
 
|  07:57
| For more details, please write to '''contact AT spoken HYPHEN tutorial DOT org.'''
+
| For more details, please write to '''contact@spoken-tutorial DOT org.'''
  
 
|-
 
|-

Latest revision as of 11:02, 18 October 2017

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 and How to use it.
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 the 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 loop 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 it 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 from 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 we end with 70.
03:57 Change the condition to n less than or equal to 70.
04:03 This way, we make sure that 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 which is symbolic for digit sum will contain the sum of digits.
05:18 Type: while n greater than 0 open close parentheses.
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 the modulo operator.
05:40 Type: dSum = dSum + (n % 10) So, we get the unit's 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 become 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 And 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 in action. Save and run.
06:59 As we can see, the sum of digits 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 learned: About while loop.How to use it.
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. It 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 to those who pass an online test.
07:57 For more details, please write to contact@spoken-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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha