Textbook Chapter 5: Programming with Control Structures
Decision control structures allow us to conditionally change the order in which a program is executed. Looping control structures allow us to repeat sections of code. Nested control structures make code easier to write and maintain.
Notice how the parentheses change the order of operations in the following EXAMPLE.
- Action statements - code that carries out a task such as displaying text
- steps are executed in the order in which they are written - known as linear programming
- Control statements - code that controls the execution order of scripts and
- allow you to conditionally determine whether and how an action statement is executed, and the order
Control structures use control statements to control the execution order of scripts and action statements. The benefits of using control structures include:
- clearer and more readable Internet programming
- less time spent debugging, testing and modifying the script
- increased productivity
- improved script quality, reliability and efficiency
Programmers often indent action statements within a control structure to make it easier to identify control statements.
The two types of control structures are:
- Decision Control Structures
- Loop Structures
Decision control structures allow you to alter the execution order of action statements on the basis of conditional expressions. A conditional expression is an expression that is evaluated by the scripting engine as true or false. Loop structures allow you to repeat action statements on the basis of conditional expressions.
Both types of control structures use control statements to alter the order in which action statements are executed. There are three types of control statements:
- Looping statements - allow you to conditionally repeat a block of code
- Branching statements - program decides which block of code to run, based on criteria in the control statement
- Jumping statements - allow you to conditionally exit a code block and jump to another section of code
Algorithms and flowcharts make visually clear the sequence in which a script is executed. It is a good idea to plan and write out the order in which you want the script to be able to execute, before writing the code.