The Iteration Structure Cont.
Let's look at some more examples of the iteration structure. Let's write
a program that asks the user for two integers, then prints a list of
all the integers between those two:
#include <stdio.h>
#include <stdlib.h>
int main () {
int i, start, end;
/* prompt for and read lower and upper limits */
printf ("Enter two integers: ");
scanf ("%i %i", &start, &end);
/* complain if end < start */
if (end < start) {
printf ("No integers between %i and %i!\n", start, end);
exit (1);
}
/* i starts out at 'start' */
i = start;
/* continue printing and incrementing i until it reaches 'end' */
while (i <= end) {
printf ("%i\n", i);
i++;
}
exit (0);
}
The for Control Structure
while is the only iteration structure you'll ever need in C.
However, a certain kind of loop that has an initialization, simple comparison
in the while, and end-of-loop operation like an increment
occurs frequently in programming, so C provides a shorthand in the form
of the for control structure. It looks like this:
for ( expr1; expr2; expr3 )
stmt;
expr1 is done once, right before the loop begins. expr2
is evaluated at the beginning of the loop; the loop terminates when
expr2 becomes false, just like in a while loop.
expr3 is evaluated at the end of each iteration of the loop.
It is equivalent to the following:
expr1;
while ( expr2 ) {
stmt;
expr3;
}
but shorter to write and often easier for other programmers to read.
In the previous program, for example, we can replace the code beginning
with i = start with this:
for (i=start; i<=end; i++)
printf ("%i\n", i);
This is much shorter and easier to read. Note that you can also use a
compound statement with a for, just like with while
and if.
Now let's look at a program that asks the user for an integer and then
determines whether the integer is a prime number. Recall that a prime
number is a positive integer that has exactly two factors: itself and 1.
So, for example, 13 is prime because no two numbers, other than 1
and 13, can be multiplied to yield 13. For another example,
15 is not prime since it is
the product of 3 and 5 (a number that isn't prime and isn't 1 is said
to be composite). One way to determine whether a number n is
prime is to see whether it is evenly divided by any of the integers
from 2 through n-1; if not, then it is prime. Here's the
program:
#include <stdio.h>
#include <stdlib.h>
int main () {
int i,
n,
prime; /* this will be a truth value */
/* prompt for and read integer */
printf ("Enter an integer: ");
scanf ("%d", &n);
/* start out assuming the number is prime (1 stands for "true") */
prime = 1;
/* run through all the numbers from 2..n-1 */
for (i=2; i<n; i++) {
/* if the remainder when n is divided by i is 0,
* the i is a factor of n so n can't be prime
*/
if (n % i == 0) prime = 0;
}
if (prime == 1)
printf ("%d is prime\n", n);
else
printf ("%d is not prime\n", n);
exit (0);
}
Nested Loops
Let's extend this work and get a program that prints out the
prime numbers between 2 and 100. We'll have to have a loop inside
another loop, a concept known as as nested loops. We'll run
through all the integers from 2 through 100, testing each for primality.
Each time we find a prime number, we'll print it:
#include <stdio.h>
#include <stdlib.h>
int main () {
int i,
n,
prime; /* this will be a truth value */
/* let n run through all the numbers from 2..100 */
for (n=2; n<=100; n++) {
/* start out assuming n is prime */
prime = 1;
/* run through all the numbers from 2..n-1 */
for (i=2; i<n; i++) {
/* if the remainder when n is divided by i is 0,
* the i is a factor of n so n can't be prime
*/
if (n % i == 0) prime = 0;
}
/* if we still think that n is prime, print it out */
if (prime == 1)
printf ("%i\n", n);
}
exit (0);
}