CSC 161 Grinnell College Spring, 2012
 
Imperative Problem Solving and Data Structures
 
 

counting-loop.c: A basic program about loops

As a basic program about loops, consider the following code that prints the numbers from one to ten, with each number on a new line. Commentary after the program explains each element of this program.


/*
 * Program using a simple loop that prints the numbers from 1 to 10.
 */
#include <stdio.h>

int
main()
{
  int i;

  printf("Program to print the numbers from 1 to 10.\n");

  for (i = 1; i < 11; i++)
    {
      printf("%d\n", i);
    }

  return 0;
}


Commentary on the counting-loop.c program

A for loop is composed of an initializing statement, and a body of actions. The initializing statement has three components.


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp12/modules/cond-loops-testing/counting-loop-annotated.shmtml

created 28 July 2011 by April O'Neill
last full revision 29 July 2011 by April O'Neill
minor editing 26 September 2011 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.