Java/C2/while-loop/English

From Script | Spoken-Tutorial
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.

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 must have knowledge of relational operators in Java



If not, for relevant tutorials please visit our website 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 second is the loop variable.


Let us now 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){

}

}

Here We have the eclipse IDE skeleton required for rest of the code


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


Highlight

int n = 1;

This variable n is going to be our loop variable
Type in next line,

while (n <= 10)

Then type while 'in parenthesis n less than or equal to 10 open and close braces
Highlight

while (n <= 10)

This condition is called looping running 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
 System.out.println(n); and then increment


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.


Since it is true.


2 is printed and   n becomes 3.
Highlight condition


And so on loop progresses untill 10 is printed after that n becomes 11 and the condition is not true and the loop will stop



So Let us see the code in action.
Save and run. Point to output Save and run.



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


Now we shall print numbers from 50 to 40

Change n = 1 to n = 50 So We start with 50.

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. So change the condition to n greater than or equal to 40.


Change n = n + 1 to n = n - 1 And since we are looping from a bigger number to a smaller number, we have decrement the loop variable.


So Change n=n + 1 to n=n - 1


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

the numbers 50 to 40 has been printed

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


So change n = 50 to n = 7and then end with 70.


Change the condition 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=n - 1 to n=n + 7


This way first 7 is printed and n becomes 14, 14 is printed and so on untill 70. Save and run
Save and run. Point to output As we can see, the first 10 multiples of 7 is printed.


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.



int n equal to 13876. This is the number

Add int dSum = 0 as the next line Then int dSum equal to 0The variable dsum with symbolic for digit sum will contain the sum of digits


Change n <= 70 to n > 0 Type while,

n greater than 0 open close bracket


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.

To do that we use modulo operator.



Type dSum = dSum + (n % 10) So we get the unit digit and add it to dsum

Now that we remove the digit by dividing by 10.


 n = n / 10
Point to the code : dSum So when the loop is run for the first time, dSum will be 6 and n will be 1387.
Point to the code : dSum and n And when the loop is run for the the second time, dSum will be sum of 7 and 6 which is 13, and

n will become 138.


So on, as the loop progresses, the digits will be removed from n and finally

 n   become zero.After that the condition n greater than 0 will be false and the loop will stop
So let us now add a print statement
Change println(n) to println(dSum)

save and run. Point to output

System.out.println(dSum)

Let us see the code and action. Save and run

As we can see the sum of digit which is 25 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

This brings us to the end of this tutorial.


In this tutorial we have learnt

  • About 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