0% found this document useful (0 votes)
13 views

CSC201 Lecture 1 Slide1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CSC201 Lecture 1 Slide1

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Computer Programming I

Course Lecturer: Mr. Bello Moses Eromosele


Computer Programming I

The aim of the course is to give you a thorough


and practical introduction to computer
programming
C++ programming language is our tool of
choice to achieve this aim

Dev C++ is our choice of IDE

2
Computer Programming I
At the end of this course you should be able to
Demonstrate an understanding of computer programming
language concepts
Define data types and use them in simple data processing
applications
Understand the concept of pointers, declarations, initialization,
operations on pointers, and their usage.
Understand and apply the concept of array, union, and
enumeration user-defined data types
Demonstrate an understanding of threading and concurrency
Demonstrate an understanding of resource management
Develop C++ programs
3
Brief History of C++

C++ was developed by Bjarne Stroustrup in


1983
C++ is an improvement of C programming
language
It improved on C by adding a number of new
features, the most important of which was
classes
C++ is a multi- paradigm programming language
that supports objected oriented programming
4
Who is using C++ Programming Language

C++ is used in different sectors of the software


industry.
Apple OS is written in C++.
Windows 95, 98, Me, 200, and XP are written
in C++.
Microsoft Office, Internet Explorer, and Visual
Studio are written in Visual C++.
Google search engine and Chromium Web
Browser are written in C++.
5
Why should you learn C++

•C++ is used in various areas where high-


performance software is needed.

•C++ gets you much closer to hardware and this


can help you better understand how computers
work.

6
Structure of a C++ program

7
Structure of a C++ program
// my first program in C++: is a comment line. C++ comments come in two
types:
•Single-line comments are accomplished using a double slash (//) . The
double slash tells the compiler to ignore everything that follows, until the
end of the line.
•Multiline comments are started by using a forward slash followed by an
asterisk (/*).

#include <iostream>: is a header tag. All lines beginning with a hash sign
(#) are directives for the preprocessor. In this case the directive #include
tells the preprocessor to include the iostream standard file.

8
Structure of a C++ program
using namespace std; All the elements of the standard
C++ library are declared within what is called a namespace,
the namespace with the name std. So in order to access its
functionality we declare with this expression that we will be
using these entities. Without this declaration the expression
cout << "Hello World!";
would be written as
std::cout << "Hello, World!\n";

The std:: specifies that the name cout is to be found in the


standard-library namespace

9
Structure of a C++ program

int main () :This is the main function. The main function is


the point by where all C++ programs start their execution.
The word main is followed in the code by a pair of parentheses
() and right after these parentheses we can find the body of
the main function enclosed in braces ({}).

cout << "Hello World!"; This statement prints Hello World


on the screen

return 0; 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 0)

10
Data types (Variable and constants)

Data types in C++ are


classified into following
categories:
*
• Built-in types
• User-defined
* types

11
Data types (Variable and constants)

Data types in C++ are classified into the following categories:


C++ Data
Types

Built-in User Defined


Types Types

Void
Structure
Integral type
Boolean

Union
int
Floating Type
Class
Char
Float

Enum

Double

12
Built-in (Primitive)Data types

Integer: This data type is used to store integer values. It has


three size modifiers that may go along with it: short, int and
long

int a = 123456789;
int b = 123456789;
int c = a + b;
short d = 1234;
short e = 1234;
short f = d + e;
long g = 123456789;
long h = 123456789;
long i = g + h;

13
Built-in (Primitive)Data types

Character: This data type is used for storing characters, which


may be any single character. It may also include a letter, a
digit, a punctuation mark, or space. The keyword for this data
type is char.

14
Built-in (Primitive)Data types

Floating point: This data type is used for storing single


precision floating point values or decimal values. Its keyword is
float or double

15
Built-in (Primitive)Data types

Boolean: This is used for storing Boolean or logical values. Any variable with
this data type can store either true or false. Its Keyword is bool.

16
Built-in (Primitive)Data types

Void: Specified by void keyword, this data type is without any


value.

17
Built-in (Primitive)Data types

The list of all the built-in data types with each modifier and their range is given
in the following table.

Group Type names Range/Size


Exactly one byte in size. At least 8
Char
bits.
Not smaller than char. At least 16
Character types char16_t
bits.
Not smaller than char16_t. At least
char32_t
32 bits.

18
Built-in (Primitive)Data types

The list of all the built-in data types with each modifier and their range is given
in the following table.

Group Type names Range/Size

signed char Same size as char. At least 8 bits.

signed short int Not smaller than char. At least 16 bits.

Integer types (signed) signed int Not smaller than short. At least 16 bits.

signed long int Not smaller than int. At least 32 bits.

signed long long int Not smaller than long. At least 64 bits.

19
Built-in (Primitive)Data types

The list of all the built-in data types with each modifier and their range is given
in the following table.

Group Type names Range/Size

Float 4bytes or 32 bits

Floating-point types Double Precision not less than float

long double Precision not less than double

Boolean type Bool 8 bits

Void type Void no storage

20
Variable Name (Identifier)

A valid identifier or variable name is a sequence of one or more letters, digits


or underscore characters (_).
•A variable name can begin with a letter or an underline character (_ )
•Only letters, digits and single underscore characters can be used for a variable
name
•Neither spaces nor punctuation marks or symbols can be part of an identifier.
•A variable name should not be a keyword

Examples of valid variable names:


int a; bool ripe; float gpa;

NOTE: The C++ language is a "case sensitive" language. That means that an
identifier written in upper case letters is not equivalent to another one with the
same name but written in lower case letters. For example, a variable CGPA is
not the same as Cgpa or cgpa.

21
Compound Statement (Block)

What is a statement: A statement is an executable line of


code, eg.
int a=2;
int b=4;

A compound statement (also called a block, or block statement) is a group of


zero or more statements that is treated by the compiler as if it were a single
statement.

Blocks begin with a { symbol, end with a } symbol, with the statements to be
executed being placed in between. Example of a compound statement
{
int a=2;
int b=4;
}

22
Scope and Lifetime of a Variable

A variable can be either of global or local. A global variable is a variable


declared in the main body of the source code, outside all functions, while a
local variable is one declared within the body of a function or a block.

The global variable is visible anywhere within the program while the local
variable is only visible within the block or function.

23
Scope and Lifetime of a Variable
Let’s consider the following example:

24
Constants

Constants has a fixed


value and their value
does not change
during program
execution.

25
Constants
constexpr
This is used primarily to specify constants that are computed by the
compiler at compilation time. The value is stored in a read-only memory.

26
Constants

Constant can also be


defined using the
#define preprocessor
directive.

Its format is:


#define identifier value

27
Literals
Literals are used to express particular values within the source code of a
program.

Literal constants can be divided in Integer Numerals, Floating-Point Numerals,


Characters, Strings and Boolean Values.

Floating point literals: 3.14159 , 6.02e23 , 1.6e-19


Integer literals: 9,8,-1,0
Bololean literals: true, false
Character literals : ‘k’, ‘l’, ‘a’
String literals: “My car”, “Computer programming I”

28
Escape Codes
These are special characters that are difficult or impossible to express in the
source code of a program. Given below is a list of some escape codes

Code Function
\n New line
\r Carriage return
\t Tab
\v Vertical table
\b Backspace
\f Form feed (page feed)
\a Alert (beep)
\’ Single quote(‘)
\” Double quote (“)
\? Question mark (?)
29
\\ Backslash (\)

You might also like