Skip to main content
As you saw in Counting with a While Loop, a while loop can be used to to make something happen an exact number of times.However, this isn’t our best choice. while loops are designed to keep going as long as something is true. But if we know in advance how many times we want to do something, Java has a special kind of loop designed just for making a variable change values: the for loop.for loops are best when we know in advance how many times we want to do something.
  • Do this ten times.
  • Do this five times.
  • Pick a random number, and do it that many times.
  • Take this list of items, and do it one time for each item in the list.
On the other hand, while loops are best for repeating as long as something is true:
  • Keep going as long as they haven’t guessed it.
  • Keep going as long as you haven’t got doubles.
  • Keep going as long as they keep typing in a negative number.
  • Keep going as long as they haven’t typed in a zero.
Sample Output
Type in a message, and I'll display it five times.
Message: Hey, hey.
1. Hey, hey.
2. Hey, hey.
3. Hey, hey.
4. Hey, hey.
5. Hey, hey.
Write a program that prints the important phrase “Mr. Mitchell is cool.” on the screen ten times. Use a for loop to do it.Sample Output
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Mr. Mitchell is cool.
Write a program that gets an integer from the user. Count from 0 to that number. Use a for loop to do it.Sample Output
Count to: 19
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Write a program that gets three integers from the user. Count from the first number to the second number in increments of the third number. Use a for loop to do it.Sample Output
Count from: 4
Count to  : 13
Count by  : 3

4 7 10 13
Write a program that uses a for loop. With the loop, make the variable x go from -10 to 10, counting by 0.5. (This means that x can’t be an int.)Sample Output
x
------
-10.0
-9.5
-9.0
-8.5
-8.0
...
9.0
9.5
10.0
Write another program that uses a for loop. With the loop, make the variable x go from -10 to 10, counting by 0.5. (This means that x can’t be an int.)Inside the body of the loop, make another variable y become the current value of x squared. Then display the current values of both x and y.To get your output to line up like mine, use a tab.Sample Output
x      y
-----------------
-10.0   100.00
-9.5    90.25
-9.0    81.00
-8.5
-8.0    64.00
...
9.0     81.00
9.5     90.25
10.0    100.00
Write a program that uses a for loop to display all the numbers from 1 to 20, marking those which are even (divisible by two). It should use modulus by 2: if the remainder is zero, it’s divisible by 2.This means you’ll need an if statement inside the loop.
for ( <stuff> )
{
    if ( <something with modulus> )
    {
        System.out.println( <something> );
    }
    else
    {
        System.out.println( <something different> );
    }
}
Sample Output
1
2 <
3
4 <
5
6 <
7
8 <
9
10 <
11
12 <
13
14 <
15
16 <
17
18 <
19
20 <
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.The output of your program will look something like this:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
...
97
98
Fizz
Buzz
Did you know that using a loop, you can examine a String one letter at a time? The two key built-in String methods are length() and charAt().
  • length() returns an int representing the total number of characters in the String (including punctuation and whitespace). For example, if the variable str contains the String "hello", then str.length() will return 5.
  • charAt( int n ) returns the nth character (char) in the String. The character positions are zero-based, so if the variable str contains the String "ligature", then str.charAt(0) will return 'l', and str.charAt(4) will return 't'.
Files NeededSample Output
What is your message? A man, a plan, a canal: Panama!

Your message is 31 characters long.
The first character is at position 0 and is 'A'.
The last character is at position 30 and is '!'.

Here are all the characters, one at a time:

    0 - 'A'
    1 - ' '
    2 - 'm'
    3 - 'a'
    4 - 'n'
    5 - ','
    6 - ' '
    7 - 'a'
    8 - ' '
    9 - 'p'
    10 - 'l'
    11 - 'a'
    12 - 'n'
    13 - ','
    14 - ' '
    15 - 'a'
    16 - ' '
    17 - 'c'
    18 - 'a'
    19 - 'n'
    20 - 'a'
    21 - 'l'
    22 - ':'
    23 - ' '
    24 - 'P'
    25 - 'a'
    26 - 'n'
    27 - 'a'
    28 - 'm'
    29 - 'a'
    30 - '!'

Your message contains the letter 'a' 10 times. Isn't that interesting?
Get a blank sheet of paper (scratch paper is fine) and something to write with. Tell Mr. Mitchell you’d like to take the For Loop Challenge. He will ask you to write an arbitrary for loop on the sheet of paper. If you do it without any mistakes, you will receive full points.You may take this challenge more than once, but you’ll receive 5 fewer points each time you do it. The for loop you must write will be different each time.Here’s an example of what you’ll be asked to do:     ~~~~~“Write a for loop that makes the variable j go from 15 to 30, counting by 3s.”
Write a program that gets an integer from the user. Add up all the numbers from 1 to that number, and display the total. Use a for loop to do it.You have done something like this before.Sample Output
Number: 5

1 2 3 4 5
The sum is 15.
Just a short program to refresh your memory about how to program. Write a program that prompts the user for a name. Then display that name ten times. You must use a loop. If the name given is “Mitchell”, display it only five times.Sample Output
What is your name: gump

gump
gump
gump
gump
gump
gump
gump
gump
gump
gump
This assignment is almost the same as A Refresher.Write a program that prompts the user for a name. Then display that name ten times using a loop. However, if the name given is “Mitchell”, display it only five times.So here’s the challenge: write the program using only one if statement (no else) and one for loop.Sample Output
What is your name: gump

gump
gump
gump
gump
gump
gump
gump
gump
gump
gump
Write a program to calculate the multiples of a given number. Have the user enter a number, and then use a for loop to display all the multiples of that number from 1 to 12. It is not necessary to use a function.You must use a for loop.Sample Output
Choose a number: 7

7x1 = 7
7x2 = 14
7x3 = 21
7x4 = 28
7x5 = 35
7x6 = 42
7x7 = 49
7x8 = 56
7x9 = 63
7x10 = 70
7x11 = 77
7x12 = 84