Java/C2/while-loop/English

From Script | Spoken-Tutorial
Revision as of 15:53, 27 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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


If not, for relevant tutorials please visit our website which is as shown. http://spoken-tutorial.org

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.


Let us look at an example. Switch to Eclipse

Point to the code


Minimize Slides and open Eclipse

Eclipse should contain the following code.


public class WhileDemo{

public static void main(String[] args){

}

}

We have the skeleton code required.


I have created a class WhileDemo and added the main method to it.

Type in main method,

int n = 1;

We shall print numbers from 1 to 10 using a while loop.


Type int n = 1 semi-colon.



Highlight

int n = 1;

We have created an integer n that we would be using inside the loop.


This is called the loop variable.

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.


It means the loop will run as long as this condition is true.


In our case, it will run as long as the value of n is less than or equal to 10.


And it will stop only when the value of n become greater than 10.

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.


Type System.out.println(n);


n = n + 1;



Highlight print statement

Then increment statement

This way, first 1 is printed and then the value of n becomes 2.
Highlight condition


Highlight print statement

Then increment statement.

Then the loop condition is checked.


The condition is true.


Then 2 is printed and the value of n becomes 3.

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.


Let us see the code in action.

Save and run. Point to output Save.


Run the code.

As we can see, the numbers from 1 to 10 are printed.


Now let us print numbers from 50 to 40

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.


In other words as long as n is greater than or equal to 40.


Change n lessthan equal to 10 to n greater than equal to 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.


Change n + 1 to n - 1


Let us now see the output.

Save and run. Point to output As we can see, the output is as expected.


Now we shall print the first 10 multiples of 7.

Change n = 50 to n = 7

Change n >= 40 to n <= 70


Change n = n – 1 to n = n + 7

To do that, we start with 7 and end with 70.


So change n = 50 to n = 7


n greater than equal to 40 to n less than equal to 70


This way, we make sure the loop stops at 70.

To get the multiples, we shall increment the loop variable by 7.


So, change n - 1 to n + 7


Save. Run.

Save and run. Point to output As we can see, the output is the first 10 multiples of 7.


We can also use a while loop to get the sum of digits of a number.


Let us see how to do so.

Change n = 7 to n = 13876 First clear the main method.


We start with the number.

n equal to 13876

Add int dSum = 0 as the next line int dSum equal to 0


This variable will contain the sum of digits.

Change n <= 70 to n > 0 Change n less than equal to 70 to n greater than 0


The reason for using this condition will be evident in a while.

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.


We do that by using the modulo operator.


Type dSum = dSum + (n % 10)

Now after adding that digit, we remove the digit by dividing the number by 10.


Type n = n / 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


n will become 138.


So on, as the loop progresses, the digits will be added to dSum.

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)


Save and run.


As we can see, the sum of digits has been printed.

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.


Slide 6

Summary

We have come to the end of this tutorial.


In this tutorial we have learnt

  • The while loop
  • how to use it


Slide 7Assignment


As an assignment for this tutorial solve the following problem.


Given a number, compute its reverse by using a while loop.

Example: 19435 => 53491

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


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


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


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



Contributors and Content Editors

Chandrika, Sneha