Class Exercise: Repetition -- for loop
Syntax: for loop
for (initial statement; loopCondition; updateStatement) {
statement body
}
Description: A repetition structure useful for counter-controlled loops
where a counter is to be initialized and incremented or decremented to
determine the number of times a loop is repeated. The loop repeats as
long as the loopCondition is true.
1. Construct a for loop to write the first five positive integers. To control
this repetition, define variables named count, initialized to 1, and limit
initialized to 5. Specify a loop condition to test whether count is less
than or equal to limit. The update component should increment count by 1.
2. Code a for loop with statement block that prints and accumulates counter
values from 1 through 5. Declare and initialize the necessary variables
prior to the for statement. Print the sum after the for loop.
3. Prepare a for loop similar to that coded for number 2. It is to sum the even
numbers from 10 down through 2.
4. Given a variable starCount has been initialized to some positive integer,
have a for loop control the printing of that number of asterisks. During
each iteration of the loop, the output statement should print a single
asterisk.
5. Use for statement(s) to print a two-dimensional pattern of asterisks based
upon values assigned to variables named rowCount and columnCount.