0% found this document useful (0 votes)
21 views57 pages

DFC20113 - Chapter 1 (Introduction To Fundamentals of Programming)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views57 pages

DFC20113 - Chapter 1 (Introduction To Fundamentals of Programming)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

DFC20113 PROGRAMMING FUNDAMENTALS

CHAPTER 1
INTRODUCTION TO FUNDAMENTALS
OF PROGRAMMING

Presented by: Madam Siti Zaharah binti Sidek


SE LEARNING OUTCO
COUR ME
CLO 1:
IMPLEMENT PROGRAMMING ELEMENT AND ARTICULATE HOW THEY ARE
USED TO ACHIEVE A WORKING PROGRAM. ( C3, PLO 2 )

CLO 2:
SHOW SIMPLE PROGRAMS BY DEVELOPING CODE TO SOLVE PROBLEMS IN
A COMPUTER USING C++ PROGRAMMING LANGUAGE.
( P2, PLO 3 )
LEARNING OUTCOMES
1.1 Define the C++ program basic structure
Describe the item in C++ program structure
- Comments
- Preprocessor directives
- Header files
- main() function
- return statements.
Describe two types of comments that supported by C++
program
Explain coding standard best practices
TOPIC 1.1
Probably the best way to start learning a programming
language is by writing a program.
Therefore, here is our first program:
a) Comment Style
TOPIC 1.1
Entries in the source code which are ignored by the compiler
C++ support 2 style/form of comments:
//Single Line Comments
- for simple 'side' notes
- best places to put these comments are next to variable
declarations, and next to pieces of code that may need
explanation.

/ *block comment */or /*Multi-Line Comments*/


- most useful for long explanations of code.
- useful for two reasons: They make your functions easier to
understand, and they make it easier to spot errors in code.
TOPIC 1.1
Illustrate the valid & invalid comments within a
program.
TOPIC 1.1
b) Pre Processor Directive
What’s a #?
Any line that begins with # symbol is a pre-processor directive
Processed by preprocessor before compiling
What’s pre-processor?
A utility program, which processes special instructions are
written in a C/C++ program.
Include a library or some special instructions to the compiler
about some certain terms used in the program
Different preprocessor directives (commands) perform different
tasks.
Uses for?
It is a message directly to the compiler.
TOPIC 1.1
a) Pre Processor Directive
How to write/syntax ?
- Begin with #
- No semicolon (;) is expected at the end of a preprocessor directive.
Example:
TOPIC 1.1
c) Header File
sometimes known as an include file. Header files almost
always have a .h extension.
Why use?
For big projects having all the code in one file is
impractical. But if we split the project into smaller files,
how can we share functions between them? Headers!
The purpose of a header file?
To hold declarations for other files to use.
When we use the line #include <iostream>, we are
telling the compiler to locate and then read all the
declarations from a header file named “iostream”.
TOPIC 1.1
c) Header File
Consider the following program:

Program never declare cout, so how does the compiler know


what cout is?
The answer is that cout has been declared in a header file called
“iostream”.
TOPIC 1.1
d) Main Function
This line corresponds to the beginning of the definition of
the main function.
A part of every C++ program and identify the start of the
program
Exactly one function in a program must be main
main is a Keyword.
Keyword : A word in code that is reserved by C++
for a specific use.
Header of function main?
int main( )
TOPIC 1.1
d) Main Function
The structure of a main Function:
TOPIC 1.1
e) Curly Braces {}
Body is delimited by braces ({ })
Identify a segment / body of a program
The start and end of a function
The start and end of the selection or repetition block.

Since the opening brace indicates the start of a segment with


the closing brace indicating the end of a segment, there must
be just as many opening braces as closing braces.
TOPIC 1.1
f) C++ statement
What's?
A specification of an action to be taken by the computer as the program executes.
Instruct the program to perform an action
Syntax?
All statements end with a semicolon (;)
Statement has two parts :
Declaration
The part of the program that tells the compiler the names of memory cells in a
program
Executable statements
Program lines that are converted to machine language instructions and executed
by the computer
TOPIC 1.1
g) Return Statement
Means?
The return statement causes the main function to finish.
Return may be followed by a return code (in our example is followed
by the return code with a value of zero).
A return code of 0 for the main function is generally interpreted as
the program worked as expected without any errors during its
execution.
When used?
At the end of main
TOPIC 1.1
g) Return Statement
Two situations for return statement:
a) Return optional in void functions
A void function doesn't have to have a return statement when the end
is reached, it automatically returns.
g) Return Statement
TOPIC 1.1
Two situations for return statement:
b) Return required in non-void functions
If a function returns a value, it must have a return statement that
specifies the value to return. It's possible to have more than one return.
TOPIC 1.1
Coding Standards Best Practices
Refer this link:
https://google.github.io/styleguide/cppguide.html

