Revision Exercises - Iteration, part 1

These revision exercises are provided to give you some extra practice with the basic programming concepts covered in the first semester, especially those that people had difficulty with on the test. The first exercise set covers iteration: for, while and do loops.

Solutions to the exercises available here.


1. More triangles, and some other shapes too

Write programs to produce the following output. You should use for loops only for any iteration that is required. In addition, each program should prompt the user to enter an integer value, and use this value to determine the `size' of the pattern that is drawn. It is up to you to determine exactly how the value entered relates to the size of the pattern.
 
1.
*
**
***
****
*****
****
***
**
*
2.
*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *
3.
* ******* *
** ***** **
*** *** ***
**** * ****
***** *****
**** * ****
*** *** ***
** ***** **
* ******* *
4.
**        **
 **      **
  **    **
   **  **
    ****
     **
    ****
   **  **
  **    **
 **      **
**        **
5.
*
**
***
*****
********
*************
*********************
**********************************
Hint: look at the difference in the lengths of consecutive lines
6.
***   ***   ***
***   ***   ***
***   ***   ***
   ***   ***   ***
   ***   ***   ***
   ***   ***   ***
***   ***   ***
***   ***   ***
***   ***   ***
   ***   ***   ***
   ***   ***   ***
   ***   ***   ***
For this question the user should be able to specify both the size of the small squares and the size of the large grid.
7.
***********
***** *****
****   ****
***     ***
**       **
*         *
**       **
***     ***
****   ****
***** *****
***********
8.
*************
****** ******
***** #  ****
**** ### ****
*** ##### ***
** ####### **
* ######### *
** ####### **
*** ##### ***
**** ### ****
***** # *****
******  *****
*************
9. Combine the patterns for questions 6 and 8, so that the output is a checkerboard pattern where each `black' square looks like the pattern from question 8.
10.
****          **** ****          ****
 ****        ****   ****        ****
  ****      ****     ****      ****
   ****    ****       ****    ****
    ****  ****         ****  ****
     ********           ********
The user should be able to specify the width of the ribbon as well as the height of the pattern.

2. Counting iterations

How many times is the body of each of the following loops executed? The answer is not necessarily a particular known value; it may depend on some of the other variables. You may assume that all variables are of type int unless they are declared otherwise.
 
 
11.
for (int i = 0; i < 101; i++) {
  // body
}
12.
for (int i = 1; i < 101; i++) {
  // body
}
13.
for (int i = 0; i <= 101; i++) {
  // body
}
14.
for (int i = 0; i < 101; i += 2) {
  // body
}
15.
for (int i = 1; i < 101; i += 2) {
  // body
}
16.
for (int i = 0; i < x; i++) {
  // body
}
17.
for (int i = x; i <= y; i += 2) {
  // body
}
18.
for (int i = x; i > y; i -= z) {
  // body
}
19.
for (int i = 42; i < 42; i += 3) {
  // body
}
20.
for (int i = 0; i <= 1024; i *= 2) {
  // body
}

Written by Scott Mitchell. Updated by Matthew Huntbach

These notes were produced as part of the course Introduction to Programming as it was given in the Department of Computer Science at Queen Mary, University of London during the academic years 1998-2001.


Last modified: 30 September 1999