Study Materials - for loops

Introduction

Keep in mind that any loop written with a while construct can also be written using a for loop construct. To that end, is recommended that you re-write the exercises found in the while Loop Programs section (found under the Study Materials menu category) using for loops in place of while loops. In each exercise, the program is to prompt the user, asking how many integers will be entered (note: a specific number of items will be entered, this being determined by the user). This value, n, will be used in the second (middle) statement of the for loop (for example: for (i = 0; i < n; i++) .


Group 1

Think of how you would write the following program using a for loop. Note: you may not expect the user to enter all valid values (in other words, more values than specified may need to be entered, if the user enters in invalid value):

Program: Write a JAVA program that prompts the user to enter five odd integers. The program is to output each of the odd integers (each on a separate line). After the fifth odd integer is entered, the program will output the sum of the 5 odd integers.


Group 2
  1. Write a program using a for loop that will output one row of seven characters (character being a star = ‘*’).
  2. Write a program using for loops that will output three rows of seven characters each (seven characters on each row), (character being a star = ‘*’).

Group 3

Write a JAVA program that will generate a multiplication table. The user is to prompted to input the number of rows and columns for the table. As output program is to generate and output the multiplication table based on the row/column parameters entered by the user.

  • The program is to include the following:
    • appropriate prompts for the user to enter row and column parameters;
    • two for loops to generate the table;
    • an output statement to display the table.

Sample Output:

        Please enter the number of the multiplication rows (number of lines):
        sample input: 3
        Please enter the number of the multiplication columns (number of elements in a row):
        sample input: 5

        1    2    3    4     5 
        2    4    6    8     10
        3    6    9    12    15

Group 4
  1. Write a JAVA program that prompts the user to enter a positive value, n. Using a for loop, output all values in ascending order from 1 to n, inclusive, all on the same line.
  2. Write a JAVA program that prompts the user to enter a positive value, n. Using a for loop, output all values in ascending order from 1 to n, inclusive, each value on a separate line.
  3. Write a JAVA program that prompts the user to enter a positive value, n. Using a for loop, output all values in descending order from n to 1, inclusive, all on the same line.
  4. Write a JAVA program that prompts the user to enter a positive value, n. Using a for loop, output all values in descending order from n to 1, inclusive, each value on a separate line.
  5. Re-write examples 6 – 9 where you do not know the value of n. Here, n may be positive or negative. In other words, n may be greater than, less than or equal to 1. In addition to the for loop required for output, you must now also test the value of n, and write output options for each case (ex. n >= 1, n <= 1).