flow of control
DESCRIPTION
Flow of Control. Control Structures. Control structure: An instruction that determines the order in which other instructions in a program are executed. Structured programming: A programming methodology in which each logical unit of a program should have just one entry and one exit. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/1.jpg)
Flow of Control
![Page 2: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/2.jpg)
2
Control Structures
• Control structure: An instruction that determines the order in which other instructions in a program are executed.
• Structured programming: A programming methodology in which each logical unit of a program should have just one entry and one exit.
• Sequence, selection statements, looping statements, and subprogram statements are control structures.
![Page 3: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/3.jpg)
3
Functionality of Imperative Languages
• Sequence - executing statements in sequence until an instruction is encountered that changes this sequencing
• Selection - deciding which action to take• Iteration (looping) - repeating an action• Subprogram - a named section of code which
performs a specific task and is relatively independent of the remaining code.
Both selection and iteration require the use of a Boolean expression
![Page 4: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/4.jpg)
4
Boolean Expressions
• Boolean expression A sequence of identifiers, separated by compatible operators, that evaluates to true or false
• Boolean expression can be
– A Boolean variable
– An arithmetic expression followed by a relational operator followed by an arithmetic expression
– A Boolean expression followed by a Boolean operator followed by a Boolean expression
![Page 5: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/5.jpg)
5
Boolean Expressions
• Variable: A location in memory that is referenced by an identifier that contains a data value.
Thus, a Boolean variable is a location in memory that can contain either true or false
![Page 6: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/6.jpg)
6
Boolean Expressions• A relational operator between two arithmetic
expressions is asking if the relationship exists between the two expressions.
• For example, xValue < yValue, asks the question:“Is it true that xValue is less than yValue?”
![Page 7: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/7.jpg)
7
Selection Statements
• The if statement allows the program to test the state of the program variables using a Boolean expression.
• It then selects one of two actions depending on the result of the test.
![Page 8: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/8.jpg)
8
Selection Statements
![Page 9: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/9.jpg)
9
Looping Statements
• There two distinct types of repetitions:• Event-controlled loops
– The number of repetitions is controlled by an event that occurs within the body of the loop itself
• Count-controlled loops– Repeat a specified number of times
– Use of a special variable called a loop control variable
![Page 10: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/10.jpg)
10
Looping Statements
![Page 11: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/11.jpg)
11
Subprogram Statements
• We can give a section of code a name and use that name as a statement in another part of the program.
• When the name is encountered, the processing in the other part of the program halts while the named code is executed.
![Page 12: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/12.jpg)
12
Subprogram Statements
![Page 13: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/13.jpg)
13
Subprogram Statements
• There are times when the calling unit needs to give information to the subprogram to use in its processing.
• A parameter list is a list of the identifiers with which the subprogram is to work, along with the types of each identifier placed in parentheses beside the subprogram name.
![Page 14: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/14.jpg)
14
Subprogram Statements
![Page 15: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/15.jpg)
15
Subprogram Statements
• Parameters: Identifiers listed in parentheses beside the subprogram declaration; sometimes they are called formal parameters.
• Arguments: Identifiers listed in parentheses on the subprogram call; sometimes they are called actual parameters:
![Page 16: Flow of Control](https://reader030.vdocument.in/reader030/viewer/2022033022/56813ee2550346895da9598a/html5/thumbnails/16.jpg)
16
Subprogram Statements
• Value parameter: A parameter that expects a copy of its argument to be passed by the calling unit.
• Reference parameter: A parameter that expects the address of its argument to be passed by the calling unit.