SlideShare a Scribd company logo
Chapter 8
Control Structures
Lecture-13.ppt
Lecture-13.ppt
Lecture-13.ppt
Lecture-13.ppt
Lecture-13.ppt
Lecture-13.ppt
Two Way Selection Statement
Two Way Selection Statement
• ALGOL 60 2-way selector:
if (Boolean_expr)
then statement
else statement
• Nested if-else
• Special delimiters and selection closure ALGOL
68, FORTRAN 77, Ada use end if
Two Way Selection Statement
• 2-Way Selection Statements ALGOL 60's
solution - disallow direct nesting
Nesting Selectors
Lecture-13.ppt
Lecture-13.ppt
Lecture-13.ppt
Nesting Selectors
• FORTRAN 90 and Ada solution – closing
special words
Multiple-Way Selection Statements
Design issues:
• What is the form and type of expression that controls the
selection
• May single statements, sequences of statements, or compound
statements be selected
• Is the entire construct encapsulated in a syntactic structure
• Is execution flow through the structure restricted to include just a
single selectable segment
• How should unrepresented selector expression values be
handled
Multiple Selection Statements
Design choices:
• Expression is any ordinal type
(int, boolean, char, enum)
• Segments can be single or compound
• Only one segment can be executed per execution of the
construct
• In Wirth's Pascal, result of an unrepresented control
expression value is undefined (In 1984 ISO Standard, it is a
runtime error)
• Many dialects now have otherwise or else clause
Lecture-13.ppt
Modern multiple selectors
• C, C++, Java switch statement
• FORTRAN arithmetic IF
• Ada case statement
• Pascal case statement
Multiple Way Selection -Example
More reliable than C’s switch (once a stmt_sequence execution
is completed, control is passed to the first statement after the
case statement
Multiple-Way Selection Using if
• Multiple Selectors can appear as direct extensions to two-way
selectors,
• using else-if clauses, for example in Python:
Multiple-Way Selection Using if
• The Python example can be written as a Ruby
case
Far more readable than deeply nested if 's Allows a Boolean gate on every
selectable group
Iterative Statements
• The repeated execution of a statement or
compound statement is accomplished either
by iteration or recursion
• General design issues for iteration control
statements:
1. How is iteration controlled?
2. Where is the control mechanism in the loop?
Counter-Controlled Loops
• A counting iterative statement has a loop variable, and a
means of specifying the initial and terminal, and stepsize
values
Design Issues:
• What are the type and scope of the loop variable?
• What is the value of the loop variable at loop termination?
• 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?
• Should the loop parameters be evaluated only once, or once
for every iteration?
Iterative Statements: Examples
Design choices:
• 1. Loop variable must be INTEGER
• 2. Loop variable always has its last value
• 3. The loop variable cannot be changed in the loop, but the
parameters can; because they are evaluated only once, it does
not affect loop control
• 4. Loop parameters are evaluated only once
Iterative Statements: Examples
Iterative Statements: Examples
• Ada
Iterative Statements: Examples
Ada Design choices:
• 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
• The loop variable cannot be changed in the loop, but the
discrete range can; it does not affect loop control
• The discrete range is evaluated just once
Iterative Statements: Examples
• C-based languages
for ([expr_1] ; [expr_2] ; [expr_3]) statement-
The expressions can be whole statements, or even statement
sequences, with the statements separated by commas
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
Iterative Statements: Examples
C++ differs from C in two ways:
• The control expression can also be Boolean
• 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
Iterative Statements: Examples
Iterative Statements: Logically-Controlled
Loops
• 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?
Iterative Statements: Logically-Controlled
Loops: Examples
• C and C++ have both pretest and posttest forms, in which the
control expression can be arithmetic:
• Java is like C and C++, except the control expression must be
Boolean
Iterative Statements: Logically-Controlled
Loops: Examples
• Ada has a pretest version, but no posttest
• FORTRAN 95 has neither
• Perl and Ruby have two pretest logical loops,
while and until. Perl also has two posttest
loops
Iterative Statements: User-Located Loop
Control Mechanisms
• 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
• Should the conditional be part of the exit?
• Should control be transferable out of more
than one loop?
Iterative Statements: User-Located Loop
Control Mechanisms break and continue
• 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
Iterative Statements: Iteration Based on
Data Structures
• Number of elements of in a data structure control
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 terminate
• C's for can be used to build a user-defined iterator:
for (p=root; p==NULL; traverse(p)){
}
Iterative Statements: Iteration Based on
Data Structures
Unconditional Branching
• Transfers execution control to a specified place in the program
• Represented one of the most heated debates in 1960’s and
1970’s
• Well-known mechanism: goto statement
• 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
Guarded Commands Designed by Dijkstra
• Purpose: to support a new programming
methodology that supported verification
(correctness) during development
• Basis for two linguistic mechanisms for concurrent
programming (in CSP and Ada)
• Basic Idea: if the order of evaluation is not
important, the program should not specify one
Guarded Commands
if <Boolean expression> -> <statement>
[ ] <Boolean expression> -> <statement>
[ ] . . .
[] <Boolean expression> -> <statement>
fi
• The closing reserved word, fi, is the opening reserved word spelled
backward. This form of closing reserved word is taken from ALGOL 68.
• The small blocks, called fatbars, are used to separate the guarded clauses
and allow the clauses to be statement sequences.
• Each line in the selection statement, consisting of a Boolean expression (a
guard) and a statement or statement sequence, is called a guarded
command.
Selection Guarded Command
Guarded Commands
• If more than one expression is true, one of the corresponding
statements can be non-deterministically chosen for execution.
• If i = 0 and j > i, this statement chooses non-deterministically
between the first and third assignment statements.
• If i is equal to j and is not zero, a runtime error occurs
because none of the conditions is true
Guarded Commands
• This computes the desired result without over-
specifying the solution.
• In particular, if x and y are equal, it does not matter
which we assign to max. This is a form of abstraction
provided by the nondeterministic semantics of the
statement
Selection Guarded Command: Illustrated
Guarded Command
The loop structure proposed by Dijkstra has the form
The semantics of this statement is that all Boolean expressions are
evaluated on each iteration. If more than one is true, one of the
associated statements is non-deterministically (perhaps randomly)
chosen for execution, after which the expressions are again evaluated.
When all expressions are simultaneously false, the loop terminates.
Guarded Commands
• Now, consider the following code, which uses guarded commands to solve
the same problem but in a more concise and elegant way.
Guarded Commands
• Dijkstra’s guarded command control statements are interesting, in part
because they illustrate how the syntax and semantics of statements can
have an impact on program verification and vice versa
Guarded Commands: Rationale
• Connection between control statements and
program verification is intimate
• Verification is impossible with goto statements
• Verification is possible with only selection and
logical pretest loops
• Verification is relatively simple with only
guarded commands
Lecture-13.ppt

More Related Content

PPT
8 statement level
PDF
8 statement-level control structure
PPTX
chapter 6.pptx
PDF
07 control+structures
PPT
PPTX
control structures in c if switch for
PDF
Chapter 3 - Flow of Control Part II.pdf
PPTX
C language (Part 2)
8 statement level
8 statement-level control structure
chapter 6.pptx
07 control+structures
control structures in c if switch for
Chapter 3 - Flow of Control Part II.pdf
C language (Part 2)

Similar to Lecture-13.ppt (20)

PPTX
C Programming - Decision making, Looping
PPT
Decision making and looping
PPT
control-statements....ppt - definition
PPT
control-statements, control-statements, control statement
PPTX
C language 2
PDF
Control statements
PDF
Controls & Loops in C
PDF
Unit II chapter 4 Loops in C
PPTX
Lecture 03 Programming C for Beginners 001
PPT
2. Control structures with for while and do while.ppt
PPTX
Diploma ii cfpc u-3 handling input output and control statements
PDF
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
PPTX
Bsc cs pic u-3 handling input output and control statements
PDF
UNIT 2 PPT.pdf
PPTX
Ch6 Loops
PPTX
handling input output and control statements
PPTX
Control structure of c
PDF
Slide 6_Control Structures.pdf
PDF
Repetition, Basic loop structures, Loop programming techniques
PPTX
Mca i pic u-3 handling input output and control statements
C Programming - Decision making, Looping
Decision making and looping
control-statements....ppt - definition
control-statements, control-statements, control statement
C language 2
Control statements
Controls & Loops in C
Unit II chapter 4 Loops in C
Lecture 03 Programming C for Beginners 001
2. Control structures with for while and do while.ppt
Diploma ii cfpc u-3 handling input output and control statements
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
Bsc cs pic u-3 handling input output and control statements
UNIT 2 PPT.pdf
Ch6 Loops
handling input output and control statements
Control structure of c
Slide 6_Control Structures.pdf
Repetition, Basic loop structures, Loop programming techniques
Mca i pic u-3 handling input output and control statements
Ad

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
01-Introduction-to-Information-Management.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
VCE English Exam - Section C Student Revision Booklet
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Complications of Minimal Access Surgery at WLH
TR - Agricultural Crops Production NC III.pdf
RMMM.pdf make it easy to upload and study
Sports Quiz easy sports quiz sports quiz
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
STATICS OF THE RIGID BODIES Hibbelers.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPH.pptx obstetrics and gynecology in nursing
01-Introduction-to-Information-Management.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Ad

Lecture-13.ppt

  • 8. Two Way Selection Statement
  • 9. Two Way Selection Statement • ALGOL 60 2-way selector: if (Boolean_expr) then statement else statement • Nested if-else • Special delimiters and selection closure ALGOL 68, FORTRAN 77, Ada use end if
  • 10. Two Way Selection Statement • 2-Way Selection Statements ALGOL 60's solution - disallow direct nesting
  • 15. Nesting Selectors • FORTRAN 90 and Ada solution – closing special words
  • 16. Multiple-Way Selection Statements Design issues: • What is the form and type of expression that controls the selection • May single statements, sequences of statements, or compound statements be selected • Is the entire construct encapsulated in a syntactic structure • Is execution flow through the structure restricted to include just a single selectable segment • How should unrepresented selector expression values be handled
  • 17. Multiple Selection Statements Design choices: • Expression is any ordinal type (int, boolean, char, enum) • Segments can be single or compound • Only one segment can be executed per execution of the construct • In Wirth's Pascal, result of an unrepresented control expression value is undefined (In 1984 ISO Standard, it is a runtime error) • Many dialects now have otherwise or else clause
  • 19. Modern multiple selectors • C, C++, Java switch statement • FORTRAN arithmetic IF • Ada case statement • Pascal case statement
  • 20. Multiple Way Selection -Example More reliable than C’s switch (once a stmt_sequence execution is completed, control is passed to the first statement after the case statement
  • 21. Multiple-Way Selection Using if • Multiple Selectors can appear as direct extensions to two-way selectors, • using else-if clauses, for example in Python:
  • 22. Multiple-Way Selection Using if • The Python example can be written as a Ruby case Far more readable than deeply nested if 's Allows a Boolean gate on every selectable group
  • 23. Iterative Statements • The repeated execution of a statement or compound statement is accomplished either by iteration or recursion • General design issues for iteration control statements: 1. How is iteration controlled? 2. Where is the control mechanism in the loop?
  • 24. Counter-Controlled Loops • A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize values Design Issues: • What are the type and scope of the loop variable? • What is the value of the loop variable at loop termination? • 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? • Should the loop parameters be evaluated only once, or once for every iteration?
  • 25. Iterative Statements: Examples Design choices: • 1. Loop variable must be INTEGER • 2. Loop variable always has its last value • 3. The loop variable cannot be changed in the loop, but the parameters can; because they are evaluated only once, it does not affect loop control • 4. Loop parameters are evaluated only once
  • 28. Iterative Statements: Examples Ada Design choices: • 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 • The loop variable cannot be changed in the loop, but the discrete range can; it does not affect loop control • The discrete range is evaluated just once
  • 29. Iterative Statements: Examples • C-based languages for ([expr_1] ; [expr_2] ; [expr_3]) statement- The expressions can be whole statements, or even statement sequences, with the statements separated by commas 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
  • 30. Iterative Statements: Examples C++ differs from C in two ways: • The control expression can also be Boolean • 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
  • 32. Iterative Statements: Logically-Controlled Loops • 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?
  • 33. Iterative Statements: Logically-Controlled Loops: Examples • C and C++ have both pretest and posttest forms, in which the control expression can be arithmetic: • Java is like C and C++, except the control expression must be Boolean
  • 34. Iterative Statements: Logically-Controlled Loops: Examples • Ada has a pretest version, but no posttest • FORTRAN 95 has neither • Perl and Ruby have two pretest logical loops, while and until. Perl also has two posttest loops
  • 35. Iterative Statements: User-Located Loop Control Mechanisms • 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 • Should the conditional be part of the exit? • Should control be transferable out of more than one loop?
  • 36. Iterative Statements: User-Located Loop Control Mechanisms break and continue • 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
  • 37. Iterative Statements: Iteration Based on Data Structures • Number of elements of in a data structure control 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 terminate • C's for can be used to build a user-defined iterator: for (p=root; p==NULL; traverse(p)){ }
  • 38. Iterative Statements: Iteration Based on Data Structures
  • 39. Unconditional Branching • Transfers execution control to a specified place in the program • Represented one of the most heated debates in 1960’s and 1970’s • Well-known mechanism: goto statement • 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
  • 40. Guarded Commands Designed by Dijkstra • Purpose: to support a new programming methodology that supported verification (correctness) during development • Basis for two linguistic mechanisms for concurrent programming (in CSP and Ada) • Basic Idea: if the order of evaluation is not important, the program should not specify one
  • 41. Guarded Commands if <Boolean expression> -> <statement> [ ] <Boolean expression> -> <statement> [ ] . . . [] <Boolean expression> -> <statement> fi • The closing reserved word, fi, is the opening reserved word spelled backward. This form of closing reserved word is taken from ALGOL 68. • The small blocks, called fatbars, are used to separate the guarded clauses and allow the clauses to be statement sequences. • Each line in the selection statement, consisting of a Boolean expression (a guard) and a statement or statement sequence, is called a guarded command.
  • 43. Guarded Commands • If more than one expression is true, one of the corresponding statements can be non-deterministically chosen for execution. • If i = 0 and j > i, this statement chooses non-deterministically between the first and third assignment statements. • If i is equal to j and is not zero, a runtime error occurs because none of the conditions is true
  • 44. Guarded Commands • This computes the desired result without over- specifying the solution. • In particular, if x and y are equal, it does not matter which we assign to max. This is a form of abstraction provided by the nondeterministic semantics of the statement
  • 46. Guarded Command The loop structure proposed by Dijkstra has the form The semantics of this statement is that all Boolean expressions are evaluated on each iteration. If more than one is true, one of the associated statements is non-deterministically (perhaps randomly) chosen for execution, after which the expressions are again evaluated. When all expressions are simultaneously false, the loop terminates.
  • 47. Guarded Commands • Now, consider the following code, which uses guarded commands to solve the same problem but in a more concise and elegant way.
  • 48. Guarded Commands • Dijkstra’s guarded command control statements are interesting, in part because they illustrate how the syntax and semantics of statements can have an impact on program verification and vice versa
  • 49. Guarded Commands: Rationale • Connection between control statements and program verification is intimate • Verification is impossible with goto statements • Verification is possible with only selection and logical pretest loops • Verification is relatively simple with only guarded commands