It looks like you're new here. If you want to get involved, click one of these buttons!
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.
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.
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) { attempts++; 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.
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)
Answers
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.
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?
Yes, that is a perfect example of when to use a for loop.