Java/C2/while-loop/English
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.
|