Importance of following coding standards best practices:


easier for a developer to understand
easier to find and correct bugs
provides a better view of how that code fits within the
larger application
easier for someone to maintain that code.
PROGRAMMING EXERCISE
Ahmad are asked to create a program that can calculate the area of
triangle. He need to identify the IPO, draw a flowchart and create a
program. Then label the program code with C++ program basic
structure. You are asked to help Ahmad.
TOPIC 1.2
Explain identifier and data types
•Explain identifier, variable and constant.
•State the rules for naming an identifier.
•Name the variables according the standards
•Explain the data types.
•List the data types and their respective range with
examples:
•Integer
•Floating point
•Character
•Boolean
•String
IDENTIFIER is refer to a unique combination of letters
and digits, either short or descriptive, that are used to
identify a variable, method, class or any other object in
a programming language uniquely.

VARIABLE is identifier whose value can change during


the course of execution of a program.

CONSTANTS are values that do not change during


program execution. They can be any type of integer,
character or floating-point.
IDENTIFIER VARIABLE
refer to a unique combination of letters is a location in memory which:
and digits, either short or descriptive, Ø we can refer to by an identifier,
that are used to identify a variable, Ø in which a data value that can be
method, class or any other object in a change, and
programming language uniquely. Ø where value can be stored

Example: The value of a variable could be


int number; changed while the program is running.
number is an identifier used as a Declaring a variable means specifying
program variable both its name and its data type.

void CalculateTotal(int value)


CalculateTotal is an identifier
used as a function name.
CONSTANTS LITERAL CONSTANT
Entities that appear in the program Literals are the most obvious kind of
code as fixed values. constants.
Any attempt to modify a CONSTANT They are used to express particular
will result in error. values within the source code of a
Declared using the const qualifier program.
Must be initialized with a constant e.g:
expression when they are declared int a=5;
and cannot be modified char grade=‘A’;
Example: float tax = 2.50;
const int size = 5;
DEFINE CONSTANT DECLARED CONSTANT
We can define our own names for With the const prefix you can declare
constants that we use very often constants with a specific type in the
without having to resort to memory- same way as you would do with a
consuming variables, simply by using variable:
the #define preprocessor directive.
Its format is: const int pathwidth = 100;
#define identifier value const char grade = ‘A';
Examples:
#define PI 3.14159 They are treated just like regular
#define GRAVITY 9.8 variables except that their values cannot
be modified after their definition.
NAMING
CONVENTION RULES
FOR IDENTIFIER
WHY VARIABLES NEEDED IN
PROGRAMMING?
Ø Variables are used to store information to be referenced
and manipulated in a computer program.

Ø They also provide a way of labeling data with a descriptive


name, so our programs can be understood more clearly by
the reader and ourselves.

Ø It is helpful to think of variables as containers that hold


information.
BASIC DATA TYPES
Numeric Non-Numeric

Lorem ipsum dolor sit


int amet, consectetur string
- whole number, positive - sequence of characters
adipiscing elit, sed do
and negative - 10 bytes + string length
eiusmod
03 04
- 4 bytes

Lorem ipsum dolor sit


double
amet, consectetur char

02 05
- floating point numbers - single character
adipiscing elit, sed do
- 8 bytes -1 byte
eiusmod

float
01 06
bool
- floating point numbers - boolean value (true/false)
- 4 bytes - 1 byte
CHARACTER
• Equivalent to ‘letters’ in English language
• Example of characters:
- Numeric digits: 0 - 9
- Lowercase/uppercase letters: a - z and A - Z
- Space (blank)
- Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc.
• Single character
• Keyword: char
• Sample values:
‘B’ ‘d’ ‘4’ ‘?’ ‘*’
INTEGER
• used to declare numeric program variables of integer type
• whole numbers, positive and negative
• keyword: int
• Example code :

• Sample values:
4578 -4578 0
DOUBLE
• used to declare floating point variable of higher precision or
higher range of numbers
• exponential numbers, positive and negative
• keyword: double
• Example code :

• Sample values:
45.000500 5.9999999 0.550
FLOAT
• Fractional parts, positive and negative
• Real numbers with a decimal point
• Keyword: float
• Example code:

• Sample values
95.274 95.5 2.265
FLOAT
The bool data type is capable of holding a Boolean
value.
Objects of type bool may have only the values true or
false.
Keyword: bool
Example code:
STRING
A string is a sequence of characters enclosed in double
quotes (" ").
String sample values:
“Hello” “Year 2000” “1234”
The empty string (null string) contains no characters
and is written as “ ” .
Keyword: string
Example code:
string name = “Ahmad Albab”;
CODE PROGRAM
CODE PROGRAM
TOPIC 1.3
•IDENTIFY THE BASIC OF COMPUTER PROGRAM

