You are on page 1of 18

SOE 201:

INTRODUCTION TO SOFTWARE ENGINEERING

PROGRAMMING PRACTICES
Definition of Terms
• Computer Program
• A specific set of ordered instructions given to the computer to direct its
operations.
• Computer Programming
• An act of writing computer programs that will perform some specific tasks.
• Programming Language
• A language that can be used to write computer programs.
• Syntax
• Rules that define and control the structure and organization of a
programming language.
• Semantics
• The meaning or logic of a programming language’s syntax.
Programming Practices by Dr. Caesar Nwandu 2
Programming Language Groups
A. Low Level Languages
• Are machine specific, i.e. program written on Computer-A cannot run on
Computer-B and vice versa.
• Include Machine Language and Assembly Language.
⎯Machine Language programs are binary codes (0’s and 1’s).
• They are hard to read and understand.
⎯Assembly language syntax are symbolic abbreviations of English
Language words, known as mnemonics.
• Mnemonics must be converted to binary (using Assembler)
before execution.

Programming Practices by Dr. Caesar Nwandu 3


Programming Language Groups
B. High Level Languages (HLL)
• They are not machine specific. Instead they are machine
independent, i.e. Computer-A program can run on Computer-B.
• Examples include BASIC, FORTRAN, Pascal, C, C++, C#, Java, Python,
etc.
• HLLs use natural languages for writing programs.
• HLL programs must be converted to binary codes before execution.
⎯HLL program conversion can either be Interpretation or Compilation.
⎯Interpretation is done by an Interpreter. Interpreter translates and executes
programs line by line. (Used in JavaScript, Python, etc.)
⎯Compilation is done by a Compiler. Compiler translates entire program
before execution begins. (Used in Java, C/C++, etc.)

Programming Practices by Dr. Caesar Nwandu 4


Programming Concepts
1. Data Types
• These are program entities and the formats in which they exist and operate.
They are of 2 categories: Primitive data types and User-defined data types:
a. Primitive data types: Are fundamental, language-specific data.
• Integer:- Whole numbers. Integer may have variants, depending on the language. For
instance, Java has short, int, long variants. E.g. int Age = 12
• Decimal:- Definite/accurate representation of data especially business data. E.g. double
Amount = 20.5
• Floating point:- Real number representation. E.g. float Time = 15.6f
• Character:- Values stored as bytes (8-bits or 16-bits); more like a word. E.g. char color
• Boolean:- Data that has only two outcomes - yes/no, true/false, 0/1, etc. E.g. bool
gender
• String:- A sequence or array of characters; more like a sentence. Represented in single
or double quotes. E.g. string str = “This is a program”
Programming Practices by Dr. Caesar Nwandu 5
Programming Concepts

Data types ctd.


b. User-defined data types: Are data whose range of values are
defined by the programmer
• E.g. Enumerated data:
Enum Alarm = {Mon, Tue, Wed, Thu, Fri, Sat, Sun}. This can
be used to set the weekdays in which an alarm should
ring.
Enum Fighters = {dog, leopard, lion, tiger}. This is a list of
some carnivorous animals that belong to dog family.

Programming Practices by Dr. Caesar Nwandu 6


Programming Concepts
2. Control Structures
• These are programming tactics that handle situations that require logical tests, called
conditional execution. There are 3 types of control structures:
a. Sequential control structure: Execution of program one line after the other.
b. Selection control structure: Making choice among several outcomes of a logical
test.
i. Unconditional Selection: Automatic transfer of execution from one point to the other. E.g.
GoTo
ii. Selection between 2 or 3 options: Execution that depends on 2 or 3 outcomes. E.g. True/False,
Negative/Zero/Positive, Good/Fair/Bad, etc. usually implemented with IF-THEN-ELSE
iii. Selection among several options: Execution depends on any of several possible outcomes of
the logical test. There is an option of “no choice” in case none of the possibilities is true. E.g.
CASE
c. Iteration (or Loop) control structure: A group of program statements (a segment) is
executed repeatedly until a condition is met. E.g. REPEAT, FOR, WHILE, WHILE-IF

Programming Practices by Dr. Caesar Nwandu 7


Unconditional Selection: Unconditional goto
• void checkNum(int num)
{
num = num + 2;
X = num;
return;
goto end;
If (num / 2 ==0)
print (“The number is not valid”);
End:
print (“The final value of the number is ”, x);
}

