Scanner class Methods in Java

Looping Structures- The do-while Loop

 

We discussed about Looping Structures and the while loop. In this post, we are going to discuss the second type of loop. 

 The do-while Loop

The do-while loop is the similar to while loop except it tests the condition at the end of loop. It means we first write the statements whom we want to execute in loop and later we check for condition. The do-while loop let us to execute statement once before testing the condition.Even if the condition is false during the first pass still the statements will get executed once.
Syntax:
do

    statement(s);

while(condition);

In the above syntax, the conditions can be any relational or logical expression which must be enclosed within  parenthesis.Lets understand the syntax in points.

  1. do is a keyword which means to execute the statements written in body of loop.
  2. After closing the body, while is used to check condition passed in parenthesis.
  3. Never forget to add ; at the end of while condition.

Working of do-while loop

 Flowchart given below shows the working of do-while loop,
Flowchart of do-while loop

Working:
  1. The control enters.
  2. Unlike while loop, statements in the body of loop get executed first.
  3. After the first execution of statements, the while tests for condition.
  4. If condition is true, the control goes back to do and execute the body of loop.
  5. If condition is false,the control transferred to  next statement after the loop.

Example of while loop

  Let's understand do-while loop with an example.

Example: Program to find sum of first N natural numbers using do-while loop. 

Program to find sum of first N natural numbers using do-while loop.

In the example, N means to find sum of natural numbers up to any number. In the program,

  1. We first declare a integer variable "n".This variable will get value from user.If user give 10 then program will find the sum of first 10 natural numbers.
  2. Next three statements are used to get input from user.In Eclipse, it is important to close the scanner object because it continues to show warning of memory leak until it is not closed.
  3. Now, we defined two variables, "i" to hold the value of natural numbers and also to control the loop, second variable "sum" to hold the sum of natural numbers.
  4. The do-while loop, where do contains block of statements. In here, first statement, sum+=i; which add value of i to sum variable each time loop runs.And second statement i++; it increments the value of i.
  5. The while checks for condition (i<=n), it tests if i is less than or equal to n. When the value of i gets greater than n then loop will stop executing.
  6. The last print statements prints the sum of n natural numbers on screen.
  7. In the output, user gave 10 as input, and the value of n will be 10 in our program . Try to analyse output on your own by putting n=10.

 Experiment: Write a program to print first n natural numbers.

Happy Coding!


Comments

Post a Comment

If you have any doubt, ask here