Programming construct Flashcards
(60 cards)
Variables (meaning and indication for memory use)
> Variable is a name to refer to a particular memory location that is used to store data.
Variables have data types:( String, Boolean, integer, or decimal ) which will indicate how much memory they will use and the type of data they will store.
Procedural language
> .upper() > >lower() > .string slicing() > print() > .length > .substring
Selection statements (meaning)
Selection statements carry out a set of commands on a condition. This allows your code to make choices - “Branching”
Selection - IF statements
IF [condition] then [code for true] endif ------------------------------------------------------------------------------------------ IF [condition] then [code for true] else [code for false] endif
Selection - IF statements with multiple branch
IF [condition] then [code for true] elseif [condition 1 ] then [code for true 1 ] else [code for false, false 1] endif
selection - switch case
switch entry: case "A": print(................) case "B": print(................) default: print(..........................) endswitch
Relational operators
" == " " Equal to" " != " "Not equal to" " < " " less than" " <= " " less or equal to " " > " " greater than " " >= " " greater or equal to "
logical operators
> AND = Return true if both conditions are true
OR = Return true if any of the conditions are true
NOT = returns the out come of the expression ,true becomes false and false becomes true
Select…… Case…… End case (pseudocode)
print("1. add number") print("2. subtract numbers") print("3.quit") choice = input("enter your choice") switch choice case "1" print("adding numbers chosen") case "2" print("subtracting numbers chosen") case "3" print("quit chosen") default: print("pick between choice 1 ,2 and 3") endswitch
sequence
Sequence is when the algorithm is in logical order in step by step format
Iteration/loops
An Iteration is a section of code that gets repeated
counter controlled iterations(meaning and
> counter controlled iterations are used when you want to illiterate a known number of times
Counter controlled iterations use a variable which can be used within the code to know how many times the code has been run.
Code is indented within an iteration to make it easier to read.
For… Next (Pseudocode)
For counter = 1 to 5
print(counter)
Next counter
Condition Controlled loops
Condition-controlled iteration repeatedly executes a section of code until a condition is met - or no longer met.
While… Loop
counter=0 While counter < 3 print(counter) counter=counter + 1 endwhile
Do…Until loop
counter=0 Do print(counter) counter=counter + 1 Until counter == 3
Variable
A name used to refer to a particular memory location that is used to store data.
The value of the data held in that memory location is not known when the program is written and can change while the program is running.
Constant
Defined at the start where it can be changed if necessary and cannot be changed during execution
Makes reading the code more understandable
Eg: Const InterestRate AS Double = 0.2
Local variables
> A variable defined within one part of program or module.
It is only accessible in that part of the program or module.
Data contained is lost when execution of that part of program or module is completed
The same variable names can be used in different modules
Eg: loop counter
Global variables
> A variable that is defined at the start of a program.
It exists throughout program including functions/procedures
Allows data to be shared between modules
Overridden by local variables with the same name
Eg: VAT rate
Difference between Local and Global Variables
Local variable is visible only in module or construct where it is created or declared and Global variable is visible throughout a program or may be accessed from more than one part of the program
Why good programming practice generally avoids the use of global variables
> Global variables make it difficult to integrate modules and they increase complexity of a program.
Global variables may cause conflicts with names written by others in other modules
Parameters
> Information about an item of data supplied or transferred to a function or procedure.
Pass values between functions and procedures.
Can be passed by reference or by value
Used as a local variable if it is BY Val
Why parameter passing to a function can be a better alternative to using global variables.
> When a parameter is passed to a function, only a copy of the data passed may be changed therefore no unforeseen effects will occur in other modules.
This is the case only if passed by value.