SlideShare a Scribd company logo
1 of 34
PRINCIPLE & TECHNIQUES OF
PROGRAMMING LANGUAGES
With
Basic_Python
AJAYI O. O., PhD
CHAPTER TWO: THE PRINCIPLE OF
GOOD PROGRAMMING STYLE
•PROGRAMMING STYLE
•GENERAL GUIDELINES / RULES GOVERNING
GOOD PROGRAMMING STYLE
•THE STRUCTURE OF QBASIC AND ITS COMMON
I/O STATEMENTS
2.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!
-PROGRAMMING STYLE CONT’D
•Programming styles are usually designed for a
specific programming language / family language.
i. C & C++: Same Style
ii. Basic (QBasic, GW-Basic) & VB: Same Style
iii. Style in C != Style in QBasic
-PROGRAMMING STYLE CONT’D
•BUT:
•We have general principles/rules which
every programming language must
conform with.
2.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
-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).
• 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
• Look at PASCAL:
PROGRAM. AJAYI-PROGRAM (INPUT, OUTPUT)
{* Description : This program calculates CGPA of students*}
-PROGRAM HEADER CONT’D….INSTANCES
• 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
• 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
• And C++, we have it as:
#include<iostream>
using namespace
//You can insert comments here or anywhere
Main()
{
}
-PROGRAM HEADER CONT’D….INSTANCES
-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!
-THE RULES…
•INDENTATION & BRACING STYLE…
If (condition) {
// code block - Your program goes here
}
The above is called IMMEDIATE Bracing Style
-THE RULES…
•INDENTATION & BRACING STYLE…
If (condition)
{
// code block - Your program goes here
}
The above is called DIRECT Bracing Style
-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.
-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;
-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.
-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.
-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
-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*/
2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS
• THE STRUCTURE OF QBASIC…
•QBasic is a procedural language. Unlike object-
oriented language, it has no standard structure
according to programming style rule. Howbeit, it
does have structure for programmers to follow.
2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
•GUIDE TO QBASIC STRUCTURE
i. Line Number: Programs in QBasic are easily written
with line numbers for readability purpose.
ii. CLS: This means Clear Screen
iii. REM: Implies REMark – Comment.
iv. END: To signify the end of a program
2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GUIDE TO QBASIC STRUCTURE- ILLUSTRATION
• For example, the program below does nothing but shows the guide
to QBasic Structure:
5 CLS
10 REM
15 END
OR
2 CLS
4 REM
6 END
2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GARBAGE IN & OUT IN QBASIC
• INPUT STATEMENT
• To feed/supply data unto the computer system using QBasic,
we make use of any of the following two commands:
INPUT statement
READ&DATA statement
2.3 THE STRUCTURE OF QBASIC AND
ITS COMMON I/O STATEMENTS…
• GARBAGE IN & OUT IN QBASIC
• OUTPUT STATEMENTS
• These are statements that are used for fetch/retrieve/show
to screen, inputted or stored data from the computer
system.
• The major/basic output statement used in QBasic is called
PRINT.
ILLUSTRATIONS
• Coding Examples
• Open your System and See the Examples to be done on
the board NOW!
EXAMPLE 1
•Code in QBasic to output the literals:
BIO-DATA Quiz
HE was born and brought up in Lagos.
He hails from Kwara.
He settles down in Ondo.
Who is He?
SOLUTION
5 CLS
10 REM
15 Print “BIO-DATA Quiz”
20 Print “_____________”
25 Print “He was born and brought up in Lagos.”
30 Print “He hails from Kwara.”
35 Print “He settles down in Ondo.”
40 Print
45 Print “Who is He?”
50 END
EXAMPLE 2
•Given two variables: birthyear and
currentyear, input values for the two
variables and compute your AGE by
finding the difference of the two
variables.
SOLUTION
5 CLS
10 REM
15 INPUT birthyear
20 INPUT currentyear
25 yourage = currentyear – birthyear
30 PRINT currentyear-birthyear
35 END
EXAMPLE 3
•Given:
y = 18, u = 21, r = 11
Code in QBasic to compute:
V = y + (u – r)
SOLUTION
5 CLS
10 REM
15 READ y, u, r
20 DATA 15, 210, 11
30 Let V = (y + (u-r))
35 PRINT “The answer is: ”, V
40 END
TAKE HOME
•Check your manual, solve and print the
output of the questions in Lab Exercise 1, 2
and 3 (Pages 45 & 46)

