Home >Java >javaTutorial >While loop 1 adds to the sum of 100
The sum between 1-100, if we use ordinary addition operations, it will be very difficult. This is a waste of time, inefficient, and prone to errors. Here I use a while loop to show you, For beginners to exercise their ideas, it is still very useful to master basic programming.
First of all, we need to create a class in MyEclipse to perform calculations. When choosing a name, we must pay attention to it and be aware of the name, so that it will be easy for others. Understand what you are trying to do. (Recommended learning: Java Video Tutorial)
After it is established, we will start operating the code. If we want to perform calculations at this time, we have to define the variables we need, so that it is convenient for us to perform calculations
After defining the variables, we will start the loop. When i< ;=100, repeat the addition operation, assign the value of sum i to sum, and increment the value of i after each addition.
When we loop, we must pay special attention not to forget i; if we do not write this, it will not close, in which case we will output.
For more Java-related technical articles, please visit the Java Development Tutorial column to learn!
The above is the detailed content of While loop 1 adds to the sum of 100. For more information, please follow other related articles on the PHP Chinese website!