Programming Languages Chapter 8 Part 2:

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Counter-Controlled Loops: Example for ([expr_1] ; [expr_2] ; [expr_3]) loop-body

- The expressions can be whole statements, or even statement sequences, with the statements separated by commas • The value of a multiple-statement expression is the value of the last statement in the expression - If the second expression is absent, it is an infinite loop

Counter-Controlled Loops: Examples • C++ differs from C in two ways: 1. The control expression can also be Boolean 2. The initial expression can include variable definitions (scope is from the definition to the end of the loop body) • Java and C# - Differs from C++ in that the control expression must be Boolean

...

Counter-Controlled Loops: Examples • F# - Because counters require variables, and functional languages do not have variables, counter-controlled loops must be simulated with recursive functions let rec forLoop loopBody reps = if reps <= 0 then () else loopBody() forLoop loopBody (reps - 1) - This defines the recursive function forLoop with the parameters loopBody (a function that defines the loop's body) and the number of repetitions - ()in then clause means do nothing and return nothin

...

Counter-Controlled Loops: Examples • Python for loop_variable in object: loop body [else: else clause] - The object is often a range, which is either a list of values in brackets ([2, 4, 6]), or a call to the range function (range(5), which returns 0, 1, 2, 3, 4 - The loop variable takes on the values specified in the given range, one for each iteration - The else clause, which is optional, is executed if the loop terminates normally

...

Logically-Controlled Loops: Examples • C and C++ have both pretest and posttest forms, in which the control expression can be arithmetic: while (control_expr) do loop body - In both C and C++ it is legal to branch into the body of a logically-controlled loop • Java is like C and C++, except the control expression must be Boolean (and the body can only be entered at the beginning -- Java has no goto

...

User-Located Loop Control Mechanisms • C , C++, Python, Ruby, and C# have unconditional unlabeled exits (break) • Java and Perl have unconditional labeled exits (break in Java, last in Perl) • C, C++, and Python have an unlabeled control statement, continue, that skips the remainder of the current iteration, but does not exit the loop • Java and Perl have labeled versions of continue

...

Counter-Controlled Loops *A counting iterative statement has a...... *Design Issues: 1. What are the type and scope of the..... 2. Should it be legal for the loop variable or..... 3. Should the loop.....

• A counting iterative statement has a loop variable, and a means of specifying the initial value, terminal value , and stepsize value • Design Issues: 1. What are the type and scope of the loop variable? 2. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 3. Should the loop parameters be evaluated only once, or once for every iteration

Counter-Controlled Loops: Examples *Design choices: -there is no explicit.... -everything can be changed..... -the first expression is evaluated once, but.... -it is legal to branch.....

• Design choices: - There is no explicit loop variable - Everything can be changed in the loop - The first expression is evaluated once, but the other two are evaluated with each iteration - It is legal to branch into the body of a for loop in C

F# Looping

• F# is not a pure functional language - It is not restricted to using recursion for iteration - It provides a number of looping constructs like those in imperative / object-oriented languages *for...to and for...downto *for...in *while...do

Iterative Statements *General design issues for iteration 1. How is...... 2. Where is the control..... *Pretest means the test for loop occurs..... *Posttest means that the test for loop occurs........

• General design issues for iteration control statements: 1. How is iteration controlled? 2. Where is the control mechanism in the loop? • Pretest means the test for loop completion occurs before the loop body is executed • Posttest means that the test for loop completion occurs after the loop body is executed

Logically-Controlled Loops *Repetition control is based on...... *Design issues: -pretest or.... -should the logically controlled loop be a......

• Repetition control is based on a Boolean expression • Design issues: - Pretest or posttest? - Should the logically controlled loop be a special case of the counting loop statement or a separate statement?

User-Located Loop Control Mechanisms *Sometimes it is convenient for the programmers to..... *Simple design for...... *Design issues for nested loops 1. Should the conditional.... 2. Should control be transferable.....

• Sometimes it is convenient for the programmers to decide a location for loop control (other than top or bottom of the loop) • Simple design for single loops (e.g., break) • Design issues for nested loops 1. Should the conditional be part of the exit? 2. Should control be transferable out of more than one loop?

Iterative Statements *The basic present loop.... *Other types are loops ( ) are provide for.... 1. They are no more.....

• The basic pretest loop is adequate for all looping needs • Other types are loops (posttest, counting, etc.) are provided for the programmer's convenience - They are no more powerful than the basic pretest loop construct

Iteration Based on Data Structures *The number of elements in a data structure.... *Control mechanism is a call to...... C's for can be used to.....

• The number of elements in a data structure controls loop iteration • Control mechanism is a call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminated • C's for can be used to build a user-defined iterator: for (p=root; p!=NULL; traverse(p)) { ... }

Iterative Statement *The repeated execution of a statement or...... *An iterative statement is one that causes a...... -often times called a...... -the body of an iterative statement is the......

• The repeated execution of a statement or compound statement is accomplished either by iteration or recursion • An iterative statement is one that causes a statement or collection of statements to be executed zero, one, or more times - An iterative statement is often called a loop - The body of an iterative statement is the collection of statements whose execution is controlled by the iteration statement

Unconditional Branching *Transfers execution control to a..... *Represented one of the most heated.... *Major concern:...... *Some languages do not support....... *C# offers...... *Loop exit statements are......

• Transfers execution control to a specified place in the program • Represented one of the most heated debates in 1960's and 1970's • Major concern: Readability • Some languages do not support goto statement (e.g., Java) • C# offers goto statement (can be used in switch statements) • Loop exit statements are restricted and somewhat camouflaged goto's


Set pelajaran terkait

Fight or flight response article

View Set

Craniectomy for Stereotactic Procedures

View Set

309 Quality Management - Unit 1 & 2

View Set

Chapter 18- Culturally Responsive Nursing Care

View Set

Security+ Review Questions - Chapter 1

View Set

IELTS: Beginner Education and Knowledge Vocabulary Set 1

View Set

CTC Intro PSYC1101 CH5 Sensation and Perception

View Set

USMLE Step 2 CK Board Preparation: Diseases of the Blood

View Set

Chapter 34: The School-Age Child and Family

View Set

Examples of Application Software

View Set