Difference between revisions of "Java/C2/do-while/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': do-while Loop '''Author''': TalentSprint '''Keywords:''' loop, conditions, do-while loop, video tutorial {| style="border-spacing:0;" | style="border:…')
 
 
(One intermediate revision by one other user not shown)
Line 44: Line 44:
  
 
'''Prerequisites'''
 
'''Prerequisites'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you should have knowledge on''' while loop in java.'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| To follow this tutorial, you musthave knowledge on''' while loop in java.'''
  
  
Line 57: Line 57:
  
 
'''do-while loop'''
 
'''do-while loop'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here is the structure of a do-while loop.  
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Here is the structure for  do-while loop.  
  
  
Line 66: Line 66:
  
  
First is the loop condition. And the second is the loop variable
+
First is the loop running condition. And the second is the loop variable
  
 
|-
 
|-
Line 73: Line 73:
  
  
And so the condition is checked after the execution of the statements.
+
And so the condition is checked after the execution of the statements inside the do block
  
  
Line 93: Line 93:
  
 
'''}'''
 
'''}'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| We have Eclipse IDE and the skeleton required for the rest of the code.
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"|Switch to eclipse, Here  We have Eclipse IDE and the skeleton required for the rest of the code.
  
  
Line 123: Line 123:
  
  
n is going to be our loop variable.
+
n is going to be   loop variable.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| '''do '''
+
| style="border:1pt solid #000000;padding:0.176cm;"| then type '''do '''
  
 
'''open curly braces and close'''
 
'''open curly braces and close'''
Line 147: Line 147:
  
  
Type '''while(n ''less than equalto'' 10)'''
+
Outside the braces Type '''while in paranthesis(n ''less than equalto'' 10)'''
 
+
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| '''Add a ''semi-colon'' '''to complete the do-while statement.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Then close the do-while using a semi-colon
  
 
|-
 
|-
Line 158: Line 157:
  
  
Save. Run.
+
Save and Run.
  
 
|-
 
|-
Line 166: Line 165:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| Let us now understand how the code is executed.
+
| style="border:1pt solid #000000;padding:0.176cm;"| now  Let us understand how the code is executed.
  
 
|-
 
|-
Line 176: Line 175:
  
 
'''Highlight condition statement'''
 
'''Highlight condition statement'''
| style="border:1pt solid #000000;padding:0.176cm;"| First, the value 1 is printed and n becomes 2.
+
| style="border:1pt solid #000000;padding:0.176cm;"| First, the value 1 is printed and then n becomes 2.
  
  
Line 182: Line 181:
  
  
since it is true, 2 is printed and n becomes 3.
+
since it is true, again 2 is printed and n becomes 3.
  
  
Line 195: Line 194:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| Let us now print numbers from 50 to 40 in decreasing order.
+
| style="border:1pt solid #000000;padding:0.176cm;"|Now  Let us print numbers from 50 to 40 in decreasing order.
  
 
|-
 
|-
Line 215: Line 214:
  
  
Change the condition to n >= 40
+
So Change the condition to n >= 40
  
 
|-
 
|-
Line 237: Line 236:
  
 
'''int n = 25;'''
 
'''int n = 25;'''
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the main method.
+
| style="border:1pt solid #000000;padding:0.176cm;"|First Clear the main method.
  
Type
+
Again Type
  
Inside the main method, type
+
  
 
'''int n = 25;'''
 
'''int n = 25;'''
Line 254: Line 253:
  
  
| style="border:1pt solid #000000;padding:0.176cm;"| '''int x = 0;'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Then type'''int x = 0;'''
  
  
We shall use x to store the square root if the number is a perfect square.
+
We shall use x to store the square root of the number if it  is a perfect square.
  
 
|-
 
|-
Line 265: Line 264:
  
 
'''}'''
 
'''}'''
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''do'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Then Type '''do'''
  
  
 
Open close braces.
 
Open close braces.
  
'''x '''''equal to '''''x '''''plus''''' 1'''
+
Inside the braces'''x '''''equal to '''''x '''''plus''''' 1'''
  
 
|-
 
|-
Line 278: Line 277:
  
 
'''} while(x * x<nowiki> < </nowiki>n);'''
 
'''} while(x * x<nowiki> < </nowiki>n);'''
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Then,
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| And outside the braces
  
  
'''while(x ''into (star)'' x<nowiki> < </nowiki>n)'''
+
'''while in paranthesis (x ''into (star)'' x<nowiki> < </nowiki>n)'''
  
  
Close with a semi-colon
+
And Close the do-while with a semi-colon
  
 
|-
 
|-
Line 299: Line 298:
  
  
Or x into x must be greater than n.
+
Or x must be greater than n.
  
 
|-
 
|-
Line 324: Line 323:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Save''' and '''Run'''. Point to output
 
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Save''' and '''Run'''. Point to output
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| As we can see, the output is '''true'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| '''Save''' and '''Run'''.  As we can see, the output is '''true'''
  
  
Let us with another perfect square.
+
Let us try  with other perfect square.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''Change n = 25 to n = 49'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''Change n = 25 to n = 49'''
| style="border:1pt solid #000000;padding:0.176cm;"| '''n = 49;'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| '''Change n = 25 to n = 49'''
  
 
|-
 
|-
Line 344: Line 343:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change '''n = 25''' to '''n = 23'''
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change ''' 49''' to ''' 23'''
  
  
Line 351: Line 350:
  
 
Point to output.
 
Point to output.
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change 25 to 23.
+
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.191cm;padding-right:0.191cm;"| Change 49 to 23.
  
  
Line 361: Line 360:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Change n = 23 to n = 0
 
| style="border:1pt solid #000000;padding:0.176cm;"| Change n = 23 to n = 0
| style="border:1pt solid #000000;padding:0.176cm;"| Since 0 is not a natural number, we must get a false.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Change n = 23 to n = 0 Since 0 is not a natural number, we must get a false.
  
  
 
Let us run the code.
 
Let us run the code.
  
Save. Run.
+
Save and Run.
  
 
|-
 
|-
Line 383: Line 382:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| That way, by using a do-while loop, we make sure that 0 is not considered as a perfect square.
+
| style="border:1pt solid #000000;padding:0.176cm;"| This way, by using a do-while loop, we make sure that 0 is not considered as a perfect square.
  
 
|-
 
|-

Latest revision as of 12:24, 9 May 2013

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