Java/C2/do-while/English
Title of script: do-while Loop
Author: TalentSprint
Keywords: loop, conditions, do-while loop, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on do-while Loop in java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn about
|
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 should have knowledge on while loop in java.
|
Slide 5
Point on slide do-while loop |
Here is the structure of a do-while loop.
|
The only difference is that the condition is written after the do block.
| |
Point to the code
Minimize Slides and open Eclipse Eclipse should contain the following code
public static void main(String[] args){ } } |
We have Eclipse IDE and the skeleton required for the rest of the code.
|
In the main method and type,
do { System.out.println(n); n = n + 1; } while(n <= 10); |
We are going to print the numbers from 1 to 10 using a do-while loop.
|
do
open curly braces and close System.out.println(n);
| |
And we do this as long as
| |
Add a semi-colon to complete the do-while statement. | |
Save and Run. Point to output. | Let us see the code in action.
|
We see that, the numbers from 1 to 10 are printed. | |
Let us now understand how the code is executed. | |
Navigate through the code
|
First, the value 1 is printed and n becomes 2.
|
Let us now print numbers from 50 to 40 in decreasing order. | |
Change n = 1 to n = 50 | So we start with 50.
Change n = 1 to n = 50. |
Change n = n + 1 to n = n - 1 | Since we are looping from a bigger number to a smaller number, we decrement the loop variable.
|
Change the n <= 10 to n >= 40 | We loop as long as n is greater than or equal to 40.
|
Save and Run. | Let us look at the output.
Save and Run. As we can see, the numbers from 50 to 40 are printed. |
Now let us try a different logic using the do-while loop. | |
Given a number, we shall find out if it is a perfect square or not. | |
Inside the main method, type
int n = 25; |
Clear the main method.
Type Inside the main method, type int n = 25; |
We shall see if the value in n is a perfect square or not. | |
int x = 0;
|
int x = 0;
|
do {
x = x + 1; } |
Type do
x equal to x plus 1 |
do {
x = x + 1; } while(x * x < n); |
Then,
|
Point to the condition then increment statement. | As long a x into x is less than n, we keep incrementing the value of x. |
So when the loop stops, the reverse of this condition will be true. | |
Which means either x into x must be equal to n.
| |
If x into x is equal to n, the number is a perfect square.
| |
So finally, we print the condition. | |
Type
System.out.println(x * x == n); |
System.out.println(x * x == n); |
Let us see the code in action. | |
Save and Run. Point to output | As we can see, the output is true
|
Change n = 25 to n = 49 | n = 49; |
Save and Run
We again get a true. | |
Let us try with a number which is not a perfect square. | |
Change n = 49 to n = 23
|
Change 49 to 23.
|
Change n = 23 to n = 0 | Since 0 is not a natural number, we must get a false.
Save. Run. |
We see that we get false as expected. | |
This happens because even before the condition
x into x is less than n is checked, the value of x is incremented and it becomes 1
| |
That way, by using a do-while loop, we make sure that 0 is not considered as a perfect square. | |
This way, a do-while loop is used for solving a range of problems.
Specially, when the loop must run at least once. | |
Minimize the Eclipse window and switch to slides.
Summary |
This brings us to the end of the tutorial.
do-while loop and how to use it |
Slide 7Assignment
|
As an assignment for this tutorial, solve the following problem.
|
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link. It summarizes the 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 and 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 and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|