0% found this document useful (0 votes)
13 views22 pages

Chapter 05

Uploaded by

harodaashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Chapter 05

Uploaded by

harodaashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 5

Repetition
and Loop
Statements

Instructor:
Kun-Mao Chao
( 台大資工 趙坤
茂)
Repetition in Programs
• In most software, the statements in the program
may need to repeat for many times.
– e.g., calculate the value of n!.
– If n = 10000, it’s not elegant to write the code as
1*2*3*…*10000.
• Loop is a control structure that repeats a group
of steps in a program.
– Loop body stands for the repeated statements.
• There are three C loop control statements:
– while, for, and do-while.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-2
Flow Diagram of Loop Choice Process

e.g., read the content in a file

e.g., calculate the value of n!


Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-3
Comparison of Loop Choices (1/2)

Kind When to Use C Structure


Counting loop We know how many loop while, for
repetitions will be needed
in advance.
Sentinel- Input of a list of data ended while, for
controlled loop by a special value
Endfile- Input of a list of data from while, for
controlled loop a data file

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-4


Comparison of Loop Choices (2/2)
Kind When to Use C Structure
Input validation Repeated interactive input do-while
loop of a value until a desired
value is entered.
General Repeated processing of while, for
conditional data until a desired
loop condition is met.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-5


The while Statement in C
• The syntax of while statement in C:
while (loop repetition condition)
condition
statement
• Loop repetition condition is the condition
which controls the loop.
• The statement is repeated as long as the loop
repetition condition is true.
true
• A loop is called an infinite loop if the loop
repetition condition is always true.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-6
An Example of a while Loop
Loop repetition condition

Statement

Loop control variable is the variable whose value controls


loop repetition.
In this example, count_emp is the loop control variable.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-7
Flowchart for a while Loop

Loop repetition condition

Statement
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-8
Compound Assignment Operators
(1/2)
• The loop body usually consists of statements of
the form: variable = variable op expression.
– e.g., count_emp = count_emp + 1;
• C provides compound assignment operators
which enable a more concise notation for this
kind of statements.
– “variable op = expression” is the same to
“variable = variable op expression.”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-9


Compound Assignment Operators
(2/2)
Simple Assignment Compound Assignment
Operators Operators
count_emp = count_emp += 1;
count_emp + 1;
time = time -1; time -= 1;

product = product * product *= item;


item;
total = total / total /= number;
number;
n = n % (x+1); n %= x+1;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-10
The for Statement in C
• The syntax of for statement in C:
for (initialization expression;
expression
loop repetition condition;
condition
update expression)
expression
statement
• The initialization expression set the initial value of the
loop control variable.
• The loop repetition condition test the value of the loop
control variable.
• The update expression update the loop control
variable.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-11
An Example of the for Loop
Initialization Expression

Loop repetition condition

Update Expression

count_emp is set to 0 initially.


count_emp should not exceed the value of number_emp.
count_emp is increased by one after each iteration.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-12
Increment and Decrement Operators

• The statements of increment and decrement are


commonly used in the for loop.
• The increment (i.e., ++)
++ or decrement (i.e., --)
--
operators are the frequently used operators
which take only one operand.
• The increment/decrement operators increase or
decrease the value of the single operand.
– e.g., for (int i = 0; i < 100; i++){
i++ … }
– The variable i increase one after each iteration.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-13
Comparison of Prefix and Postfix
Increments
The value of the expression (that uses the ++/-- operators)
depends on the position of the operator.

The value The value


of j is of j is not
increased increased

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-14


Sentinel-Controlled Loops

• Sometimes we may not know how many times


the loop will repeat.
• One way to do this is to choose a sentinel value
as an end marker.
– The loop exits when the sentinel value is read.
• If the user wish to exit the loop, he or she has to
input the sentinel value.
value
– It is similar to the “logout” function in many
applications.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-15
An Example of Sentinel-Controlled
while Loops

If the user wish to exit the loop,


he or she has to input -99.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-16


An Example of Endfile-Controlled
Loops
• fscanf is a function used to read file.
• EOF stands for the special value of end-file returned by
fscanf.
fscanf
• This loop repeats until reading the end of the file.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-17


Nested Loops
• Nested loops consist of an outer loop with one
or more inner loops.
loops
• e.g.,
for (i=1;i<=100;i++){ Outer loop

for(j=1;j<=50;j++){
… Inner loop
}
}
• The above loop will run for 100*50 iterations.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-18
The do-while Statement in C

• The syntax of do-while statement in C:


do
statement
while (loop repetition condition);
condition
• The statement is first executed.
• If the loop repetition condition is true, the
statement is repeated.
• Otherwise, the loop is exited.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-19
An Example of the do-while Loop

/* Find even number input */


do{
printf(“Enter a value: ”);
scanf(“%d”, &num);
}while (num % 2 !=0)
This loop will repeat if the user
inputs odd number.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-20


Homework #4 (1/2)
• Write a program that prompts the user to input
an integer n.
• Draw a triangle with n levels by star symbols.
For example,
n = 3,
*
**
***
• After drawing the triangle, repeat the above
process until the user input a negative integer.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-21
Homework #4 (2/2)
• An usage scenario:
Please input: 2
*
**
Please input: 3
*
**
***
Please input: -9
Thank you for using this program.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy