You are on page 1of 1

// A for loop places critical loop information (initialization, the loop test, a

nd the loop
// update) into the header, separated by semicolons. Note that, in a for loop, t
he update step
// *always* occurs after the loop body executes (whereas the order may be interm
ingled in a
// while loop).
import java.util.*;
public class ForLoopPractice {
public static void main (String [] args)
{
// Print "Hello" 10 times
for (int i = 0; i < 10; i = i + 1)
{
System.out.println("Hello");
}
// This for loop runs a user-specified number of times to add up
the numbers from
// 1 through the user-supplied number
Scanner foo = new Scanner(System.in);
System.out.print("How many times should the loop run? ");
int num = foo.nextInt();
int total = 0;
for (int counter = 1; counter <= num; counter = counter + 1)
{
total = total + counter;
}
System.out.println("Total is: " + total);
}
}

You might also like