Converting for loop to while/do-while ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JanineXD
    New Member
    • Feb 2010
    • 18

    Converting for loop to while/do-while ?

    What seems to be wrong with my program?

    Code:
    public class FooBizBaz {
    	public static void main(String []args){
    int num1;
    int num=1;
    
    num1=num <= 50;
    
    
    System.out.println(num);
    
    	do{System.out.print("foo");}
    	while((num%3)==0);
    	
    	do{System.out.print("biz"); }
    	while((num%5)==0);
    
    	do{System.out.print("baz");}
    	while((num%7)==0);
    
    number++;	
    }  
    }
    here's what i made using for loop, and now i find it hard to convert it to do-while..
    Code:
    public class FooBizBaz{
    public static void main(String []args){
    
     for(int number=1; number <=50, number++)
    {
    System.out.println(number);
    if((number%3)==0){
    System.out.print("foo");
    }
    {
    if((number%5)==0){
    System.out.print("biz");
    }
    {
    if((number%7)==0){
    System.out.print("baz");
    }
    }
    }
    }
    Help please? Thank you very much!
  • ramesh shakya
    New Member
    • Aug 2009
    • 1

    #2
    hello are u looking for this

    class TestCase
    {
    public static void main(String argv[]) throws Exception
    {
    int number=0;
    do {
    if((number%3)== 0){
    System.out.prin t("foo");
    }
    if((number%5)== 0){
    System.out.prin t("biz");
    }

    if((number%7)== 0){
    System.out.prin t("baz");
    }
    number++;
    }while(number<= 50);
    }
    }

    Comment

    • anurag275125
      New Member
      • Aug 2009
      • 79

      #3
      You can use this code-----
      _______________ _______________ _______________ _______________ _____
      public class foobizbaz
      {
      public static void main(String args[])
      {
      int number=1;
      do
      {
      if(number%3==0)
      System.out.prin tln("foo");
      else if(number%5==0)
      System.out.prin tln("biz");
      else if(number%7==0)
      System.out.prin tln("baz");
      number++;
      }
      while(number<=5 0);
      }
      }

      _______________ _______________ _______________ _______________ _____

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        Is this an assignment question? If so, it might pay to say what the question actually is.

        It seems to me that you have converted the if statements into do loops. But the question would make more sense if you had been asked to convert the for loop into a do loop. (and leave the if statements alone.)

        Code:
        for(int ndx = 0; ndx < limit; ndx++) {
          if(whatever) {
            doSomethig();
          }
        }
        
        // is similar to
        
        int ndx = 0;
        do {
          if(whatever) {
            doSomething();
          }
          ndx++;
        } while(ndx < limit);

        Comment

        Working...