Programming Techniques Flashcards
Algorithm
A set of step-by-step instructions to solve a problem
Three programming constructs
Sequences, selection, iteration
Sequences
A series of logical steps that perform an action
Selection (branching)
A decision or question that changes the direction of a program depending on the outcome
Examples of sequences
Print statements and variable initialisation
Examples of selections
If and case statements
Iteration
Iteration is the process of repeating sections of a program
Three types of iteration
Infinite loops, count-controlled loops, condition-controlled loops
Infinite loops
Repeats until the user terminates the program or it crashes
Count-controlled loops
Used to make the program repeat the same thing a specific number of times
Example of count-controlled loop
FOR loop
Condition-controlled loops
Repeated based on a certain condition
Example of condition-controlled loop
WHILE loop
DO WHILE loop
Condition-controlled loop - iteration occurs indefinitely as long as specified condition is met
REPEAT UNTIL loop
Condition-controlled loop - iteration occurs indefinitely until specified condition is met
Scope
The area where a function or variable is visible and accessible to other code
Global variable
Declared outside any function and can be accessed throughout all of the program
Local variable
Declared inside a function and can only be accessed inside of that function
Why are local variables better to use than global variables?
Unnecessary use of global variables can make programs hard to test,debug and maintain