Programming Practices by Dr. Caesar Nwandu 8


Selection between 2 options: IF-THEN-ELSE
void Score_Manip(int score, Result)
{
if (score >= 40)
Result = pass;
printf(“Result is pass”, score);
return;
else
Result = fail;
printf(“Result is fail”, score);
}

Programming Practices by Dr. Caesar Nwandu 9


Selection between 3 options: NESTED-If
if (totalScore >= 40 && totalScore < 70) {
remark = ‘Good’;
} else if (totalScore >= 70) {
remark = ‘Excellent’;
} else {
remark = ‘Fail’;
}
System.out.println(“The student’s result is ”,
remark);
)

Programming Practices by Dr. Caesar Nwandu 10


Selection among several options: CASE
Switch(grade == A) {
case 1:
cout<<”Case1: Grade is = ”<<grade<<endl;
break;
case 2:
cout<<”Case2: Grade is = ”<<grade<<endl;
case 3:
cout<<”Case3: Grade is = ”<<grade<<endl;
case 4:
cout<<”Case4: Grade is = ”<<grade<<endl;
default:
cout<<”Default: Grade is = ”<<grade<<endl;
}
return 0;

Programming Practices by Dr. Caesar Nwandu 11


Iteration: WHILE
int i = 0;
while(i < 6) {
i = i + 2;
System.out.println(i);
i++;

Programming Practices by Dr. Caesar Nwandu 12


Iteration: FOR
for(int i = 1; i <= 6; i++) {
i = i + 4;
System.out.println(i);

Programming Practices by Dr. Caesar Nwandu 13


Iteration: WHILE-IF
int score = 0
char = grade
while True:
score = eval(input(“Please enter score: ”))
if score >= 70:
grade == ‘A’
print(“The score grade is ” + grade)
break
Programming Practices by Dr. Caesar Nwandu 14
Programming Concepts
3. Modularization
• Also called sub-programming, is the process of breaking down (complex)
programs into smaller units known as modules or subprograms or
components.
• Modules can be executed in the form of a block or a co-routine.
a. Block: This is usually a program unit whose outcome is invisibly used to
complete the execution of the main program. A block is triggered when a
certain condition is encountered as program execution progresses.
b. Co-routine: This is a module which can be called by other modules and vice
versa. Once a co-routine invokes another, it suspends its own direct
execution, which will be resumed when needed by the called co-routine at
the exact point where it had been suspended.

Programming Practices by Dr. Caesar Nwandu 15


Programming Concepts
4. Concurrent Units
• These are program units which engage in overlapping executions, i.e. a module can
begin execution when another one has not finished. They copy the functioning style
of operating systems.
5. Exception Handling
• This points to a condition in which a program meets an error leading to a sudden
termination e.g. division by zero, running out of memory, etc. Exception handler
takes care of such unexpected anomalies.
6. Parameter Passing
• This is the transfer of data from one module to the other, typically to communicate
data values among themselves as they call themselves. E.g.
PROCEDURE S(F1, F2, F3, …, Fn)
CALL S(A1, A2, A3, …, An)
• Formal parameters Fi are bound to Actual parameters Ai.
• Data will be transferred from Ai to Fi as they are being received as inputs.
Programming Practices by Dr. Caesar Nwandu 16
Categories of Programming Languages
Programming languages are categorized according to the method and structure of
the language syntax and semantics;
1. Imperative Languages: They are simple in nature and are built to work with
statements, variables, assignments, and control structures. E.g. Ada, Small Talk,
COBOL, FORTRAN, Pascal, C, etc.
2. Functional Languages: They present programs as series of functions (i.e.
segmented expressions). This is meant to add readability and understandability.
E. g. LISP
3. Logic Languages: They make use of rules to give out computer instructions,
which must be satisfied before an execution is done. E.g. ProLog.
4. Mark-up Languages: These are not languages per say. They refer to organized
annotation systems used in computer text processing. They make use of tags to
mark certain parts of a document as different from plain text usually in specifying
web contents. E.g. HTML, XHTML, XML, PHP, etc.
5. Object-Oriented Languages: They model real-life entities as objects and analyze
life scenarios using the object states and behaviours. E.g. C++, Java, Python, etc.

Programming Practices by Dr. Caesar Nwandu 17


Review Questions

Programming Practices by Dr. Caesar Nwandu 18

You might also like