1

So I had this while loop for a lab in my class I had to make. I then had to convert the loop to a do-while loop to see the difference in design and make sure I got the same results. Now I need to convert it into a for loop, but I don't know how to set up the for statement. Here is how it is currently:

  do
  {
     die1Value = generator.nextInt(6) + 1;
     die2Value = generator.nextInt(6) + 1;
     if (die1Value == die2Value)
     {
        if (die1Value == 1)
        {   
           snakeEyes += 1;
        }
        else if (die1Value == 2)
        {   
           twos += 1;
        }   
        else if (die1Value == 3)
        {   
           threes += 1;
        }   
        else if (die1Value == 4)
        {   
           fours += 1;
        }   
        else if (die1Value == 5)
        {   
           fives += 1;
        }   
        else if (die1Value == 6)
        {   
           sixes += 1;   
        }
      }
      count += 1;
    }while (count < NUMBER);
2
  • And what have you tried? Commented Feb 10, 2014 at 1:55
  • 6
    I'd actually start with your original while, not your do while. The reason is that a for loop does its check at the beginning, not the end, just like while. So the original will be closer to what you want to finish with. Commented Feb 10, 2014 at 1:56

3 Answers 3

1

As others have mentioned, the problem arises that the for loop will check the condition at the beginning of the loop, but your do while loop will check the condition at the end of your loop.

All that means is that with a for loop, you are not guaranteed at least one iteration. However, with a do while loop, you are guaranteed at least one.

Assuming that's not a problem, the for loop statement could be this:

for (count = 0; count < NUMBER; count++)

count = 0 may need to be changed to count = 1 or whatever count is originally supposed to start at.


However, seeing as how count would need to be initialized for your current code, you can just omit the initialization part in your for loop like so:

 for (; count < NUMBER; count++)
Sign up to request clarification or add additional context in comments.

1 Comment

You can also just do for (; count < NUMBER; count++) if the initial value is defined before the loop.
1

Make use of the Java Tutorials and Java Docs; you look to have a grip on the syntax and the meaning of each part of the loop so it shouldn't be too hard to figure out how to convert it once you read this.

(source)http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
    statement(s);
}

When using this version of the for statement, keep in mind that:

The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Comments

1

The do-while loop does not check the condition on the first iteration, but a for loop does. Therefore, you need to make condition in for loop always true at the first time.

Try this:

for (boolean keepWorking = true; keepWorking; keepWorking = ++count < NUMBER) {
   // ...
}

2 Comments

Why not just for (count = X; count < NUMBER, count ++)? where X is whatever initial value; it's not clear from the question You could even just do for (; count < NUMBER; count++) if the initial value is defined before the loop.
@ADTC: First, you might be wrong in for loop statements. As I explained, a for loop check condition on the first iteration, so count < NUMBER may cause it terminated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.