The general form of while loop is-
Initialize loop counter;
while(test loop counter using a condition){
do this;
and this;
Increment loop counter;
}
In this the loop continues until the condition becomes false.
There must be a case where the condition becomes false,otherwise the loop will turn into an infinite loop.
The statements in the loop can be a single statement or block of statements.
Instead of incrementing the loop counter,we can also decrement the loop counter.
Example:
Note: The increment operator ++ increments the value of variable by 1,every time the statement is executed.
Similarly,the decrement operator - - decrements the value of variable by 1,every time the statement is executed.
+= is the compound assignment operator.It also increments the value by 1.
Other compound assignment operators are -= ; *= ; /= ; %=.