Search code examples
javawhile-loopboolean

Why don't we have to use two '=' operands for mentioning equality for boolean variable?


When I coding in a while loop in java , I discovered that we don't have to use two assignment operands for give meaning equality for JUST BOOLEAN VARİABLE(I found just it). What is the reason behind this situation? Thank you from now

` int a=30; boolean d=true;

  while (a==30) {   <- We need to use like that for other variables
       
     
    }`

` int a=30; boolean d=true;

  while (d=false) {   <- We need to use like that for other variables
       
     
    }`

Solution

  • Because assignment resolves to the assigned value. That sets d to false (and evaluates to false) and thus the while does not enter the loop body.

    while (d=false) {
    

    And you don't need any = for boolean(s).

    while (!d) {
    

    or

    while (d) {
    

    are correct, and don't involve assignment.