Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Please, explain why

So i was trying to make program that roll a dice and keeps looping until it rolled a six. i made the program using for loop. so at first, i made it like this

for (int rollDice = 0; rollDice != 6; rollDice++);

when i put 6 in the middle, the program stopped looping when it rolled a 5 instead of 6.

but when i put 7, the program stopped when it rolled 6.

why is that happening? doesnt the "rollDice != 6" mean that it will loop as long as rollDice is not 6?

thank you and sorry for (probably) bad english.

Best Answers

  • MouledouxMouledoux Member
    edited July 28 Accepted Answer

    Ok, so this is not how you would normally use a for loop, nor is this a good way for checking a dice roll.

    If you break down what is actually happening here, it doesn't make sense.

    1. you set the dice roll to 0
    2. then you set it to a random number
    3. if that number is not 7, you increase the dice roll by 1? what?

    This should be in a while loop because you want to keep rolling while the dice is not 6.

    int diceRoll = 0;
    int attempts = 0;
    while(diceRoll != 6)
         diceRoll = numGen.Next(1, 7);
    // print stuff here

    You would use a for loop if you wanted to do something like roll a hundred dice, and get the total. Because then you want to roll for all of the dice.

  • Accepted Answer

    No, wait, i just wanted to try it using for loop. Thank you for your answer tho, i realized i should write it like this instead :

    for (int rollDice = 0; rollDice != 6; rollDice = rollDice)


  • I guess yes you could do that, but it's still not the proper use of a for loop.

    And even so, you probably wouldn't want to make the iterator the value of the dice roll.

  • edited July 28

    alright, im new to this, i will learn about it more, thank you! have a nice day

  • If you are looking for practice with for loops though, here's one you could try.

    Roll a dice 100 times, and then print out how many times the rice rolled 1, 2, 3, 4, 5, 6, etc.

    Tip: an array might be helpful, but not necessary.

  • i haven't learned array yet, but this works, i guess?

    int rollDice = 0;
                    Random numGen = new Random();
                    int countOne = 0;
                    int countTwo = 0;
                    int countThree = 0;
                    int countFour = 0;
                    int countFive = 0;
                    int countSix = 0;
                    Console.WriteLine("Pressed a key to roll the dice!");
                    for (int x = 0; x < 100; x++)
                        rollDice = numGen.Next(1,7);
                        Console.WriteLine("You rolled " + rollDice);
                        if(rollDice == 1)
                        } else if(rollDice == 2)
                        } else if(rollDice == 3)
                        } else if(rollDice == 4)
                        } else if(rollDice == 5)
                        } else if(rollDice == 6)
                    Console.WriteLine("You rolled a one " + countOne + " Times");
                    Console.WriteLine("You rolled a two " + countTwo + " Times");
                    Console.WriteLine("You rolled a three " + countThree + " Times");
                    Console.WriteLine("You rolled a four " + countFour + " Times");
                    Console.WriteLine("You rolled a five " + countFive + " Times");
                    Console.WriteLine("You rolled a six " + countSix + " Times");
                // keeps open the thing until i pressed a key
  • Yes, that is a perfect example of when to use a for loop.

Sign In or Register to comment.