0% found this document useful (0 votes)
5 views16 pages

PL2 Lecture 1-Basic Programming Concepts (C++) 1

programming language

Uploaded by

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

PL2 Lecture 1-Basic Programming Concepts (C++) 1

programming language

Uploaded by

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

Pseudo Code

 Pseudocode is a simple way of writing


programming code in English.

 Pseudocode is not an actual programming


language.

 It uses short phrases to write code for


programs before you actually create it in a
specific language.

 Once you know what the program is about


and how it will function, then you can use
pseudocode to create statements to achieve
the required results for your program.
What is Computer
Programming?
 A computer program is a collection of instructions/ statements
that performs a specific task when executed by a computer.
 Usually written by a computer programmer with a programming
language such as C, C++, Java, Pascal, Python, C# etc.
 Programming languages are used to write programs
Type of Programming Languages

Procedure Oriented programming Object Oriented Programming


In POP, program is divided into small parts called In OOP, program is divided into parts
functions. called objects.

POP does not have any access specifier. OOP has access specifiers named Public, Private,
Protected, etc.

In POP, Data can move freely from function to In OOP, objects can move and communicate with
function in the system. each other through member functions.

In POP, Overloading is not possible. In OOP, overloading is possible in the form of


Function Overloading and Operator Overloading.

POP does not have any proper way for hiding data, OOP provides Data Hiding so provides more
so it is less secure. security.

Example: COBOL, C, Fortan etc. Example: C++, Java, Perl etc.


What is Programming Language?

 A programming language is a formal constructed language designed to communicate


instructions to a machine, particularly a computer. Programming languages can be used to
create programs to control the behavior of a machine or to express algorithms.

 Example: C, C++, Java, Fortran etc.

 Generally speaking, the program is a simple text file (written using a word processor or a text
editor, this is called the source file).

 The source file contains lines of program called source code. Once the source file has been
completed it must be compiled. Compilation takes place in two stages:
Difference Between Compilers & Interpreters

 Definition of Compiler : A computer program which reads source code and


outputs assembly code or executable code is called compiler.

 Example : gcc , Microsoft Visual Studio, MinGW etc.


Difference Between Compilers & Interpreters
cont.

 Definition of Interpreter : It is a computer program that directly executes,


i.e. performs, instructions written in a programming or scripting language,
without previously compiling them into a machine language program.

 Example: Perl, Python, Ruby etc.


Difference Between Compilers & Interpreters
(cont.)

Interpreter Compiler
Translates program one statement at a Scans the entire program and translates it
time as a whole into machine code.
It takes less amount of time to analyze the It takes large amount of time to analyze
source code but the overall execution the source code but the overall execution
time is slower. time is comparatively faster.

No intermediate object code is generated, Generates intermediate object code which


hence are memory efficient. further requires linking, hence requires
more memory.

Continues translating the program until It generates the error message only after
the first error is met, in which case it scanning the whole program. Hence
stops. Hence debugging is easy. debugging is comparatively hard.

Programming language like Python, Ruby Programming language like C, C++ use
use interpreters. compilers.
C++ Program Structure
C++ Program Structure (cont.)
 The C++ language defines several headers, which contain
information that is either necessary or useful to your program. For
this program, the header <iostream> is needed.
 The line using namespace std; tells the compiler to use the std
namespace. Namespaces are a relatively recent addition to C++.
 The line int main() is the main function where program execution
begins.
 The next line cout << "This is my first C++ program."; causes the
message "This is my first C++ program" to be displayed on the
screen.
 The next line return 0; terminates main( )function and causes it to
return the value 0 to the calling process.
Data types
 Basic data types in C++: int, float, double, char and bool.

 Data type int: can be used to store integer numbers (values with no decimal
places)

 Data type type float: can be used for storing floating-point numbers (values
containing decimal places).

 Data type double: the same as type float, only with roughly twice the
precision.

 Data type char: can be used to store a single character, such as the letter a, the
digit character 6, or a semicolon.

 Data type bool: can be used to store just the values 0 or 1 (used for indicating a
true/false situation). This type has been added by the C99 standard (was not in
ANSI C)
Data types
Data Type Size Range Description
-2,147,483,648 Stores whole numbers, without decimals
int 4 bytes to
2,147,483,647
float 4 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 7 decimal
digits
double 8 bytes Stores fractional numbers, containing one or
more decimals. Sufficient for storing 15 decimal
digits
bool 1 byte true or false Stores true or false values
Stores a single character/letter/number, or
char 1 byte 0 to 255
ASCII values
Variables
 Programs can use symbolic names for storing computation data

 Variable: a symbolic name for a memory location


 programmer doesn’t have to worry about specifying (or even knowing)
the value of the location’s address

 Variables have to be declared before they are used


 Variable declaration: [symbolic name(identifier), type]

 Declarations that reserve storage are called definitions


 The definition reserves memory space for the variable, but doesn’t put
any value there

 Values get into the memory location of the variable by


initialization or assignement
Variable names
 Rules for valid variable names (identifiers) in C ++:
 Name must begin with a letter or underscore ( _ ) and can be followed
by any combination of letters, underscores, or digits.
 Any name that has special significance to the C compiler (reserved
words) cannot be used as a variable name.
 Examples of valid variable names: Sum, pieceFlag, I, J5x7,
Number_of_moves, _sysflag
 Examples of invalid variable names: sum$value, 3Spencer, int.
 C++ is case-sensitive: sum, Sum, and SUM each refer to a different
variable !
 Variable names can be as long as you want, although only the first 31
characters might be significant. (Anyway, it’s not practical to use
variable names that are too long)
 Choice of meaningful variable names can increase the readability of a
program.
Variable declarations

DataType VariableName;

Which data types Which variable names


are possible are allowed

int number1;
Variables - Examples
int a; // declaring a variable of type int

int sum, a1,a2; // declaring 3 variables

int x=7; // declaring and initializing a variable

a=5; // assigning to variable a, the value 5

a1=a; // assigning to variable a1, the value of


a

a1=a1+1; // assigning to variable a1 the value of


a1+1
OR a1 += 1; // (increasing value of a1 with 1)
Working with arithmetic
expressions
#include <iostream>
using namespace std;
int main ()
{
int a = 100; int b = 2;
int c = 25; int d = 4;
int result;
result = a - b; // subtraction
cout<<"a - b = "<< result<<endl;
result = c * d; // multiplication
cout<<"c * d = "<< result<<endl;
result = a / c; // division
cout<<"a / c = "<< result<<endl;
result = a + b * c; // precedence
cout<<"a + b * c = "<<result<<endl;
return 10;
}

You might also like