Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Iterative Statements

No description
by

Chris Daniel

on 11 September 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Iterative Statements

Notes
It is the one that causes a statement or a group of statements to be executed zero, one, or more times.
General Design Issues for Iterative Statements
How is the iteration controlled?


Where should be the controlled mechanism appear in the loop statement?
Pretest VS Post test Loops
Logically Controlled Loops
- Repetition control is based on a boolean expression.
Conclusion
Iterative Statements
Iterative Statements
It is often called a loop.
Counter-controlled
Loops
Logically controlled
Loops
Pretest Loop
Pretest VS Post test Loops
Pretest Loop
Condition
Action or actions
true
false
end
Condition
end
false
Post test Loop
Action or actions
- the test for loop completion occurs before the loop body is executed
Post test Loop
Counter-controlled Loops
Counter-controlled Loops
- Repeat one or more statements for a fixed number of times
Loop Parameters
Loop variable - a variable whose value controls loop repetition. It must be initialized, tested, and updated for the loop to execute properly.
Initial value - a value of the loop variable before the first iteration of the loop.
Terminal value - the last value the loop variable could possibly take on.
Step size - states how much to add to the loop variable after each iteration of the loop.
What are the type and scope of the loop variable?
Design Issues
Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect the loop control?
Should the loop parameters be evaluated only once, or once every iteration?
Ada 'for' Statement
Ada 'for' Statement
for var in [reverse] discrete_range loop
Syntax:
-- Loop body goes here
end loop;
Example:
Count : Float := 1.35;
for Count in 1..10 loop
Sum := Sum + Count;
end loop;
Ada 'for' Statement
The 'for' Statement of C-Based Language
for (expression_1; expression_2; expression_3)
Syntax:
-- Loop body goes here
Start
Initialization
Expression
Expression
Test
end
Loop body
Update Expression
Different Ways of Implementing for Loop
C for Loop Flowchart
Form
Comment
for (i = 0; i < 10; i++)
Statement1;
for (i = 0; i < 10; i++) {
Statement1;
Statement2;
Statement3;
}
for (i = 0; i<10; i++);
for (i = 0, j = 0; i < 100; i++, j++)
Statement1;
for (; i < 10; i++)
for (; i < 10;)

for (;;)
Single statement
Multiple statements within for loop
For Loop with no body
Multiple initialization and multiple update statements
Initialization not used
Initialization and update not used
Infinite loop
The for Statement of Python
for loop_variable in object:
- loop body
[else:
- else clause]
Syntax:
The 'for' Statement of C-Based Language
C Design Choices
Python for Loop Flowchart
Item from sequence
Execute
statement
next item from sequence
if no more item
in sequence
int counter = 0
counter <= limit
body
increase counter
yes
no
Is condition
true?
body
no
yes
Example:
Count : Float := 1.35;
Five : Integer := 5;
Sum : Integer;

for Count in 1..Five loop
Sum := Sum + Count;
Five := 25;
Put (Sum);
New_Line;
end loop;
Put (Five);
Example:
for count in [2, 4, 6]:
print count
2
4
6
returns:
Example:
for count in range(5):
print count
returns:
0
1
2
3
4
Example:
for count in range(2, 7):
print count
returns:
2
3
4
5
6
Example:
for count in range(0, 8, 2):
print count
returns:
0
2
4
6
Example:
for count in [1, 2, 3]:
print(count)
print('Yes' * count)
returns:
1
Yes
2
YesYes
3
YesYesYes
Example:
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print 'Current fruit :', fruits[index]
print "Good bye!"
Current fruit : banana
Good bye!
Current fruit : apple
Good bye!
Current fruit : mango
Good bye!
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print 'Current fruit :', fruits[index]
print "Good bye!"
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
returns:
Example:
returns:
Example:
for letter in 'Python':
print letter
returns:
p
y
t
h
o
n
Example:
for letter in 'Python':
print letter
returns:
Indentation error: expected an indented block
Counter-Controlled Loops in Functional Languages
Functional languages use recursion rather than iteration.
- Recursion is a process in which a function calls itself as a subroutine.
General form of an F# function:
let rec forLoop loopBody reps =
if reps <= 0 then
()
else
loopBody()
forLoop loopBody, (reps-1);;
let rec forLoop loopBody reps =
if reps <= 0 then
()
else
loopBody()
forLoop loopBody, (reps-1);;
function name
number of repetitions
- The rec keyword is used together with the let keyword to define a recursive function.
Logically Controlled Loops
Examples:
C-based programming languages include both pretest and posttest logically controlled loops that are not special forms of their counter-controlled iterative statements. The control expression can also be arithmetic.
while (control_expression)
loop body
do
loop body
while (control_expression);
and
In the pretest version of a logical loop (while), the block of statements is executed as long as the expression evaluates to true.
In the posttest version (do), the loop body is executed until the expression evaluates to false.
Operational Semantics for While and Do-While Statements
while:
loop:
if control_expression is false goto out
[loop body]
goto loop
out...
do-while:
loop:
[loop body]
if control_expression is true goto loop
General Form of a Simulated Logical
Pretest Written in F#
let rec whileLoop test body =
if test() then
body()
whileLoop test body
else
();;
- the test for loop completion occurs after the loop body is executed
Discrete type - subrange of an integer or an enumeration type.
Range - must be expressed from low value to high value
'reverse' - a reserved word which indicates that the values of the discrete ranges are assigned to the loop variable in reverse order.

The type of the loop variable is that of the discrete range; its scope is the loop body (it is implicitly declared). The loop variable does not exist outside the loop.
Ada Design choices
The loop variable cannot be assigned a value in the loop body.
Variables used to specify the discrete range can be changed in the loop.
Changing the range does not affect loop control.
The discrete range is evaluated just once
The loop body can be a single statement, a compound statement, or a null statement.
First expression - used for initialization and is evaluated only once.
Second expression - a loop control and is evaluated before each execution of the loop body.
Third expression - executed after each execution of the loop body. It is often used to increment the loop counter.

There is no explicit loop var so no restriction on scope.
Everything can be changed in the loop.
The first expression is evaluated once, but the other two are evaluated with each iteration.
Expression 2 is evaluated at the beginning of each iteration.
Expression 3 is evaluated at the end of each iteration
There is no explicit loop var so no restriction on type.
Object is often a range which is either a list of values in brackets ([2, 4, 6]) or a call to a range function (range(5), which returns 0, 1, 2, 3, 4.
You can also specify the starting value, stopping value, and increment for a range function, defined as range(start, stop, step).
The loop variable takes on the values specified in the given range, one for each iteration
Note: range never returns the highest value in a given parameter range.
Design Issues
- Should the control be pretest or posttest?
- Should the logically controlled loop be a special form of a counting loop or a separate statement?
Full transcript