More Related Content

What's hot

Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in PerlIan Kluft
 
PL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and ScopePL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and ScopeSchwannden Kuo
 
Programming language
Programming languageProgramming language
Programming languageDhani Ahmad
 
javabasics_ programming development chapter01
javabasics_ programming development chapter01javabasics_ programming development chapter01
javabasics_ programming development chapter01Udeshg90
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesSchwannden Kuo
 
Code: from Procedural to Object Oriented
Code: from Procedural to Object OrientedCode: from Procedural to Object Oriented
Code: from Procedural to Object Orientedkleinron
 

What's hot (9)

Code Generation in Perl
Code Generation in PerlCode Generation in Perl
Code Generation in Perl
 
PL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and ScopePL Lecture 02 - Binding and Scope
PL Lecture 02 - Binding and Scope
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Programming language
Programming languageProgramming language
Programming language
 
javabasics_ programming development chapter01
javabasics_ programming development chapter01javabasics_ programming development chapter01
javabasics_ programming development chapter01
 
PL Lecture 01 - preliminaries
PL Lecture 01 - preliminariesPL Lecture 01 - preliminaries
PL Lecture 01 - preliminaries
 
Programming paradigms
Programming paradigmsProgramming paradigms
Programming paradigms
 
Code: from Procedural to Object Oriented
Code: from Procedural to Object OrientedCode: from Procedural to Object Oriented
Code: from Procedural to Object Oriented
 
Language Workbenches
Language WorkbenchesLanguage Workbenches
Language Workbenches
 

Similar to DMA Chap2

STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2Bro Shola Ajayi
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdfKalighatOkira
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterHossam Hassan
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02riddhi viradiya
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxEng Teong Cheah
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptANISHYAPIT
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 

Similar to DMA Chap2 (20)

STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2
 
Computer programming
Computer programmingComputer programming
Computer programming
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Coding
CodingCoding
Coding
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Basics of C Prog Lang.pdf
Basics of C Prog Lang.pdfBasics of C Prog Lang.pdf
Basics of C Prog Lang.pdf
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
Qbasic program
Qbasic programQbasic program
Qbasic program
 
Qbasic
QbasicQbasic
Qbasic
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
Embedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals masterEmbedded c c++ programming fundamentals master
Embedded c c++ programming fundamentals master
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
Learn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic SyntaxLearn C# programming - Program Structure & Basic Syntax
Learn C# programming - Program Structure & Basic Syntax
 
Coding
CodingCoding
Coding
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 

More from Bro Shola Ajayi

More from Bro Shola Ajayi (13)

DMA113 Chap1
DMA113 Chap1DMA113 Chap1
DMA113 Chap1
 
CSC431_Chap1
CSC431_Chap1CSC431_Chap1
CSC431_Chap1
 
Csc413 chap1
Csc413 chap1Csc413 chap1
Csc413 chap1
 
Csc303 part1 chap1
Csc303 part1 chap1Csc303 part1 chap1
Csc303 part1 chap1
 
INTRO TO SQL
INTRO TO SQLINTRO TO SQL
INTRO TO SQL
 
Intro to Web Prog
Intro to Web ProgIntro to Web Prog
Intro to Web Prog
 
Database: An Intro
Database: An IntroDatabase: An Intro
Database: An Intro
 
Database: An Introduction
Database: An IntroductionDatabase: An Introduction
Database: An Introduction
 
Seminar on cgpa calculation
Seminar on cgpa calculationSeminar on cgpa calculation
Seminar on cgpa calculation
 
CSC426 - SDLC Models
CSC426 - SDLC ModelsCSC426 - SDLC Models
CSC426 - SDLC Models
 
CSC426 - Software Engineering Lecture Note Cont'd
CSC426   - Software Engineering Lecture Note Cont'dCSC426   - Software Engineering Lecture Note Cont'd
CSC426 - Software Engineering Lecture Note Cont'd
 
CSC426 - Software Engineering Lecture Note
CSC426   - Software Engineering Lecture NoteCSC426   - Software Engineering Lecture Note
CSC426 - Software Engineering Lecture Note
 
Requirement analysis
Requirement analysisRequirement analysis
Requirement analysis
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

