The use of the for loop is same as that of the while loop and it also behaves in the same manner. But the initialization, condition and increment/decrement operation is written in the same line in case of ‘for’ loop.
Syntax of for loop is:
for (initialization; condition; increment/decrement)
//To find the sum of ‘n’ numbers
sum=sum + i;
The initialization and increment fields are optional, but the semicolon signs between them should always be present. Optionally, using the comma (,) operator we can specify more than one expression in any field we want.