Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.
A for statement can be a more convenient way to write certain types of loops. A common use for the for statement is loops where you know the number of iterations before the loop starts.
To write a loop that outputs 100 *'s, you could use a for statement that outputs 1 '*' for each iteration of the loop like the following:
for (int count = 0; count < 100; count++)
{
cout << '*';
}
The variable count is a counting variable. It is used to count the 100 iterations of the loop. If you had used a while loop, you would have had to initialize the counting variable, count, before the loop and then incremented it (count++) within the loop. The for statement takes care of these actions all at once.
Let's look at each part of the for statement. After the keyword, for, there are 3 separate sections within the parenthesis. Each section is separated by a semicolon (;).
YOUR TURN: Rewrite this loop so that it outputs only 20 *'s.
Another common use of the for statement is loops where you use the controlling variable within the loop. For example, if you wanted to output all integers from 0 to 10, you could write the following for loop. Notice that the controlling expression uses "<=" here so that the 10 is output as well. This loop will actually iterate 11 times to output the integers 0 through 10.
for (int i = 0; i <= 10; i++)
{
cout << i << " ";
}
So far, we have always initialized our controlling variable to 0. If we wanted to output the integers from 1 to 10, we could just change the initialization section to int i = 1;.
YOUR TURN: Write a for statement to output all the integers between and including 10 and 20.
Let's use a for statement to only output the even numbers between 0 and 10. We could just change the update section of the for statement.
for (int i = 0; i <= 10; i = i + 2)
{
cout << i << " ";
}
This loop initializes the controlling variable, i, to 0 and then increments by 2 instead of 1, (i = i + 2).
YOUR TURN: Write a for statement that outputs all the odd numbers between 0 and 10. Hint: Just change the intialization of i in the example given.
YOUR TURN 2: Now, write a for statement that outputs all the multiples of 3 between 0 and 100. Hint: Just change the update amount in the example given and, of course, the controlling expression.
YOUR TURN 3: Now, write a for statement that outputs all the even integers between 0 and 10 but backwards. In other words, output 10 first, then 8, etc.
So far, we have always initialized our controlling or counting variable to a constant. You can also initialize it to the value of another variable. The following example shows a loop that iterates between the value of num1 and num2:
for (int i = num1; i <= num2; i++)
{
cout << i << " ";
}
Write a program that uses a for statement to calculate the sum of all integers between 2 integers entered by the user. You may ask the user to input the smaller of the 2 numbers first. You should output all the integers and their sum.
Write a program that uses a for statement to get 10 values from the user. Then, output the maximum and minimum of these 10 values and then the average value.
Hint: Use the loop only for the 2nd - 10th numbers. Initialize max and min to the first number you get from the user then enter the loop.
Modify your program from part 5 to also output when the maximum and minimum values were entered. For example, if the user entered the values in this order, 4 9 46 82 3 56 2 55 23 8, then your program should output something like this:
Maximum: 82 (Entry 4)Hint: Use variables that get updated with the current iteration number whenever you change the value of max and min.