•Describe the features of C++ language.


•Develop C++ program using Integrated Development Enviroment
(IDE).
•Get started with IDE
•Create a project file
•Create a simple C++ program
•Compile a C++ program
•Run a C++ program
•FEATURES OF C++ LANGUAGE
C++ is object oriented programming language and it is a very simple and
easy language, this language have following features.
Simple
Every C++ program can be written in simple English language so that it is
very easy to understand and developed by programmer.

Portability
It is the concept of carrying the instruction from one system to another
system. In C++ Language .cpp file contain source code, we can edit also
this code. .exe file contain application, only we can execute this file. When
we write and compile any C++ program on window operating system that
program easily run on other window based system.

Powerful
C++ is a very powerful programming language, it have a wide verity of
data types, functions, control statements, decision making statements,
etc.
Platform dependent
A language is said to be platform dependent whenever the program is
execute in the same operating system where that was developed and
compiled but not run and execute on other operating system. C++ is
platform dependent language.

Object oriented
This main advantage of C++ is, it is object oriented programming
language. It follow concept of oops like polymorphism, inheritance,
encapsulation, abstraction.

Case sensitive
C++ is a case sensitive programming language. In C++ programming
'break and BREAK' both are different.
Compiler based
C++ is a compiler based programming language that means without
compilation no C++ program can be executed. First we need compiler to
compile our program and then execute.

Syntax based language


C++ is a strongly tight syntax based programming language. If any
language follow rules and regulation very strictly known as strongly tight
syntax based language.

Use of Pointers
Pointers is a variable which hold the address of another variable, pointer
directly direct access to memory address of any variable due to this
performance of application is improve.
•INTEGRATED DEVELOPMENT ENVIRONMENT
An integrated development environment (IDE) is a software application that
provides comprehensive facilities to computer programmers for software
development.

All of the tools for


writing, compiling and
testing code in one IDE consists:
place. 1. a source code editor
2. a compiler and/or an interpreter
3. build automation tools
4. a debugger
•GET STARTED WITH IDE (DEV C++)

Compile & Run


New File Compile
Run/Execute
ACTIVITY 1
Open your Dev C++ program.
Write a program to calculate the area of triangle and labelled
the program with C++ program basic structure by using
comment.
Compile, run and display the output.
ACTIVITY 2
Open your Dev C++ program.
Write a program to accept 5 numbers. Then calculate the
total and average of numbers.
Compile, run and display the output.
TOPIC 1.4
•DESCRIBE THE COMPILING AND DEBUGGING PROCESS
AND ERRORS IN PROGRAMMING
Describe the compiling process of a program
Source code
Compiler
Linker
Executable file
Describe with example the errors in programming:
Syntax/ compile time errors
Run time errors
Logical errors
Identify effective debugging process
Debug simple programs to demonstrate syntax/ compile time,
run time and logical error
•COMPILING PROCESS
A compiler is a computer program (or set of programs) that transforms source
code written in a computer language (the source language) into another
computer language (the target language, often having a binary form known as
object code). The most common reason for wanting to transform source code is
to create an executable program.
COMPILING PROCESS
How C++ Works?
•ERRORS IN PROGRAMMING
COMPILE TIME/SYNTAX ERRORS
Grammar errors in the use of the programming
language.

Example:
a. Misspelled variable and function names.
b. Missing semicolons (;)
c. Improperly matches parentheses, square brackets[ ],
and curly braces{ }
d. Incorrect format in selection and loop statements
RUN TIME ERRORS
Occur when a program with no syntax errors asks the
computer to do something that the computer is unable to
reliably do.

Example:
a. Trying to divide by a variable that contains a value of zero
b. Trying to open a file that doesn't exist
c. There is no way for the compiler to know about these kinds
of errors when the program is compiled.
LOGICAL ERRORS
Logic errors occur when there is a design flaw in your
program.

Example:
a. Multiplying when you should be dividing
b. Adding when you should be subtracting
c. Opening and using data from the wrong file
d. Displaying the wrong message
•SPECIAL CHARACTERS
•DEBUGGING PROCESS
Debugging is the process of locating and fixing or by passing
bugs (errors) in computer program code or the engineering of a
hardware device. Debugging is a generalized term which essentially
means to step through a process in order to systematically
eliminate errors.

This bug causes an application or software not to run


properly. For example, failed logins, errors when inputting
data, feature not functions, blue screens, etc.
THANK YOU FOR
LISTENING!
Don't hesitate to ask any questions!

You might also like