Transcript While Loop

CS1061: C Programming
Lecture 8: Repetition
A. O’Riordan, 2004
Iteration
The ability to iterate, i.e. to execute a block of statements repeatedly, is crucial to
prgramming languages like C.
C provides three statements for “looping”:

While statement (sentinel repetition)


Do..while statement


used when the number of iterations is not known
variant of while loop
For loop (counter-controlled repitition)

used when you know the number of iterations in advance
While Loop



Repetition structure
Programmer specifies an action to be repeated while some Boolean condition
remains true - example: While there are more items on my shopping list
purchase next item and cross it off my list.
while loop repeated until condition becomes false
The general form is:
while (expression) <statement>
The <statement> is executed repeatedly as long as expression evaluates to
true. The expression is evaluated before <statement> is executed for
the first time.
Note on syntax
The form of a statement is it’s syntax.
When giving the syntax of a statement in C we use a simple consistent notation. A statement
is represented as <statement>. This may be a single statement such as
square = number * 2;
or a compound statement such as
{
count = count + 1;
printf(“Incrementing count\n”);
}
An expression evaluates to a Boolean, either true (non-zero) or false (zero).
while loop example
/* while loop example */
#include <stdio.h>
int main(){
char letter;
printf("Enter a letter (Z to terminate):");
letter = getchar();
getchar();
while( letter != 'Z'){
printf("Next letter is %c\n", letter);
printf("Enter a letter (Z to terminate):");
letter = getchar();
getchar();
}
}
Factorial Example
The factorial function can be computing with a loop
Aide Memoire: The factorial of an n = nx(n-1)x(n-2)...x1, for example
5! = 5x4x3x2x1 = 120
/* compute factorial of integer n (i.e. n!)*/
int i = 0;
int factorial = 1; /* 0! =1 */
while (i < n) {
i++;
factorial *= i;
}
do...while loop




The do/while repetition structure - similar to the while loop
Condition for repetition tested after the body of the loop is performed
All actions are performed at least once
Format:
do {
<statement>
} while (<expression>);
do..while Example
Here is an example code fragment:
counter = 1;
do {
printf( "%d ", counter );
} while (++counter <= 10);
Prints the integers from 1 to 10
Note:
counter is (pre-)incremented inside the while expression - this is an important
side-effect because otherwise the loop won’t terminate.
Infinite Loops
A loop needs to have a terminating condition - either a counter reaches a certain
value or a Boolean expression becomes true.
If a loop never terminates it is called an infinite loop. Infinite loops will crash a
program.
Examples:
while (true){
/* do something */
}
do{
/* etc. etc. */
}while (true);
for loop
A for loop is an iterative control structure that we can use when we want to
execute some statement or statements a fixed number of times.
The syntax of the if statement is now given:
for (expression1; expression2; expression3) <statement>
The for loop can be understood as a two step process.
Step 1) Evaluate expression1
Step 2) Evaluate expression2
if expression2 is true the <statement> is executed and then expression3
is evaluated. Repeat step 2.
if expression2 is false, jump to next statement after for loop.
Factorial Example
Here is an example where we use a for loop to calculate the factorial of a number.
(Recall that the factorial of a number (written n! for a number n) is the product
of all number less than or equal to it, e.g. 4! = 4*3*2*1 = 24. In general, n! =
n*(n-1)!, n> 1 and 1!=1.)
/* calculate factorial of integer num */
int factorial = 1, i;
for (i =1; i <= num; i++)
factorial = factorial * i;
/* result is factorial */
for Loop Initialisation
An operator that is commonly used in for statements is the comma operator, which
specifies that two expressions are executed one after the other. E.g.:
int i,j;
for (i=0, j=20; i <= 20; i++, j--)
printf(“%d + %d = %d\n”,i, j, i+j);
This will print the sum 20 twenty one times in a row. Note that j is decremented
each time. Why?
Note: The comma operator is used in the initialisation part and in expression2.