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. 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 on 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 other is the loop variable.
|
Point to the code
Eclipse should contain the following code.
public static void main(String[] args){ } } |
We have the skeleton code required.
|
Type in main method,
int n = 1; |
We shall print numbers from 1 to 10 using a while loop.
|
Highlight
int n = 1; |
We have created an integer n that we would be using inside the loop.
|
Type in next line,
while (n <= 10) |
Then type while within parenthesis n less than equalto 10 |
Highlight
while (n <= 10) |
This statement is called the looping 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 and then increment it.
|
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.
|
Highlight condition
|
This happens till 10 is printed and the value of n becomes 11.
Once the value of n is 11, the condition n <= 10 is not true. And hence the loop stops.
|
Save and run. Point to output | Save.
As we can see, the numbers from 1 to 10 are printed.
|
Change n = 1 to n = 50 | We start with 50.
So Change n = 1 to n = 50 |
Change n <= 10 to n >= 40 | We go till 40.
|
Change n = n + 1 to n = n - 1 | since we are looping from a bigger number to a smaller number, we have decrement the loop variable.
|
Save and run. Point to output | As we can see, the output is as expected.
|
Change n = 50 to n = 7
Change n >= 40 to n <= 70
|
To do that, we start with 7 and end with 70.
|
To get the multiples, we shall increment the loop variable by 7.
| |
Save and run. Point to output | As we can see, the output is the first 10 multiples of 7.
|
Change n = 7 to n = 13876 | First clear the main method.
n equal to 13876 |
Add int dSum = 0 as the next line | int dSum equal to 0
|
Change n <= 70 to n > 0 | Change n less than equal to 70 to n greater than 0
|
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.
|
Now after adding that digit, we remove the digit by dividing the number by 10.
| |
Point to the code : dSum | So when the loop runs for the first time, dSum will be 6 and n will be 1387. |
Point to the code : dSum and n | After the second run, dSum will be 13, which is the sum of last two digits
And the value of n will become zero. |
Finally, we print the dSum. | |
Change println(n) to println(dSum)
save and run. Point to output |
Type System.out.println(dSum)
|
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 |
We have come 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.
|