The C "for" loop
for (i = 0; i < n; i++)
{
...statements...
}
- i is the "index variable"
- i < n is the "continuation test"
- ...statements... is the "body"
- i++ is the "iterative step"
Printing a message 10 times
The Program
/*****************************************
File: do_ten.c
Author: Unknown
Date: Unknown
Section: Unknown
Email: Unknown
An example of a C 'for' loop that
prints a message ten times.
*****************************************/
#include
int main()
{
int i ;
/* Repeat 10 times */
for (i = 0 ; i < 10 ; i++)
{
printf("I will not snore in class.\n") ;
}
return 0;
}
and the sample run.
linux1[97] % gcc -Wall -ansi do_ten.c
linux1[98] % a.out
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
I will not snore in class.
linux1[99] %
Adding a list of 10 numbers
The Program
/*****************************************
** File: add10.c
** Author: Sue Bogar
** Date: 3/3/97
** Section: 101
** EMail: bogar@cs.umbc.edu
**
** This program adds a list of ten numbers,
** printing the total at the end. Instead
** of reading the numbers into separate
** variables, this program reads in each
** number and adds it to a running total.
*****************************************/
#include
int main()
{
int i, value, total;
/* Print description for user */
printf("This program adds a list of 10 numbers.\n");
/* Initialize total */
total = 0;
/* Get 10 integers from the user,
** adding each to total */
for (i = 0; i < 10; i++)
{
printf("Enter an integer : ");
scanf ("%d", &value);
total += value;
}
/* Print the result */
printf("The total is %d\n", total);
return 0;
}
and the sample run.
linux1[99] % gcc -Wall -ansi add10.c
linux1[100] % a.out
This program adds a list of 10 numbers.
Enter an integer : 1
Enter an integer : 2
Enter an integer : 3
Enter an integer : 8
Enter an integer : 21
Enter an integer : 22
Enter an integer : 1
Enter an integer : 17
Enter an integer : 19
Enter an integer : 20
The total is 114
linux1[101] %
Nested For Loops
Multiplication Tables
The Program
/*****************************************
* File: timestab.c
* Author: S Bogar
* Date: 8/3/99
* Section: 0101
* E-Mail: bogar@cs.umbc.edu
*
* Generates a multiplication table where each
* axis runs from LowerLimit to UpperLimit.
*****************************************/
#include
/*********************
* Constants
* ---------
* LOWER -- Starting value for the table
* UPPER -- Final value for the table
*********************/
#define LOWER 1
#define UPPER 10
/* Main program */
int main()
{
int i, j;
for (i = LOWER; i <= UPPER; i++)
{
for (j = LOWER; j <= UPPER; j++)
{
printf("%5d", i * j);
}
printf("\n");
}
return 0;
}
The Sample Run
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
The Lesson
- The use of #define for constants simplifies later changes.
Drawing a box
The Task
Draw a 9x5 box of *'s.
The Program
/*****************************************
Program: box.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw a box.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 9 ; j++)
{
printf("*") ;
}
printf("\n") ;
}
return 0;
}
The Sample Run
*********
*********
*********
*********
*********
Diagonal
The Task
Draw a 5x5 box of *'s but with O's on the diagonal.
The Program
/*****************************************
Program: diag.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw
a box with a diagonal of O's.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if (i == j)
{
printf("O") ;
}
else
{
printf("*") ;
}
}
printf("\n") ;
}
return 0;
}
The Sample Run
O****
*O***
**O**
***O*
****O
Triangle
The Task
Draw a triangle of *'s.
The Program
/*****************************************
Program: triangle.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw a triangle.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= i ; j++)
{
printf("*") ;
}
printf("\n") ;
}
return 0;
}
The Sample Run
*
**
***
****
*****
Another triangle
The Task
Flip the triangle.
The Program
/*****************************************
Program: triangle2.c
Author: Sue Bogar
Date: 8/2/99
Section: 0101
E-Mail: bogar@cs.umbc.edu
Using nested for loops to draw
a triangle facing the other way.
*****************************************/
#include
int main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if ( i > 5 - j)
{
printf("*") ;
}
else
{
printf(" ") ;
}
}
printf("\n") ;
}
return 0;
}
The Sample Run
*
**
***
****
*****