count = count + 1;
It increases count
by one.
If this is not clear remember the two
steps of an assignment statement:
first evaluate the expression on
the right of the equal sign,
second put the value in whatever variable
is on the left.
Say that count
is 5.
When the above statement executes,
first the expression is evaluated, resulting in 6.
Second the 6 is put
in count
.
while
Loop WorksHere the part of the program responsible for the loop:
int count = 1; // start count out at one while ( count <= 3 ) // loop while count is <= 3 { System.out.println( "count is:" + count ); count = count + 1; // add one to count } System.out.println( "Done with the loop" );
Here is how it works:
count
is assigned a 1.( count <= 3 )
is evaluated as true.count
is written out: count is 1count
is incremented by one, to 2.( count <= 3 )
is evaluated as true.count
is written out. count is 2count
is incremented by one, to 3.( count <= 3 )
is evaluated as true.count
is written out. count is 3count
is incremented by one, to 4.( count <= 3 )
is evaluated as FALSE.