Home | | Sitemap ||Page number :16

do-while

The do-while loop can also be used to repeat a set of statements. Syntax---

do { Anything you wish!!! } while(condition);

If you will carefully examine the structure of do-while loop, you will find that the condition was tested after the execution was done once. Thus we come to a conclusion that the do-while executes the statement(s) at least once.

/* A program to print a message 1000 times. */ class dowhloop { public static void main(String args[]) { int c=1; do { System.out.println(“HAPPY B’DAY”); c++; } while(c<=1000); } }

for loop The “for” loop is most widely used for repeating a set of statements. The credit goes to the clear cut interface defined by it.

Syntax—

for(initial value of counter; condition; update counter) { // Statements }

For using for loop you should be clear about the fact-“ How many times exactly will my program actually execute?”

/* A program to print a message 1000 times */

class forloop { public static void main(String args[]) { int c; for(c=1;c<=1000;c++) System.out.println(“HAPPY B’DAY”); } }

Factorial of a number—

You have already studied about the concept of factorial, when we studied about recursion. So the definition of factorial is clear, now we just need to write a program using loops. Developing logic is the most important part which you will be able to do it yourself after some practice.

Factorial-class fact { public static void main(String args[]) { int no,c; no=Integer.parseInt(args[0]);

for(c=no;c>1;c--) { no=no*(c-1); } System.out.println(" Factorial is "+no); } }

Writing the program using while loop.

class fact {

public static void main(String args[]) { int no,c; no=Integer.parseInt(args[0]); c=no;

while(c>1) { no=no*(c-1); c--; } } }

Writing the program using do-while loop

class fact { public static void main(String args[]) { int no,c; no=Integer.parseInt(args[0]); c=no;

do { no=no*(c-1); c--; } while(c<1);

} }

Two keywords..

  1. break
  2. continue

(1).The “break” keyword is used to get out of a loop. Usually we break with an “if” statement, so the loop terminates only at a certain condition. Read this program for a clearer understanding, this program uses the “io” package and gets input from user. I have used the InputStreamReader and BufferedReader classes. You are still unfamiliar with these concepts. So rather on concentrating on them and getting carried away better have an eye on the loop and break. The program is commented extensively to assist you.

import java.io.*; //Importing io package to help you get the input class limit_no

{ public static void main(String args[]) throws IOException { int no; // Variable that will repeatedly take the input from user int c=5; InputStreamReader rd=new InputStreamReader(System.in); //Lines help us to take an //input BufferedReader inp=new BufferedReader(rd);// Lines help us to take an input

while(c==5) // Loop should not terminate until c=5 { System.out.println("Enter your marks"); // Message that asks users for an input String v1=inp.readLine(); no=Integer.parseInt(v1); //Variable "no" is finally initialized with the value entered by //user if(no<0 || no>100) //Loop will still terminate if this condition gets false. break; } System.out.println("While Loop Terminated"); } }

Have a look at the output of the terminal window----

xs

As soon as a digit greater than 100 was entered the program terminates. This is all because of the “break” and the “if” condition associated with the code. It is clear that the loop will only terminate when c is not equals to five. As c was initialized with 5 earlier and no tampering is done on it, without the “break” keyword the loop will run infinitely.

(2).continue: The “continue” keyword is used to force an execution of loop. Let's study an example similar to the previous one.

import java.io.*; //Importing io package to help you get the input class limit_no2 // Variable that will repeatedly take the input from user {

public static void main(String args[]) throws IOException { int no; int c=1; int x=1; InputStreamReader rd=new InputStreamReader(System.in); //Lines to help us to take an input BufferedReader inp=new BufferedReader(rd);

while(x<=3) //while loop will cause the block of code to execute thrice { System.out.println("Enter a Number"); //Message to be printed String v1=inp.readLine(); no=Integer.parseInt(v1); x++; //Update value of x,thus regulating the times loop executes if(no==5) // Force the execution of Loop continue; System.out.println("This message will not be printed in an exceptional case"); // This //message will not be executed if number entered is equal to 5 }

} }

asd

Have a careful look at the output while comparing it with the input. You will find that when the input was 5, the execution of loop was continued and hence the message "This message will not be printed in an exceptional case" is not printed.