Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While Loop Works with || but not &&, Not Understanding Why?

The program's goal is to roll two dice until both of them equal 6. When I used && in the while statement it stops after one dice equals 6. Why is that? Shouldn't && test the first condition then if it is correct test the second?

while ((diceOne != 6) || (diceTwo != 6)) {
    diceOne = numberGeneration.Next(1, 7);
    diceTwo = numberGeneration.Next(1, 7);
    attempt = ++attempt;

    Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
    Console.WriteLine("Press any button to continue");
    Console.ReadKey();

}

Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
like image 839
Aaron C. Avatar asked Feb 17 '21 14:02

Aaron C.


People also ask

Why is my while loop not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

Can you use || in for loop?

Suppose in a while loop, you have two conditions, and any one needs to be true to proceed to the body, then in that case you can use the || operator between those two conditions, and in case you want both to be true, you can use && operator. By using if statements and logical operators such as &&, 11,!

How does while loop work?

A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10".

Can a while statement have two conditions?

The while statement now contains two conditional expressions. The first checks to see if count is less than a , and the second checks to see if count is less than b . The logical operator and combines these two conditional expressions so that the loop body will only execute if both are true.


3 Answers

It stops because you break the condition of one dice being different from 6.

while ((diceOne != 6) && (diceTwo != 6))

While will execute while both conditions are true.

like image 66
Pedro Avatar answered Oct 16 '22 16:10

Pedro


I agree with @Pedro, also you can use break statement in the while(true) loop and stop the loop when both of dices are equal to 6:

while (true) {
    diceOne = numberGeneration.Next(1, 7);
    diceTwo = numberGeneration.Next(1, 7);
    attempt = ++attempt;

    Console.WriteLine("Your first dice is: " + diceOne + " your second dice is: " + diceTwo);
    Console.WriteLine("Press any button to continue");
    Console.ReadKey();
    if (diceOne == 6 && diceTwo == 6) break;

}

Console.WriteLine("It took you " + attempt);
Console.WriteLine("Press any key to continue");
Console.ReadKey();
like image 41
godot Avatar answered Oct 16 '22 16:10

godot


Note that the while loop condition is the necessary and sufficient condition to continue the loop. (diceOne != 6) && (diceTwo != 6)) says that "both dice are not 6". When both dice are not 6, you certainly want to continue the loop, so it's a sufficient condition, but this is not a necessary condition. When else would you want to continue the loop? When one dice is 6 and one dice isn't!

The necessary and sufficient condition to continue the loop is that at least one dice is not 6, which is what the || expresses.

like image 2
Sweeper Avatar answered Oct 16 '22 16:10

Sweeper