64. Counting with a For Loop - Link
- Task
- Answer
- Question
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.
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.
65. Ten Times - Link
- Task
- Answer
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 Output66. Counting Machine - Link
- Task
- Answer
Write a program that gets an integer from the user. Count from 0 to that number. Use a
for loop to do it.Sample Output67. Counting Machine Revisited - Link
- Task
- Answer
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 Output68. Counting By Halves - Link
- Task
- Answer
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 Output69. Xs and Ys - Link
- Task
- Answer
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 Output70. Noticing Even Numbers - Link
- Task
- Answer
Write a program that uses a Sample Output
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.71. Fizz Buzz - Link
- Task
- Answer
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:
72. Letter at a Time - Link
- Task
- Answer
- Question
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 anintrepresenting the total number of characters in the String (including punctuation and whitespace). For example, if the variable str contains the String"hello", thenstr.length()will return5.charAt( int n )returns thenth character (char) in the String. The character positions are zero-based, so if the variablestrcontains the String"ligature", thenstr.charAt(0)will return'l', andstr.charAt(4)will return't'.
73. For Loop Challenge - Link
- Task
- Answer
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.”74. Adding Values in a For Loop - Link
- Task
- Answer
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 Output116. A Refresher - Link
- Task
- Answer
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
117. Refresher Challenge - Link
- Task
- Answer
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 Output118. Displaying Some Multiples - Link
- Task
- Answer
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