2. CHAPTER TWO: THE PRINCIPLE OF
GOOD PROGRAMMING STYLE
• PROGRAMMING STYLE
• GENERAL GUIDELINES / RULES GOVERNING GOOD PROGRAMMING
STYLE
• THE STRUCTURE OF C; ITS DATA TYPES & COMMON I/O STATEMENTS
• THE STRUCTURE OF PYTHON; ITS DATA TYPES & COMMON I/O
STATEMENTS
• THE STRUCTURE OF C#; ITS DATA TYPES & COMMON I/O STATEMENTS
3. 1.1 PROGRAMMING STYLE
•Programming style can be regarded as
guiding rules that govern writing of source
code for a computer program.
•Programming Style = English Composition
•With good programming style, debugging is
never a bug to the debugger!
4. -PROGRAMMING STYLE CONT’D
•Programming styles are usually designed for a
specific programming language / family language.
•C & C++: Same Style
•Basic (QBasic, GW-Basic) & VB: Same Style
•Style in C != Style in QBasic
7. 1.2 GENERAL GUIDELINES / RULES
GOVERNING GOOD PROGRAMMING STYLE
1. PROGRAM HEADER
2. INDENTATION & BRACING STYLE
3. USE OF APPROPRIATE VARIABLE NAMES
4. CONSISTENCY IN THE USE OF VARIABLE NAMES
5. NON-USAGE OF KEYWORD/RESERVED WORD
8. -THE RULES…
•THE PROGRAM HEADER
This rule says that, every good programming
language must have, at or near the beginning
of the program, a header block indicating the
program filename, program library header,
program purpose and other identifications
that pertain to the program (e.g. the
comment block).
9. • In COBOL, we have:
PROGRAM-ID. AJAYI-PRO.
PROGRAM-NAME. AJAYI-PROGRAM.
SOURCE-MACHINE. IBM-370.
OBJECT-MACHINE. IBM-370.
/* This program calculates CGPA of students*/
-PROGRAM HEADER CONT’D….INSTANCES
10. • Look at PASCAL:
PROGRAM. AJAYI-PROGRAM (INPUT, OUTPUT)
{* Description : This program calculates CGPA of students*}
-PROGRAM HEADER CONT’D….INSTANCES
11. • Consider JAVA:
Public Class AJClassPro
{
Public Static Void Main (String args[ ])
{
/* This program calculates CGPA of students */
// 2nd version of the commenting style
} }
-PROGRAM HEADER CONT’D….INSTANCES
12. • Now to C:
#include<stdio.h>
/* This program calculates CGPA of students */
OR
#include<stdio.h>
#include<stdlib.h>
/* This program calculates CGPA of students */
-PROGRAM HEADER CONT’D….INSTANCES
13. ‘GET TO LIBRARY’
•Describe the compliance of the under-listed
programming languages in terms of the
program header rule:
i. C++
ii. PHP
iii. RUBY
iv. ALGOL
v. ADA
14. ‘PIN POINT’
•Why is QBasic not considered as a
standard programming language, but
rather, a teaching and learning
language?
15. -THE RULES…
• INDENTATION & BRACING STYLE
Indentation helps in identifying errors in control flow
statements as well as in blocks of codes.
In a well and properly indented program, errors in
codes are easily traced.
It enhances program readability!
16. -THE RULES…
•INDENTATION & BRACING STYLE…
If (condition) {
// code block - Your program goes here
}
The above is called IMMEDIATE Bracing Style
17. -THE RULES…
•INDENTATION & BRACING STYLE…
If (condition)
{
// code block - Your program goes here
}
The above is called DIRECT Bracing Style
18. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES
The use of appropriate variable name is very
important in a good programming style. Poorly-
named variables make code (program) difficult to
read and understood.
19. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES…
Consider this:
get a, b, c
if a < 24 && b < 60 && c < 60
return true;
else
return false;
20. -THE RULES…
•USE OF APPROPRIATE VARIABLE NAMES…
Now compare with this:
get hrs, mins, secs
if hrs < 24 && mins < 60 && secs < 60
return true;
else
return false;
The latter enhances readability and understandability. However, the former can be made
better by including comments indicating/conveying the variables’ representation.
21. -THE RULES…
•CONSISTENCY IN THE USE OF VARIABLE NAMES
Consistency in the use of variable name implies
sticking to the use of a set of variable names
throughout your program, regardless of the case-
sensitivity or otherwise of the language.
22. -THE RULES…
• CONSISTENCY IN THE USE OF VARIABLE NAMES…
e.g.
int sum;
float avg;
SUM = (num1 + num2) **2
avg = Sum/2
average = average + 1
23. -THE RULES…
• NON-USAGE OF KEYWORD/RESERVED WORD
For a good programming style, a programmer should as much
as possible avoid the use of words that are close to or that are
reserved words or keywords. The best way of not entering into
this trap or error is by being acquainted with the language’s
keywords or reserved words.
e.g.
int get; /*where get is a reserved word in C*/
24. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
•THE STRUCTURE OF C
•C language comprises majorly of three (3)
structures/components. These are:
- Program header
- Declaration section
- Program Statement (Simple/Compound, enclosed in brace(s))
25. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• Program header
#include<stdio.h>
#include<stdlib.h>
{
……..code goes here!
}
26. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
•THE STRUCTURE OF C…
•Declaration Section
/* declaration section */
int x;
float y;
27. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• Declaration Section….fuller version
#include<stdio.h>
#include<stdlib.h>
{
int x;
float y;
…real coding goes here!
…on and on, as much as your finger can type!
}
28. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• Program Statement
This could be as simple as simple statement:
• Sum = x + y
OR
…as compound as the compound statement
{
Sum = x + y
Avg = Sum/2
}
29. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• Program Statement….Expression
EXPRESSION
Expression forms statements. The following expressions are used
in C Language:
30. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• Program Statement….Expression
Arithmetic expression
+, -, *, /, ^, **
Relational expression
<, >, <=, >=, <>, !=
Logical expression
OR, AND, = =, !!, &&
Assignment Expression
=
Incremental/Decremental Value
++ (INCREMENTAL – increment a value by 1); --(DECREMENTAL – decrement a value by
1)
31. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• DATA TYPES
We have:
i. int /* meaning integer value - i */
ii. float /* meaning float/real data type - f */
iii. double /* meaning float/real data type - d */
iv. string/char /* meaning alphanumeric data type - c/s */
32. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• COMMON I/O STATEMENTS
INPUT STATEMENT
• Scanf: This is an input statement that is used to supply value from the
computer screen unto computer memory. This is equivalent of the Input
Statement in Basic program.
Scanf (“%datatype”, &variable name);
e.g. Scanf (“%f”, &area);
33. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• COMMON I/O STATEMENTS
OUTPUT STATEMENT
• Printf: This is an output statement that retrieves processed data
from the computer memory and output to the computer screen.
Printf (“%f”, area); /*for values of variable*/
Printf (“Enter value for length: ”); /*for literals*/
Printf (“Enter value for breadth: ”); /*ditto*/
34. -THE STRUCTURE OF C; ITS DATA TYPES
AND COMMON I/O STATEMENTS
• THE STRUCTURE OF C…
• COMMON I/O STATEMENTS
Class Exercises
1. Write a C program that captures the values of three (3)
variables: m, n, q; computes and output the sum of their
squares.
2. Write a C program that accepts the value of a variable, x, detects
its even and prime status.