Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most...

10
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;

Transcript of Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most...

Page 1: 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.

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;

}

Page 2: 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.

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.

Page 3: 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.

#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);

}

Page 4: 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.

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;

}

Page 5: 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.

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);

}

Page 6: 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.

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);

Page 7: 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.

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.

Page 8: 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.

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:

Page 9: 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.

for ( I = 1; I <= 3: ++I )

{

for ( j = 1; j <= 20; ++j )

{

printf(“\n I = %d, j = %d”, I,j);

if (j > 12)

break;

}

}

Page 10: 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.

Any Questions