System.out.println("Press C or c to continue.");
ch = System.in.read();
Thisexamplebeginsbyinitializingvariable ch .Thisvariablemustbeinitialized;oth-
erwise,thecompilerwillreportanuninitializedvariablewhenittriestoread ch 'svalue
in the while statement's Boolean expression.
ThisexpressionusestheconditionalANDoperator( && )totest ch 'svalue.Thisoper-
atorfirstevaluatesitsleftoperand,whichhappenstobeexpression ch != 'C' .(The
!= operator converts 'C' from 16-bit unsigned char type to 32-bit signed int type
prior to the comparison.)
If ch does not contain C (it does not at this point— 0 was just assigned to ch ), this
expression evaluates to true.
The && operatornextevaluatesitsrightoperand,whichhappenstobeexpression ch
!= 'c' .Becausethisexpressionalsoevaluatestotrue,conditionalANDreturnstrue
and while executes the compound statement.
Thecompoundstatementfirstoutputs,viathe System.out.println() method
nextreadstheenteredkeystrokevia System.in.read() ,savingitsintegervaluein
Fromlefttoright, System identifiesastandardclassofsystemutilities, in identifies
anobjectlocatedin System thatprovidesmethodsforinputtingoneormorebytesfrom
the standard input device, and read() returns the next byte (or -1 when there are no
more bytes) .
Following this assignment, the compound statement ends and while re-evaluates its
Suppose ch contains C's integer value. Conditional AND evaluates ch != 'C' ,
which evaluates to false. Seeing that the expression is already false, conditional AND
short circuits its evaluation by not evaluating its right operand, and returns false. The
while statement subsequently detects this value and terminates.
Suppose ch contains c's integer value. Conditional AND evaluates ch != 'C' ,
which evaluates to true. Seeing that the expression is true, conditional AND evaluates
ch != 'c' , which evaluates to false. Once again, the while statement terminates.
Note A for statement can be coded as a while statement. For example,