| |
Boolean Identifiers | page 13 of 15 |
The execution of if -else statements depends on the value of the Boolean expression. We can use boolean variables to write code that is easier to read.
For example, the boolean variable done could be used to write code that is more English-like.
Instead of
if (done == true)
System.out.println("We are done!");
we can write
if (done)
System.out.println("We are done!");
Using Boolean identifiers with conditional loops allows a separation of solving expressions from thinking about program control. Here is an example solution using the while control structure (to be covered in the next lesson), presented in a blend of Java and pseudocode:
boolean done = false;
while (!done)
// do some code that could change the value of done
Where appropriate you are encouraged to use boolean variables to aid in program flow and readability.
|