Chapter 6: Loops
Chapter 6: Loops
Chapter 6
Loops
Iteration Statements
• C’s iteration statements are used to set up loops.
• A loop is a statement whose job is to repeatedly
execute some other statement (the loop body).
• In C, every loop has a controlling expression.
• Each time the loop body is executed (an iteration
of the loop), the controlling expression is
evaluated.
– If the expression is true (has a value that’s not zero) the
loop continues to execute.
Iteration Statements
• C provides three iteration statements:
– The while statement is used for loops whose
controlling expression is tested before the loop body is
executed.
– The do statement is used if the expression is tested
after the loop body is executed.
– The for statement is convenient for loops that
increment or decrement a counting variable.
Infinite Loops
• A while statement won’t terminate if the controlling
expression always has a nonzero value.
• C programmers sometimes deliberately create an
infinite loop by using a nonzero constant as the
controlling expression:
while (1) …
• A while statement of this form will execute forever
unless its body contains a statement that transfers
control out of the loop (break, goto, return) or
calls a function that causes the program to terminate.
square.c
/* Prints a table of squares using a while statement */
#include <stdio.h>
int main(void)
{
int i, n;
i = 1;
while (i <= n) {
printf("%10d%10d\n", i, i * i);
i++;
}
return 0;
}
12 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops
sum.c
/* Sums a series of numbers */
#include <stdio.h>
int main(void)
{
int n, sum = 0;
scanf("%d", &n);
while (n != 0) {
sum += n;
scanf("%d", &n);
}
printf("The sum is: %d\n", sum);
return 0;
}
14 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops
The do Statement
• General form of the do statement:
do statement while ( expression ) ;
• When a do statement is executed, the loop body is
executed first, then the controlling expression is
evaluated.
• If the value of the expression is nonzero, the loop
body is executed again and then the expression is
evaluated once more.
The do Statement
• The countdown example rewritten as a do
statement:
i = 10;
do {
printf("T minus %d and counting\n", i);
--i;
} while (i > 0);
• The do statement is often indistinguishable from the
while statement.
• The only difference is that the body of a do
statement is always executed at least once.
16 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops
The do Statement
• It’s a good idea to use braces in all do statements,
whether or not they’re needed, because a do
statement without braces can easily be mistaken
for a while statement:
do
printf("T minus %d and counting\n", i--);
while (i > 0);
• A careless reader might think that the word
while was the beginning of a while statement.
numdigits.c
/* Calculates the number of digits in an integer */
#include <stdio.h>
int main(void)
{
int digits = 0, n;
do {
n /= 10;
digits++;
} while (n > 0);
return 0;
}
19 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops
square2.c
/* Prints a table of squares using a for statement */
#include <stdio.h>
int main(void)
{
int i, n;
return 0;
}
square3.c
/* Prints a table of squares using an odd method */
#include <stdio.h>
int main(void)
{
int i, n, odd, square;
i = 1;
odd = 3;
for (square = 1; i <= n; odd += 2) {
printf("%10d%10d\n", i, square);
++i;
square += odd;
}
return 0;
}
40 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops
Enter command: 1
Enter amount of credit: 1042.56
Enter command: 2
Enter amount of debit: 133.79
Enter command: 1
Enter amount of credit: 1754.32
Enter command: 2
Enter amount of debit: 1400
Enter command: 2
Enter amount of debit: 68
Enter command: 2
Enter amount of debit: 50
Enter command: 3
Current balance: $1145.09
Enter command: 4
checking.c
/* Balances a checkbook */
#include <stdio.h>
int main(void)
{
int cmd;
float balance = 0.0f, credit, debit;
case 1:
printf("Enter amount of credit: ");
scanf("%f", &credit);
balance += credit;
break;
case 2:
printf("Enter amount of debit: ");
scanf("%f", &debit);
balance -= debit;
break;
case 3:
printf("Current balance: $%.2f\n", balance);
break;
case 4:
return 0;
default:
printf("Commands: 0=clear, 1=credit, 2=debit, ");
printf("3=balance, 4=exit\n\n");
break;
}
}
}
58 Copyright © 2008 W. W. Norton & Company.
All rights reserved.
Chapter 6: Loops