2.2 Programming Fundamentals
No description yet. This curated deck is ready for daily study.
Card preview (24)
Sequence
Executing one instruction after another
Selection
A program branching depending on a condition IF / ELSE statements.
Iteration
Repeating sections of code - looping.
Types of Iteration
FOR loops WHILE loops
FOR Loops
AKA count-controlled loops are used when the number of iterations needed are known ahead of the iteration executing.
Example of FOR loops
for i in range(3): print(“Hello”) Hello Hello Hello
WHILE Loops
Condition-controlled loops are used when the number of iterations needed are not known beforehand as the variable used to determine that changes.
Example of WHILE loops
count= 10 while count>0: print(count) count = count-1 print(“Blast off”) 10 9 8 7 6 5 4 3 2 1 Blast off
The 4 Data Types
String Integer Float Boolean
Integer
A whole number, positive or negative. Used for calculations.
Real / Float
A number with a decimal point. Used for calculations.
String
A set of characters.
Boolean
TRUE or FALSE. Used to check if something has happened.
Character
A single alphanumeric character.
Casting
Converting a variable from one data type to another. x = int(x)
Variables
A value stored in memory that can change while the program is running.
Constants
A value that does not change while the program is running and is assigned when the program is designed.
Assignment
Giving a variable or constant a value.
Subprograms
small programs that are written within a larger, main program to perform a specific task.
Types of Subprograms
• Functions • Procedures
Functions
Manipulates data and returns a result back to the main program.
Example of Functions
function f_to_c(temperature_in_f) temperature_in_f= (temperature_in_f –32) * 5/9 return temperature_in_c endfunction
Calling a Function
celsius = f_to_c(32)
Procedures
A subprogram that performs a specific task. When the task is complete, the subprogram ends and the main program continues from where it left off.