2.0 Subs, Functions, Decisions, Flowcharts Flashcards
modular programming
info needs to be broken down into parts and organized
-computer programs are organized in modules
▪ independent and self-contained
▪ designed for specific well-defined function
▪ focused and short
- a module can have one or more procedures
▪ each procedure small and self-contained
▪ eg. subs and functions
sub procedures
take more than one input parameter
- returns more than one result
- cannot be used in an expression
- general syntax:
Sub name([arglist])
[statements]
[Exit Sub]
[statements]
End Sub
- the argument list of the first (main) Sub to be executed is BLANK
- the arglist Subs that are called by other Subs usually has one or more arguments
- a Sub can call another Sub to perform a part of the task
▪ after Call statement executed, the statements in name2 are executed, then execution resumes in
name1 right before the Call name2 statement
functions
can take more than one input parameter
- returns only ONE RESULT
- can be used in formula to return the result in a cell or in an expression in a Sub
- general syntax:
Function name([arglist]) [statements]
[name = expression] [Exit Function] [statements]
[name = expression]
End Function
structured programming
program statements are executed sequentially
▪ line by line
▪ starting at top and moving down to end
▪ creates limitations and inconveniences
- conventions to impose order on non-sequential algorithms that have been developed
- to allow program to take nonconsequential paths, ALL computer languages include statements
▪ decisions = branching of flow based on decision * 3 basic types
- If/Then/Else structures
- If/Then/Else If Cascade structures
- Select Case structures
▪ loops = looping of flow to allow statements to be repeated
how to tackle a problem while writing a code?
▪ think about the problem you’re trying to solve
▪ outline several possible methods of solutions
▪ select method that seems most clear and concise
▪ construct a detailed step-by-step procedure = an algorithm
flowcharts
for complicated algorithms, where decisions and/or looping occur, it’s more convenient to represent the algorithm with a flowchart
-graphical representation of an algorithm
-helps visualize the flow of the solution process
-must draw a flowchart before starting to write a code
-a series of blocks and arrows
- each block represents particular operation
- arrows represent sequence of those operations
(see photo)
If/Then/Else decision structure
syntax:
If condition Then [TrueStatements]
Else
[FalseStatements]
End If
- condition is a logical expression that evaluates
“TRUE” or “FALSE”
- true statements are one or more statements that are
executed if condition is TRUE
- false statements are one or more statements that are
executed if condition is FALSE
(See Photo)
If/Then block structure
-True statements are one or more statements that are executed if condition is True
-False statements goes straight to junction
loops
when you want to execute a statement or group of statements many times, use Do/Loop structure
“Do/If Exit” and “For/Next”
count-controlled loops
-using “Do/If Exit” to loop pre-specified number of times
-For/Next loop provides a much more efficient means of implementing count-controlled iteration
Nesting
loops that are enclosed completely with other loops
Orders of Priority in Evaluating Operators
Arithmetic: Parenthesis, Exponents, negation, multiplication/division, addition/subtraction
Relational: =, <>, <,>,<=,>=
Logical: Not, And, Or
Relational Operators
= Equals or is equal to
<> Is not equal to
< Is less than
> Is greater than
<= Is less than or equal to
>= Is greater than or equal to
algorithm
a set of instructions which, when followed, will produce a solution to the problem
select case structure
- takes the “If/Then/Else If” to its logical conclusion
- Select Case TextExpression [Case expression list1
[statements]] [Case expression list2
[statements]] [Case Else
[elsestatements]]
End Select
-e.g. grade