Difference between revisions of "Java/C2/while-loop/English"
(Created page with ''''Title of script''': While Loop '''Author''': TalentSprint '''Keywords:''' loop, conditions, while loop, video tutorial {| style="border-spacing:0;" | style="border:0.035c…') |
|||
Line 25: | Line 25: | ||
'''About the while loop.''' | '''About the while loop.''' | ||
− | '''To use it.''' | + | '''How To use it.''' |
|- | |- | ||
Line 43: | Line 43: | ||
'''Prerequisites''' | '''Prerequisites''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of '''relational operators in Java''' |
− | If not, for relevant tutorials please visit our website | + | |
+ | |||
+ | If not, for relevant tutorials please visit our website as shown. [http://spoken-tutorial.org/ http][http://spoken-tutorial.org/ ://][http://spoken-tutorial.org/ spoken][http://spoken-tutorial.org/ -][http://spoken-tutorial.org/ tutorial][http://spoken-tutorial.org/ .][http://spoken-tutorial.org/ org] | ||
|- | |- | ||
Line 56: | Line 58: | ||
It has two parts. | It has two parts. | ||
− | One is the '''loop running condition''' and the | + | One is the '''loop running condition''' and the second is the '''loop variable.''' |
− | Let us look at an example. Switch to Eclipse | + | Let us now look at an example. Switch to Eclipse |
|- | |- | ||
Line 77: | Line 79: | ||
'''}''' | '''}''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We have the skeleton | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Here We have the eclipse IDE skeleton required for rest of the code |
Line 89: | Line 91: | ||
− | Type '''''int n = 1'' ''''' | + | Type '''''int n = 1'' ''''' |
− | + | ||
Line 98: | Line 99: | ||
'''int n = 1;''' | '''int n = 1;''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| This variable n is going to be our loop variable |
− | + | ||
− | + | ||
− | This is | + | |
|- | |- | ||
Line 107: | Line 105: | ||
'''while (n<nowiki> <= 10)</nowiki>''' | '''while (n<nowiki> <= 10)</nowiki>''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then type '''''while '''' | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then type '''''while ''''in ''parenthesis '''n''' less than or equal to'' '''''10''''' open and close braces |
|- | |- | ||
Line 113: | Line 111: | ||
'''while (n<nowiki> <= 10)</nowiki>''' | '''while (n<nowiki> <= 10)</nowiki>''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| This | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| This condition is called looping running condition. |
Line 134: | Line 132: | ||
} | } | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Inside the loop, we shall print the value of '''n''' | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Inside the loop, we shall print the value of '''n''' |
− | + | '''System.out.println(n);''' and then increment | |
− | + | ||
Line 160: | Line 157: | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Then the loop condition is checked. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Then the loop condition is checked. | ||
+ | |||
+ | Since it is true. | ||
− | |||
− | + | 2 is printed and n becomes 3. | |
− | + | ||
|- | |- | ||
Line 171: | Line 168: | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|And so on loop progresses untill 10 is printed after that n becomes 11 and the condition is not true and the loop will stop |
− | + | ||
− | + | ||
− | + | ||
− | Let us see the code in action. | + | So Let us see the code in action. |
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save. | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. |
− | + | ||
As we can see, the numbers from 1 to 10 are printed. | As we can see, the numbers from 1 to 10 are printed. | ||
− | Now | + | Now we shall print numbers from '''50''' to '''40''' |
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n = 1''' to '''n = 50''' | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n = 1''' to '''n = 50''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We start with 50. | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|So We start with 50. |
− | ''' | + | ''' Change n = 1 to n = 50''' |
|- | |- | ||
Line 203: | Line 198: | ||
− | In other words as long as n ''is 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. |
− | |||
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n = n + 1''' to '''n = n - 1''' | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n = n + 1''' to '''n = n - 1''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| since we are looping from a bigger number to a smaller number, we have decrement the loop variable. | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|And since we are looping from a bigger number to a smaller number, we have decrement the loop variable. |
− | Change '''n + 1''' to '''n - 1''' | + | So Change '''n=n + 1''' to '''n=n - 1''' |
− | + | ||
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As we can see, the | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Save and run. As we can see, the |
− | + | the numbers 50 to 40 has been printed | |
Now we shall print the first 10 multiples of 7. | Now we shall print the first 10 multiples of 7. | ||
Line 232: | Line 225: | ||
Change '''n = n – 1''' to '''n = n + 7''' | Change '''n = n – 1''' to '''n = n + 7''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To do that, we start with 7 | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To do that, we start with 7 |
− | So change '''n = 50''' to '''n = 7''' | + | So change '''n = 50''' to '''n = 7'''and then end with 70. |
− | + | Change the condition to n''' ''less than equal to '''''70''' | |
Line 248: | Line 241: | ||
− | So, change '''n - 1''' to '''n + 7''' | + | So, change ''' n=n - 1''' to '''n=n + 7''' |
− | Save | + | This way first 7 is printed and n becomes 14, 14 is printed and so on untill 70. Save and run |
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Save and run. Point to output | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As we can see, | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As we can see, the first 10 multiples of 7 is printed. |
Line 268: | Line 261: | ||
− | + | ||
− | '''n''' ''equal to'' '''13876''' | + | int '''n''' ''equal to'' '''13876'''. This is the number |
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Add '''int dSum = 0''' as the next line | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Add '''int dSum = 0''' as the next line | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''int dSum '''''equal to'' '''0''' | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Then '''int dSum '''''equal to'' '''0'''The variable dsum with symbolic for digit sum will contain the sum of digits |
− | + | ||
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n<nowiki> <= 70</nowiki>''' to '''n > 0''' | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n<nowiki> <= 70</nowiki>''' to '''n > 0''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Type while, |
+ | n greater than 0 open close bracket | ||
Line 293: | Line 286: | ||
'''n = n / 10;''' | '''n = n / 10;''' | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To get the sum of digits, we must first get the digits. | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To get the sum of digits, we must first get the digits. | ||
+ | To do that we use modulo operator. | ||
+ | |||
− | |||
− | + | Type '''dSum = dSum + (n % 10)''' So we get the unit digit and add it to dsum | |
− | Type '''dSum = dSum + (n % 10)''' | + | |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| | | style="border:0.035cm solid #000000;padding:0.176cm;"| | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Now | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| Now that we remove the digit by dividing by 10. |
− | + | n = n / 10 | |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the code : '''dSum''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the code : '''dSum''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| So when the loop | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| So when the loop is run for the first time, '''dSum '''will be 6 and n will be 1387. |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the code : '''dSum '''and '''n''' | | style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the code : '''dSum '''and '''n''' | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| And when the loop is run for the the second time, '''dSum''' will be sum of 7 and 6 which is 13, and |
− | + | ||
'''n''' will become 138. | '''n''' will become 138. | ||
− | So on, as the loop progresses, the digits will be | + | So on, as the loop progresses, the digits will be removed from '''n''' and finally |
− | + | '''n''' become zero.After that the condition n greater than 0 will be false and the loop will stop | |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| | | style="border:0.035cm solid #000000;padding:0.176cm;"| | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| So let us now add a print statement |
− | + | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''println(n)''' to '''println(dSum)''' | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''println(n)''' to '''println(dSum)''' | ||
save and run. Point to output | save and run. Point to output | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''System.out.println(dSum)''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | Let us see the code and action. Save and run | ||
+ | As we can see the sum of digit which is 25 has been printed | ||
+ | |||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| | | style="border:0.035cm solid #000000;padding:0.176cm;"| | ||
Line 350: | Line 338: | ||
'''Summary''' | '''Summary''' | ||
− | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| | + | | style="border:0.035cm solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|This brings us to the end of this tutorial. |
In this tutorial we have learnt | In this tutorial we have learnt | ||
− | * | + | * About while loop |
* how to use it | * how to use it | ||
Latest revision as of 09:19, 9 May 2013
Title of script: While Loop
Author: TalentSprint
Keywords: loop, conditions, while loop, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on While Loop in java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn
About the while loop. How To use it. |
Slide 3
Tools Used |
For this tutorial we are using
Ubuntu 11.10, JDK 1.6 and Eclipse 3.7 |
Slide 4
Prerequisites |
To follow this tutorial, you must have knowledge of relational operators in Java
|
Slide 5
While loop |
Here is the structure of a while loop.
It has two parts. One is the loop running condition and the second is the loop variable.
|
Point to the code
Eclipse should contain the following code.
public static void main(String[] args){ } } |
Here We have the eclipse IDE skeleton required for rest of the code
|
Type in main method,
int n = 1; |
We shall print numbers from 1 to 10 using a while loop.
|
Highlight
int n = 1; |
This variable n is going to be our loop variable |
Type in next line,
while (n <= 10) |
Then type while 'in parenthesis n less than or equal to 10 open and close braces |
Highlight
while (n <= 10) |
This condition is called looping running condition.
|
Change the while... to
while (n <= 10) { System.out.println(n); n = n + 1; } |
Inside the loop, we shall print the value of n
System.out.println(n); and then increment
|
Highlight print statement
Then increment statement |
This way, first 1 is printed and then the value of n becomes 2. |
Highlight condition
Then increment statement. |
Then the loop condition is checked.
2 is printed and n becomes 3. |
Highlight condition
|
And so on loop progresses untill 10 is printed after that n becomes 11 and the condition is not true and the loop will stop
So Let us see the code in action. |
Save and run. Point to output | Save and run.
|
Change n = 1 to n = 50 | So We start with 50.
Change n = 1 to n = 50 |
Change n <= 10 to n >= 40 | We go till 40.
|
Change n = n + 1 to n = n - 1 | And since we are looping from a bigger number to a smaller number, we have decrement the loop variable.
So Change n=n + 1 to n=n - 1
|
Save and run. Point to output | Save and run. As we can see, the
the numbers 50 to 40 has been printed Now we shall print the first 10 multiples of 7. |
Change n = 50 to n = 7
Change n >= 40 to n <= 70
|
To do that, we start with 7
|
To get the multiples, we shall increment the loop variable by 7.
This way first 7 is printed and n becomes 14, 14 is printed and so on untill 70. Save and run | |
Save and run. Point to output | As we can see, the first 10 multiples of 7 is printed.
|
Change n = 7 to n = 13876 | First clear the main method.
|
Add int dSum = 0 as the next line | Then int dSum equal to 0The variable dsum with symbolic for digit sum will contain the sum of digits
|
Change n <= 70 to n > 0 | Type while,
n greater than 0 open close bracket
|
Remove n = n + 7 and add this
dSum = dSum + (n % 10); n = n / 10; |
To get the sum of digits, we must first get the digits.
To do that we use modulo operator.
|
Now that we remove the digit by dividing by 10.
n = n / 10 | |
Point to the code : dSum | So when the loop is run for the first time, dSum will be 6 and n will be 1387. |
Point to the code : dSum and n | And when the loop is run for the the second time, dSum will be sum of 7 and 6 which is 13, and
n will become 138.
n become zero.After that the condition n greater than 0 will be false and the loop will stop |
So let us now add a print statement | |
Change println(n) to println(dSum)
save and run. Point to output |
System.out.println(dSum)
Let us see the code and action. Save and run As we can see the sum of digit which is 25 has been printed |
This way, a while loop, is one of the most fundamental constructs in programming, can be used. | |
Minimize the Eclipse window and switch to slides.
Summary |
This brings us to the end of this tutorial.
|
Slide 7Assignment
|
As an assignment for this tutorial solve the following problem.
Example: 19435 => 53491 |
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarizes the Spoken Tutorial project
If you do not have good bandwidth, you can download and watch it. |
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials, Gives certificates for those who pass an online test
For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 10Acknowledgement
|
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. More information on this Mission is available at the following link |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|