Computer Science 1621
Computer Science I

Laboratory Assignment 9
Loops
Due at the end of Lab

Introduction

The ability to repeat (iterate) a piece of code is a key aspect of any programming language. C provides three types of loop statements that allow us to explicitly repeat portions of code: the while statement, the do-while statement, and the for statement. Each of these loops causes a statement to be repeated until a termination condition becomes false. One defining aspect of these loops is whether the loop is a pre-test loop (while, for) in that the termination condition is checked before the statement is executed or if the loop is a post-test loop (do-while) in that the termination condition is checked after the statement is executed. A key different between pre- and post-test loops is that a post-test loop always executes at least once while pre-test loops may never execute the statement in the loop. Another key difference between the loops is that the for loop provides explicit parts of the loop that are used to do initialization statements before the loop starts and update statements to be executed after the statement in the loop. In the while and do-while loops initialization must come before the loop begins and and updating must occur as part of the statement in the loop.

Uses of loops

Some examples of how loops are used in programs:

The key in setting up a loop is generally determining an appropriate termination condition. Once we know how to stop the loop this will generally tell us how to initialize the loop and how to update it.

The Program

The following program performs a very simple task, it prints a simple triangle made up of star characters, where the number of lines in the diagram depends on a value entered by the user. We are going to update this program so that the data value entered by the user is checked, we are then going to print a slightly different picture, and then we are going to make the program repeat until a 0 is entered.


#include <stdio.h>

int main() {
  int I;
  int J;
  int NumStars;

  printf("Maximum # of stars in picture: ");
  scanf("%d",&NumStars);
  printf("\n");

  for (I = 1; I <= NumStars; I++) {
    for (J = 0; J < I; J++)
      printf("*");
    printf("\n");
  }

  return 0;
}

What to do

The data when entered is not checked. Add a loop that checks the data entered by the user and prompts them again if the data they entered is invalid. The # of stars entered will be considered valid if the number they enter is between 0 and 10 and invalid otherwise. If the data is invalid, you should print a message "Please enter a number between 0 and 10" and then prompt for a new value, repeating this process until the value entered is valid.

Next, change the program so that it doesn't print a triangle. Instead have the program print a right-pointing arrow, where the size of the arrow depends on the value the user enters. For example, if the # of stars entered is 6 the program should print:

*
**
***
****
*****
******
*****
****
***
**
*
If the number of stars entered is 4 the program should print:
*
**
***
****
***
**
*

Finally, rewrite the program so that it repeats the entire process of prompting the user and printing an arrow until the user enters the value 0 for the size of the arrow. You should put the body of the program into a loop that terminates when the value entered by the user is 0.

What to turn in

Turn in a hard copy of your final program. Also, turn in sample output showing your code checking the data entered by the user, printing out different sized outputs, and repeating until a 0 is entered.