Difference between revisions of "Java/C2/do-while/English"
Arya Ratish (Talk | contribs) (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 | + | | 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 | + | | 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 | + | 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;"| | + | | style="border:1pt solid #000000;padding:0.176cm;"| Then close the do-while using a semi-colon |
|- | |- | ||
Line 158: | Line 157: | ||
− | Save | + | 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 | + | | 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 | + | | 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 |
− | + | ||
'''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 | + | 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;"| | + | | 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 | + | 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 | + | 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 ''' | + | | 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 | + | | 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 | + | 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;"| | + | | 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
|
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.
|
Slide 5
Point on slide do-while loop |
Here is the structure for 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){ } } |
Switch to eclipse, Here 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.
|
then type do
open curly braces and close System.out.println(n);
| |
And we do this as long as
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.
|
We see that, the numbers from 1 to 10 are printed. | |
now Let us understand how the code is executed. | |
Navigate through the code
|
First, the value 1 is printed and then n becomes 2.
|
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 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;
|
do {
x = x + 1; } |
Then Type do
Inside the bracesx equal to x plus 1 |
do {
x = x + 1; } while(x * x < n); |
And outside the braces
|
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 | Save and Run. As we can see, the output is true
|
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
|
Change 49 to 23.
|
Change n = 23 to n = 0 | Change n = 23 to n = 0 Since 0 is not a natural number, we must get a false.
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
| |
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.
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.
|