Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most...
-
Upload
rolf-bailey -
Category
Documents
-
view
218 -
download
1
Transcript of Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most...
Chapter 5
Control Structures: Loops
5.1 The while Loop
The while loop is probably the most frequently used loop construct. The while loop is a conditional loop in which execution of the loop body depends on the value of a condition that is evaluated prior to the execution of the loop body. The general form of a while loop is
while (condition)
{
statement;
}
Example: Computation of Factorial
The factorial of an integer n is defined by
n! = n(n-1)(n-2)(n-3)…(3).(2).(1)
as well as
0! = 1
n! = n(n-1)!
Problem statement: write a program to compute the factorial of an integer n.
Solution: The program begins by assigning an initial value of 1 to the variable factorial. The computation of the factorial is performed in the following while loop.
#include <stdio.h>
main()
{
int i, n;
double factorial;
printf(“\n Enter a positive integer: “);
scanf(“%d”,&n);
factorial = 1 ;
i = 1;
while ( I <= n)
{
factorial *= i;
++i;
}
printf(“\n %d factorial is %.01f”, n, factorial);
}
5.2 The for Loop
The simplest type of loop is one in which a series of statements is repeated and the number of repetitions is known in advance. This is sometimes referred to as a counting loop. In C the for statement allows us to repeat a sequence of statements a specified number of times.
The general form of the for statement is
for ( initialization; test condition; increment)
{
statement;
}
Example: Computation of Factorials#include <stdio.h>
main()
{
int i, n;
double factorial;
printf(“\n Enter a positive integer: “);
scanf(“%d”,&n);
factorial = 1 ;
for ( i=2: i <= n; ++i )
factorial *= i;
printf(“\n %d factorial is %.0f”, n, factorial);
}
5.3 The do-while Loop
Both the while and the for loops are entry condition loops, that is, the expression that controls the loop is evaluated before each iteration of the loop. C also provides the do-while statement which enables us to implement as exit condition loop. In an exit condition loop the expression that controls the loop is evaluated at the end of each iteration instead of at the beginning.
The general form of the do-while loop is
do
{
statement;
} while (expression);
For example:
do
{
printf(“\n Enter a number between 0 and 100”);
scanf(“%f”,&num);
} while (num > 0 && num < 100);
Note: If we want to get out form this loop, this expression is false. So we will be able to get out form this loop.
5.4 The break Statement
The break statement can also be used with the while, do-while, and for loops where it performs a similar function. When a break statement is encountered within a loop the loop, is terminated and control is transferred to the next sequential statement following the loop.
The break statement is used in situations where a special condition can cause termination of the loop. For example, consider the following code segment:
for ( I = 1; I <= 3: ++I )
{
for ( j = 1; j <= 20; ++j )
{
printf(“\n I = %d, j = %d”, I,j);
if (j > 12)
break;
}
}
Any Questions