while loop in Java

while loop is similar to the for loop but does not include initialization and increment in loop part.

General syntax

while(condition or expression) {

//statements or code to be executed

}

Example

Imagine you as a money lender. You have got today some amount of interest from various people.

You want to add the interest. What you will do this with the help of programming?

Assume interest_money from number of people as a array variable.

public class WhileLoop {

public static void main(String args[]) {

int interest[]={105,110,1200,3456,1234},sum=0;

int i=0;

while(i<5) {

sum=sum+interest[i];

i++;

}

System.out.println("Total interest is "+sum);

}

}

Try In Editor

Output

Total interest is 6105