Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Convert do while loop to for loop

    Hello to all
    Please help me to covert this code to for loop

    class Sanjeev{
    public static void main(String args[]){
    int n=1;
    double x=0,s=0;
    do{
    s=10/n*n;
    x=x+s;
    n++;
    }while(s>0.1);
    System.out.println("the value of x is "+x);
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    PREPARATION;
    do {
    	EXECUTION;
    	VARIATOR;
    } while (CONDITIONAL);
    Java Code:
    PREPARATION;
    EXECUTION;
    while (CONDITIONAL) {
    	EXECUTION;
    	VARIATOR;
    }
    Java Code:
    EXECUTION;
    for (PREPARATION; CONDITIONAL; VARIATOR) {
    	EXECUTION;
    }

    Assuming that EXECUTION is the code that executes on each iteration of the loop, PREPARATION is an initial statement, CONDITIONAL is a true/false that checks when to end the loop, and VARIATOR is some change in a variable that will eventually terminate the loop, then these code blocks are all equal.

    As an example, this code will execute someFunction() fifty-one times...
    Java Code:
    int n = 1;
    do {
    	someFunction();
    	n++;
    } while (n <= 50);
    Java Code:
    int n = 1;
    someFunction();
    while (n <= 50) {
    	someFunction();
    	n++;
    }
    Java Code:
    someFunction();
    for (int n = 1; n <= 50; n++) {
    	someFunction();
    }

    Does that help at all?

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    5

    Default

    Java Code:
        public static void main(String args[]){
            int n =1;
            double x=0,s=0;
            s=10/n*n;
            x=x+s;
            n++;
            for(;s>0.1; s=10/n*n ){
                x=x+s;
                n++;
            }
            System.out.println("the value of x is "+x);
        }

  4. #4
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks Zack
    I have solved this problem and thanks again for revising me the loops

    class Sanjeev{
    public static void main(String[] args){
    int n=1;
    double x=0;
    double s;
    for(s=0;s>0.1;s--){

    s=10/n*n;
    x=x+s;
    n++;

    }
    System.out.println("the value of x is "+x);
    }
    }

    Both will produce the same output

Similar Threads

  1. Help with Loop
    By PsychoNaut in forum New To Java
    Replies: 2
    Last Post: 10-14-2010, 07:01 AM
  2. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  3. using for loop...pls help
    By princess.blue in forum Eclipse
    Replies: 2
    Last Post: 12-13-2009, 10:30 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  5. Help w/ loop
    By iWonder in forum New To Java
    Replies: 17
    Last Post: 11-20-2008, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •