0% found this document useful (0 votes)
16 views39 pages

ACLC College Computer Programming1 C SESSION7

Uploaded by

diazvanharoldm
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)
16 views39 pages

ACLC College Computer Programming1 C SESSION7

Uploaded by

diazvanharoldm
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/ 39

Introduction to programming

(C++)

Gliceria S. Tan
Instructor
Session 7 Topics

➢ The programming cycle


➢ Dissecting your first C++ program
➢ Identifiers
➢ Naming convention
➢ Data Types
➢ Declaration of variables
➢ Assignment statement
➢ Arithmetic/Relational/Logical operators

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
The Programming cycle

The programming cycle is a process which a


programmer follows to obtain a good and
effective program.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
5 steps in the
Programming Cycle

1. Defining the Problem


2. Planning the Solution
3. Coding the Solution
4. Checking the Program
5. Documentation

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Defining the Problem

Entails doing the following:


• address “What has to be done?”
• understand and carefully analyze the
problem
• know exactly what the program will do
before it is written

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Planning the Solution

• select the best method in solving the


problem
• make a program flowchart
• select an appropriate programming
language

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Coding the Solution

• convertthe steps depicted in the program


flowchart into readable instructions that
make up the actual program.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Checking the Program

• debugging is tracing the program step by


step to eliminate big or small errors. It is the
task of finding errors (bugs) and correcting
them so that the program runs correctly.

• Testing is running the program with input


data.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Types of errors:

• Syntax Error – occurs in the coding


process
• Logical Error – occurs when the program
runs well but the output is erroneous.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Documentation

includes the problem statement,


description, code listing, instructions,
specifications, etc.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
The General form
of a
C++ program

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
The simplest computer programs perform
three basic operations:

• Input data
• Process data
• Output all results

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
The most basic form of a C++ program
follows a very simple format:

preprocessor directives
main function {
declarations
statements
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
What is a program statement?

A C++ program consists of a sequence of


statements, in the same way that a
flowchart consists of a sequence of symbols.
The statements specify the operations to be
done.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
For example, there is a statement that does
an input operation, and there is a statement
that does an output operation. A statement
is usually written in one line, and a
semicolon ; is placed at the end.

We now move on to the dissection of your


first C++ program.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Dissecting your first C++ program

//Hello.cpp: My first C++ program


#include <iostream>
using namespace std;
int main() {
cout<<”Hello world!”;
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
The line,

//Hello.cpp: My first C++ program

is a C++-style comment.

Comments are used to explain clearly how


the program works and how it is to be used.
They are not part of the executable
program.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
#include <iostream>

It is a preprocessing directive that tells the


preprocessor to find a header file called
iostream from a library of header files and
insert it into the source program.

<iostream> is a header file that lets us work


with input and output objects, such as cout.
Header files add functionality to C++
programs.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
using namespace std;

Means that we can use names for objects


and variables from the standard library.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
int main(){
}

This is called a function. Any code within its


curly braces will be executed. The main()
function is called when your C++ program is
run.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
cout<<“Hello world!”;

cout is an object used together with the


insertion operator << to output/print text. In
our example program it will output “Hello
world!”.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Using comments in C++

//This is a C++-style or single line comment


/*This is an example of a
C-style or multiline comment*/

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Identifiers
Identifiers are tokens that represent names
of variables, functions, classes, etc.
C and C++ identifiers are case-sensitive. This
means that the identifier x is not the same
as X.
Identifiers must begin with a letter followed
by letters or numbers.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Naming convention

We shall follow a standard in naming


identifiers.
This standard is known as the camelCase
notation.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Naming convention
For names of variables and functions, the
first letter of the word should start with a
small letter. In case of multi-word identifiers,
use capital letters to indicate the start of
subsequent words.
For example:
average
annualGrossIncome
computePay
averageRate
ratePerHour
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Naming convention

For names of classes, capitalize the first


letter of the class name. In case of multi-
word identifiers, use capital letters to
indicate the start of subsequent words.
For example:
Person
Employee
MyClass

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
C++ Primitive Data Types
A char is for character storage and uses a
minimum of 8 bits (one byte) of storage,
although it may be larger.
An int stores an integral number and uses
a minimum of two bytes of storage.
The float and double types store floating-
point numbers. float is for single-precision
floating point and double is for double-
precision floating point.
The bool stores either true or false.
void represents the absence of type.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Declaration of variables
All variables must be declared before they
are used.

The general form of a declaration is


data_type var_name;

Examples of variable declarations:


int i, j, x;
double balance, profit, loss;

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
There are three basic places wherevariables
will be declared:
1. inside functions,
2. in the definition of function parameters,
or
3. outside all functions.
These variables are called
local variables,
formal parameters, and
global variables.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Local variables

Variables that are declared inside a


function are called local variables.
These can only be referenced by
statements that are inside the block in
which the variables are declared.
A block of code or a statement block
begins with an open curly brace and ends
with a close curly brace.
A local variable is created upon entry into
its block and destroyed upon exit.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Formal parameters

If a function is to use arguments, then it must


declare variables that will accept the
values of the arguments. These are called
the formal parameters of the function. They
behave, and can be used, like any other
local variables inside the function.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Global variables

Unlike local variables, global variables are


known throughout the entire program and
can be accessed from any function in the
program. Global variables are created by
declaring them outside of any function, and
they will hold their values during the entire
execution of the program.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Assignment statement

The assignment statement is performed with


the assignment operator =. It has a left-hand
side or lvalue and a right-hand side or
rvalue. The right-hand side is any expression
that produces a value. The left-hand side
must be a variable since there must be a
physical space in which to store the data
produced by the right-hand side.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Assignment statement

For instance, you can assign the constant


value 10 to a variable named a
a = 10;

but you cannot assign anything to a


constant value. You cannot write
10 = a;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
C++ supports the following assignment
shortcuts:

Assignment statement Shortcut


x=x+1 x++ or x+=1
x=x–1 x-- or x-=1
x=x*1 x*=1
x=x/1 x/=1
x=x%1 x%=1

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Arithmetic operators
+ addition
- subtraction
* multiplication
/ division
% modulus division (produces the
remainder from integer division)

Note: the modulus operator can be used


only with integer operands.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Relational operators
Relational operators establish a relationship
between the values of the operands. If the
relation is true, it returns 1, whereas if the
relation is false, it returns 0.
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Logical operators

The logical operators produce a value of 1


(true), or 0 (false) based on the logical
relationship of its operands.

! not
&& and
|| or

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
“QUOTE OF THE DAY!”

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR

You might also like