Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
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();
over 4 years ago · Santiago Trujillo
4 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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();
over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

Other users already have explained you why it's not working. I may suggest to modify the while statement like this:

while (!(diceOne == 6 && diceTwo == 6))
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda