for
statement.
Note that the for loop control statements are separated by semicolons and that the body of a for loop can have either a simple or compound statement. . I will be using the JCreator IDE in developing the program.
To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
import java.util.*; public class forLooping { { int counter; //loop control variable int sum; //variable to store the sum of the numbers int n; //first positive integer to be added + "integers to be added: "); n = concole.nextInt(); sum=0; for(counter = 1; counter <=n; counter ++) sum = sum + counter; + n + " positive integers is " + sum); } }
Enter the number of positive integers to be added: 100 The sum of the first 100 positive integers is 5050
+ "integers to be added: ");
n = concole.nextInt();
stores the number entered by the user in n and the statement sum=0;
initializes sum to 0.
The statement
for(counter = 1; counter <=n; counter ++) sum = sum + counter;