DMA Chap2

  • 1. PRINCIPLE & TECHNIQUES OF PROGRAMMING LANGUAGES With Basic_Python AJAYI O. O., PhD
  • 2. CHAPTER TWO: THE PRINCIPLE OF GOOD PROGRAMMING STYLE •PROGRAMMING STYLE •GENERAL GUIDELINES / RULES GOVERNING GOOD PROGRAMMING STYLE •THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS
  • 3. 2.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. i. C & C++: Same Style ii. Basic (QBasic, GW-Basic) & VB: Same Style iii. Style in C != Style in QBasic
  • 5. -PROGRAMMING STYLE CONT’D •BUT: •We have general principles/rules which every programming language must conform with.
  • 6. 2.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
  • 7. -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).
  • 8. • 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
  • 9. • Look at PASCAL: PROGRAM. AJAYI-PROGRAM (INPUT, OUTPUT) {* Description : This program calculates CGPA of students*} -PROGRAM HEADER CONT’D….INSTANCES
  • 10. • 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
  • 11. • 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
  • 12. • And C++, we have it as: #include<iostream> using namespace //You can insert comments here or anywhere Main() { } -PROGRAM HEADER CONT’D….INSTANCES
  • 13. -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!
  • 14. -THE RULES… •INDENTATION & BRACING STYLE… If (condition) { // code block - Your program goes here } The above is called IMMEDIATE Bracing Style
  • 15. -THE RULES… •INDENTATION & BRACING STYLE… If (condition) { // code block - Your program goes here } The above is called DIRECT Bracing Style
  • 16. -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.
  • 17. -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;
  • 18. -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.
  • 19. -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.
  • 20. -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
  • 21. -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*/
  • 22. 2.3 THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS • THE STRUCTURE OF QBASIC… •QBasic is a procedural language. Unlike object- oriented language, it has no standard structure according to programming style rule. Howbeit, it does have structure for programmers to follow.
  • 23. 2.3 THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS… •GUIDE TO QBASIC STRUCTURE i. Line Number: Programs in QBasic are easily written with line numbers for readability purpose. ii. CLS: This means Clear Screen iii. REM: Implies REMark – Comment. iv. END: To signify the end of a program
  • 24. 2.3 THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS… • GUIDE TO QBASIC STRUCTURE- ILLUSTRATION • For example, the program below does nothing but shows the guide to QBasic Structure: 5 CLS 10 REM 15 END OR 2 CLS 4 REM 6 END
  • 25. 2.3 THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS… • GARBAGE IN & OUT IN QBASIC • INPUT STATEMENT • To feed/supply data unto the computer system using QBasic, we make use of any of the following two commands: INPUT statement READ&DATA statement
  • 26. 2.3 THE STRUCTURE OF QBASIC AND ITS COMMON I/O STATEMENTS… • GARBAGE IN & OUT IN QBASIC • OUTPUT STATEMENTS • These are statements that are used for fetch/retrieve/show to screen, inputted or stored data from the computer system. • The major/basic output statement used in QBasic is called PRINT.
  • 27. ILLUSTRATIONS • Coding Examples • Open your System and See the Examples to be done on the board NOW!
  • 28. EXAMPLE 1 •Code in QBasic to output the literals: BIO-DATA Quiz HE was born and brought up in Lagos. He hails from Kwara. He settles down in Ondo. Who is He?
  • 29. SOLUTION 5 CLS 10 REM 15 Print “BIO-DATA Quiz” 20 Print “_____________” 25 Print “He was born and brought up in Lagos.” 30 Print “He hails from Kwara.” 35 Print “He settles down in Ondo.” 40 Print 45 Print “Who is He?” 50 END
  • 30. EXAMPLE 2 •Given two variables: birthyear and currentyear, input values for the two variables and compute your AGE by finding the difference of the two variables.
  • 31. SOLUTION 5 CLS 10 REM 15 INPUT birthyear 20 INPUT currentyear 25 yourage = currentyear – birthyear 30 PRINT currentyear-birthyear 35 END
  • 32. EXAMPLE 3 •Given: y = 18, u = 21, r = 11 Code in QBasic to compute: V = y + (u – r)
  • 33. SOLUTION 5 CLS 10 REM 15 READ y, u, r 20 DATA 15, 210, 11 30 Let V = (y + (u-r)) 35 PRINT “The answer is: ”, V 40 END
  • 34. TAKE HOME •Check your manual, solve and print the output of the questions in Lab Exercise 1, 2 and 3 (Pages 45 & 46)