Java/C2/do-while/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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
  • the do-while loop and
  • 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 musthave knowledge on while loop in java.


If not, for relevant tutorials please visit our website as shown.

http://spoken-tutorial.org

Slide 5

Point on slide

do-while loop

Here is the structure for do-while loop.


Notice that it is similar to a while loop.


It has two parts.


First is the loop running condition. And the second is the loop variable

The only difference is that the condition is written after the do block.


And so the condition is checked after the execution of the statements inside the do block


Now let us see an example.

Point to the code

Minimize Slides and open Eclipse

Eclipse should contain the following code


public class DoWhileDemo{

public static void main(String[] args){

}

}

Switch to eclipse, Here We have Eclipse IDE and the skeleton required for the rest of the code.


We have created a class DoWhileDemo and added the main method to it.



In the main method and type,


int n = 1;

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.


Type


int n equalto 1 semicolon


n is going to be loop variable.

then type do

open curly braces and close

System.out.println(n);


We shall print the value of n and then increment it.


n equalto n plus 1;

And we do this as long as


n is less than or equal to 10


Outside the braces Type while in paranthesis(n less than equalto 10)
Then close the do-while using a semi-colon
Save and Run. Point to output. Let us see the code in action.


Save and Run.

We see that, the numbers from 1 to 10 are printed.
now Let us understand how the code is executed.
Navigate through the code


Highlight print statement.


Highlight condition statement

First, the value 1 is printed and then n becomes 2.


And then, the condition is checked.


since it is true, again 2 is printed and n becomes 3.


And so on till all the 10 numbers are printed and the value of n becomes 11.


When n = 11, the condition fails and the loop stops.



Now Let us 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 n = n + 1 to n = n - 1

Change the n <= 10 to n >= 40 We loop as long as n is greater than or equal to 40.


So Change the condition to n >= 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;

First Clear the main method.
Again Type


int n = 25;

We shall see if the value in n is a perfect square or not.
int x = 0;


Then typeint x = 0;


We shall use x to store the square root of the number if it is a perfect square.

do {

x = x + 1;

}

Then Type do


Open close braces.

Inside the bracesx equal to x plus 1

do {

x = x + 1;

} while(x * x < n);

And outside the braces


while in paranthesis (x into (star) x < n)


And Close the do-while with a semi-colon

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.


Or x must be greater than n.

If x into x is equal to n, the number is a perfect square.


It is not equal to n, the number is not 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 Save and Run. As we can see, the output is true


Let us try with other perfect square.

Change n = 25 to n = 49 Change n = 25 to n = 49
Save and Run

We again get a true.

Let us try with a number which is not a perfect square.
Change 49 to 23


Save and Run.


Point to output.

Change 49 to 23.


We get a false.


Now let us see what happens when the value of n is 0.

Change n = 23 to n = 0 Change n = 23 to n = 0 Since 0 is not a natural number, we must get a false.


Let us run the code.

Save and 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


The loop condition fails and loop does not run.

This 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.


Slide 6

Summary

This brings us to the end of the tutorial.


In this tutorial, we learnt about the

do-while loop and how to use it

Slide 7Assignment


As an assignment for this tutorial, solve the following problem.


Given a binary number, find out its decimal equivalent. Example: 11010 => 26

Slide 8About the Spoken Tutorial Project
  • It summarizes the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


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
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


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
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


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
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Arya Ratish, Sneha