Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

CSC 1401

Computer Programming I
Hamid Harroud
School of Science and Engineering,
Al Akhawayn University in Ifrane
h.harroud@aui.ma

Spring 2015
Lecture 5

Repetition Statements:
while, for and do-while
Repetition in Programs

• In order to solve most computational problems,


we may need to repeat a process many times.

• Loop is a control structure that repeats a group


of statements in a program.
• Three ways in C to implement repetition control
statements:

– while
– for
– do-while
Flow Diagram of Loop Choice
The while statement

• Counter-controlled loop (or counting loop)


– A loop whose required a number of iterations can
be determined before loop execution begins.
• The syntax
while (loop repetition condition)
statement;
• Loop repetition condition: the condition that
controls the loop repetition.
• Infinite loop: a loop that executes forever
While Statement
• Syntax:

While(loop repetition condition){


loop body
}

• Example:

/* counter-controlled while loop that displays N stars */

count_star = 0;

While (count_star < N) {


printf(“ * “ );
count_star = count_star + 1;
}
Flowchart for a while Loop
Example

count_emp = 0; /* no employees processed yet */


while (count_emp < 7)
{ /* test value of count_emp */
printf("Hours:: ");
scanf("%d", &hours);
printf("Rate:: ");
scanf("%lf", &rate);
pay = hours * rate;
printf("Pay is $%6.2f\n", pay);
++count_emp; /*increment count_emp */
}
printf("\nAll employees processed\n");
Computing a Sum or a Product
in a Loop
• Loops often accumulate a sum or a
product by repeating an addition or
multiplication operation.
• Accumulator
– A variable used to store a value being
computed in increments during the
execution of a loop.
Compound Assignment Operators

• C provides special assignment


operators variable op = expression;
The for Statement
• Three loop control components with the loop
body.
– Initialization of the loop control variable,
– Test of the loop repetition condition, and
– Change (update) of the loop control variable.

• The for statement in C supplies a designed


place for each of these three components .
Counting Loop using for Statement
for Statement
Increment and Decrement
Operators
• The increment (i.e. ++) or decrement
(i.e. --) operators are the frequently used
operators which take only one operand.
for(i=0; i<100; i++) {…}
for(i=100; i>0; i--) {…}
• The value of its operand is
incremented/decremented by one.
Prefix and Postfix Increments

• The value of the expression in which the +


+/-- operator is used depends on the
position of the operator.
Example: Factorial Function
Conditional Loops

• In many programming situations, you will


not be able to determine the exact number
of loop repetitions before loop execution
begins.
Sentinel-Controlled Loops

• One way to do this is to instruct the user to enter a


unique data value, called a sentinel value, after
the last data item.
The do-while Statement
• When we know that a loop must execute
at least one time.
Example
• Write a program that keeps asking the
user to enter her/his date of birth until the
value entered is a valid one.

( 1900 <= year <= 2007 , 1 <= month <= 12, 1 <= day <= 31)
Nested Loops
• Consist of an outer loop with one or more
inner loops. (Fig. 5.13)
Nested Loop Example
• How many stars are displayed after the
following code fragment is executed:

for(i = 0; i <= 3; i++)


for(j = 0; j <= i; j++)
printf(‘*’);
Example
• Write a C program that displays the following
using nested loops:

*
**
***
****
*****
******
Example
• Write nests of loops that cause the
following output to be displayed:

0
01
012
0123
01234
0123
012
01
0
Flag-Controlled Loops

• A flag is a variable used to represent whether


or not a certain event has occurred.
(Fig. 5.14)
Summary

• Repetition and Loop Statements


– Counter-Controller Loop
– Sentinel-Controller Loop
– Endfile-Controller Loop
– Input Validation Loop
– General Conditional Loop
• while, for, and do-while
• Compound Assignment Operators
Example
• Write an algorithm that computes the
factorial function.

• fact (0) = 1
• fact (i) = i x (i-1) x (i -2) x … x 1
Example
• Write a program that reads in two integers
N1 and N2 and then display all odd
integers between N1 and N2.
(Assume N1 is always less than or equal to
N2).
Example
• Write an algorithm that calculates the
sum of the first N natural numbers for
any value of N.
Example
• Write a program that determines the
smallest, largest, and average values in
a collection of N numbers entered by
the user.
3N + 1 Problem
• Start with an integer n. If n is even, divide by 2. If
n is odd, multiply by 3 and add 1. Repeat this
process with the new value of n, terminating when
n = 1.
• For example, the following sequence of numbers
will be generated for n = 22:
22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

• Write a program that displays this sequence for


any entered integer N.
Example

• Write a program that keeps asking the


user to enter integer numbers as long
as their sum is not exceeding 1000.
Example
• Write a program that computes the sum
of a list of exam scores entered by the
user as long as she/he did not enter a
unique data value (sentinel value) that
marks the end of the list

• Let us assume that the sentinel value in


our case is -99.
Example
• Write a program that displays the first
N numbers in the Fibonacci Sequence
where each successive number in the
sequence is the sum of the previous
two numbers.

You might also like