# Help with loops!

• 10-12-2009, 08:14 PM
jrelvi23
Help with loops!
I am creating a craps game with java. I am having a little problem identifying with the different loops options that we in the class have to follow using the for loop or the while loop. I have most of the code done. It is just a matter of knowing what each loop will do for me.

Code:

```import java.util.Scanner; public class CrapsGame {   public static void main(String[] args) {     // Generate a two random numbers between 1 - 6   int genNum1 = 1 + (int)(Math.random() * 6);   int genNum2 = 1 + (int)(Math.random() * 6);   int point = 0;   int sum = 7;     System.out.println("The two generated numbers are " + genNum1 + " and " + genNum2);       if (genNum1 + genNum2 == 7 || genNum1 + genNum2 == 11) {   System.out.println("You win!\n");   Scanner input = new Scanner(System.in);   System.out.print("Do you want to play again? Y/N: ");     }           if (genNum1 + genNum2 == 2 || genNum1 + genNum2 == 3 ||   genNum1 + genNum2 == 12) {   System.out.println("!!!!CRAPS!!!! YOU LOSE!");   }     else                   if (genNum1 + genNum2 > 0) {       point = (genNum1 + genNum2);       System.out.println(point);       Scanner input = new Scanner(System.in);       System.out.print("POINT! Role Again!! Press 1: ");       int enter = input.nextInt();       }                       int genAnotherNum1 = 1 + (int)(Math.random() * 6);       int genAnotherNum2 = 1 + (int)(Math.random() * 6);                       if (genAnotherNum1 + genAnotherNum2 == point) {       System.out.println(genAnotherNum1 + genAnotherNum2);       System.out.println("You win!\n");       }                               if (genAnotherNum1 + genAnotherNum2 == 7){       System.out.println(genAnotherNum1 + genAnotherNum2);       System.out.println("!!!!CRAPS!!!! YOU LOSE!");                      }                               else       System.out.print("Please Roll Again! ");   }  }```
Again, any help will be appreciated!!
• 10-12-2009, 08:35 PM
Fubarable
I added code tags to aid with readability, but your code's indentation is poor making it still very difficult to read which makes it difficult for us to help you. Consider reposting your code as I think it may get you more replies. Much luck.
• 10-12-2009, 08:46 PM
jrelvi23
How's that? I didn't know to add code tags so thanks for that!
• 10-12-2009, 08:52 PM
Fubarable
Also, regarding which loop to use, do you know which loop should be used here:
You know that you'll need to perform a loop 4 times

vs. here:
You don't know in advance how many times that the loop will loop and it will depend on the user's responses.

?
• 10-12-2009, 08:56 PM
jrelvi23
I'm not sure what you are asking... but there is no limit as to how many times the user will play. It could be going non-stop.
• 10-12-2009, 09:05 PM
Fubarable
I'm just trying to see if you understand which loop should be used in which situation. So which loop should be used in my first example, for or while? And the same for the second?
• 10-13-2009, 11:19 PM
jrelvi23
I do know that a while loop and a for lop is checked before the loop body is executed while the do-while loop would be used as a post-test loop.

I would think that the majority of the code will be under a while loop and where it test for point... would be a do while loop. Am I close? I updated my code btw. I think it works better than the previous one.

Code:

```import java.util.Scanner; public class CrapsGame {   public static void main(String[] args) {     // Generate a two random numbers between 1 - 6   int genNum1 = 1 + (int)(Math.random() * 6);   int genNum2 = 1 + (int)(Math.random() * 6);   int point;     Scanner input = new Scanner(System.in);     System.out.println("The two generated numbers are " + genNum1 + " and " + genNum2);       if (genNum1 + genNum2 == 7 || genNum1 + genNum2 == 11) {     System.out.println("You win!\n");     }             if (genNum1 + genNum2 == 2 || genNum1 + genNum2 == 3 ||     genNum1 + genNum2 == 12) {     System.out.println("!!!!CRAPS!!!! YOU LOSE!");     }         if (genNum1 + genNum2 == 4 || genNum1 + genNum2 == 5 ||     genNum1 + genNum2 == 6 || genNum1 + genNum2 == 8 || genNum1 + genNum2 == 9 ||     genNum1 + genNum2 == 10) {     System.out.print("POINT! Role Again!! Press 1: ");     point = input.nextInt();               int genAnotherNum1 = 1 + (int)(Math.random() * 6);       int genAnotherNum2 = 1 + (int)(Math.random() * 6);                       if (genAnotherNum1 + genAnotherNum2 == point) {       System.out.println(genAnotherNum1 + genAnotherNum2);       System.out.println("You win!\n");       }                               if (genAnotherNum1 + genAnotherNum2 == 7){       System.out.println(genAnotherNum1 + genAnotherNum2);       System.out.println("!!!!CRAPS!!!! YOU LOSE!");                        }                                               else         System.out.print("Please Roll Again! ");     }            }        }```
• 10-13-2009, 11:52 PM
Fubarable
My understanding about these loops is this:

for loop: when you know in advance how many times the loop will occur. An example includes if you want to loop through an array and print each item in the array, you will know that this will loop exactly array.length times.

while loop: when you don't know in advance how many times the loop should occur and want to check this out at the beginning of the loop. This can loop 0 or more times.

do-while loop: when you don't know in advance how many times the loop should occur, and you want to check this out at the end of the loop. This loop will always occur at least one time if not more.

So given this, should you use a for loop here or one of the while loops? After deciding, why not try to place a loop in your code?
• 09-04-2012, 11:41 AM
hardcoder
Re: Help with loops!
HI ,