Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 72

Basic Computer Programming

By Tadesse M (MSc).
Chapter 2

Programming
What is computer Programming?
Programming is the process of creating a set of direction that tell

a computer how to perform a task.


Is “Instruct the computer”: this basically means that you provide

the computer a set of instructions that are written in a language that


the computer can understand.
The instructions could be of various types. For example:
 Adding 2 numbers,
 Rounding off a number, etc.

Programming can be done using a variety of computer

programming languages.

 
What is computer Languages
A computer language is a method of communication

with a computer.
Computer programming language, any of various

languages for expressing a set of detailed instructions for


a digital computer.
Is a communication language by which a human can

specify an executable problem solution to a computer.

, Example HTML,C ++, JavaScript, Python, and etc


Why do we need to learn computer programming?
Computer programming is critical if one wants to know
how to make the computer perform a task.
1. You Can Create Anything You Want
The computer programming gives you the ability to
digitize your ideas.
 It help us to simplify our life
 We can get the power to control every thing
2. Enhanced problem solving skills
Knowledge of computer languages enhances
mathematical and logical reasoning and can be learned
surprisingly quickly.
These deduction skills will help you in all areas of your
life, like making decisions and solving complex problems.
3, Opportunities for innovation
With more than 4 billion users on the internet, the

application of computer coding is more important than


ever. And, the computer science field attracts some of
the most talented minds around the world.
Graduates from programming or computer science

courses secure rewarding and unique career


opportunities in various fields, like environment,
medicine, gaming, business operations and finance.
4, Earn a Higher Salary
Pay is probably the biggest advantage of working as a
computer programmer. Computer programmers, who write
computer code that enables a computer to perform certain
tasks, work in almost every industry you can imagine,
including the engineering, educational and medical fields.
According to the Indeed website, starting salaries in the
computer programming field in January 2012 range from
$59,000 to $112,000 per year. Some companies that do not
employ computer programmers are willing to contract
their services for as much as $100 to $400 per hour,
according to Ask IT Career Coach, a career informational
website.
5. Does Not need large space
 Computer programming does not require office

space to be leased or bound to an etched-in-stone


work schedule.
 computer programming can be done from

anywhere, you can bring your desktop computer,


you can access the internet and you can complete
the work from home.
What is a programmer ?
A programmer is an individual that writes/creates
computer software or applications by giving the computer
specific programming instructions.
 Most programmers have a broad computing and coding
background across multiple programming languages and
platforms, including Structured Query Language (SQL),
Perl, Extensible Markup Language (XML), PHP, HTML,
C, C++ and Java.
A programmer also may specialize in one or more
computing fields, like database, security or
software/firmware/mobile/Web development.
These individuals are instrumental to the development
of computer technology and the field of computing.
What skills do we need to be a programmer?
1, Problem-Solving:
 One of the major skills that a programmer must
possess is problem-solving skills.
Not only should a programmer be unafraid of
problems and in fact, face them head on, the
programmer should also be able to find problems to
solve.
a programmer should be able to come out with
technological ways of solving basic human problems.
This is why good programmers have to be great or at
least, possess basic proficiency in mathematics,
geometrics or statistics.
2, Time Management:
As a programmer, you have to be reliable and trusted

to get the job done as quickly as possible.


You have to be trusted to meet deadlines.

Even if you come highly recommended because of

the technical skill, employers will expect that you


imbibe good time management skills.
3, Good Communication Skills:
 Programmers have to be able to communicate

effectively with both the computer and to people.


You shouldn’t look down on their lack of understanding

and should instead explain to them calmly. Also, because


people are going to be dealing with the programs you
create, you need to make them user-friendly. At least, let
the user, who want to use a website like
www.betensured.com, who has a basic knowledge of
technology be able to navigate through the programs.
4, Quick Learning Skills:
 One skill that is common with most programmers is

their ability to self-learn and learn quickly.


 They do not away from new technologies or programs

but instead sit with it until they learn how it all came
together and they can even find new ways to work it to
provide better technologies.
As a programmer, you have to be ready to self-learn and

even learn every day because new technologies are


released every other day.
5, Coding Language skill

You can’t be a programmer without becoming proficient


in at least one coding language (for example, C ++,
JavaScript or Ruby on Rails).

6, Algorithm Development:
 skill of coming up with sequence of simple and human

understandable set of instructions showing the step of


solving the problem. Those set of steps should not be
dependent on any programming language or machine
7, Programming Language Rules
 In every programming Language there are sets of

rules that govern the symbols used in a programming


language.
 These set of rules determine how the programmer

can make the computer hardware to perform a specific


operation.
 These sets of rules are called syntax.
Generations of programming language.
Programming languages are categorized into five
generations: (1st, 2nd, 3rd, 4th and 5th generation
languages)
These programming languages can also be categorized
into two broad categories: low level and high level
languages.
From the five categories of programming languages,
first and second generation languages are low level
languages and the rest are high level programming
languages.
 The higher the level of a language, the easier it is to
understand and use by programmers.
Languages after the forth generation are refereed to as a
very high level languages.
First Generation (Machine languages, 1940’s):
 Difficult to write applications with.

 Dependent on machine languages of the specific computer


being used.
 Machine languages allow the programmer to interact
directly with the hardware, and it can be executed by the
computer without the need for a translator.
 Is more powerful in utilizing resources of the computer.
 Gives power to the programmer.
 They execute very quickly and use memory very
efficiently.
Second Generation (Assembly languages, early 1950’s):
 Uses symbolic names for operations and storage
locations.
 A system program called an assembler translates a
program.
 Programs written in assembly language are not
portable. i.e., different computer architectures have their
own machine and assembly languages.
 They are highly used in system software development.
Third Generation (High level languages, 1959’s to
1970’s):
 Uses instructions and mathematicians were able to define
variables with statements such as Z = A + B
 Such languages are much easier to use than assembly
language.
 Programs written in high level languages need to be
translated into machine language in order to be executed.
 The use of common words (reserved words) within
instructions makes them easier to learn.
 All third generation programming languages are
procedural languages.
 In procedural languages, the programmer is expected to
specify what is required and how to perform it.
Fourth Generation (since late 1970’s):
Have a simple, English like syntax rules; commonly used
to access databases.
 Forth generation languages are non-procedural languages.
 The non-procedural method is easier to write, but you have

less control over how each task is actually performed.


 In non-procedural languages the programmer is not

required to write traditional programming logic.


 Programmers concentrate on defining the input and output
rather than the program steps required.
Fifth Generation (1990’s):
 These are used in artificial intelligence (AI) and expert
systems; also used for accessing databases.
 5GLs are “natural” languages whose instruction closely

resembles human speech. E.g. “get me Jone Brown’s sales


figure for the 1997 financial year”.
 5GLs require very powerful hardware and software

because of the complexity involved in interpreting


commands in human language.
Major Programming Paradigms
 The major land marks in the programming world

are the different kinds of features or properties


observed in the development of programming
languages.
 Among these the following are worth mentioning

Procedural, Structured and Object Oriented


Programming Paradigms
1, Procedural Programming
Procedural programming is a programming paradigm based

upon the concept of procedure call. Procedural programming


is often a better choice than simple sequential programming in
many situations.
It moderate complexity situation or which require significant

ease of maintainability.
Possible benefits: the ability to re-use the same code

(function or procedure) at different places, an easier way to


keep track of program flow than a collection of “GO TO” or
“JUMP” statements
2, Structured Programming.
 Process of writing a program in small, independent parts.

This makes it easier to control a program's development


and to design and test its individual component parts.
 Structured programs are built up from units called

modules, which normally correspond to single procedures


or functions.
 Can be seen as a subset or sub discipline of procedural

programming. It is most famous for removing or reducing


reliance on the GO TO statement.
3, Object-Oriented Programming.
 The idea behind OOP is that, a computer program

is composed of a collection of individual units, or


objects as opposed to traditional view in which a
program is a list of instructions to the computer.
 Object-oriented programming is claimed to give

more flexibility, easing changes to programs. The


OOP approach is often simpler to develop and
maintain.
Modeling a programs logic using flow chart
 A flowchart is a type of diagram that represents a
workflow or process. A flowchart can also be defined as a
diagrammatic representation of an algorithm, a step-by-
step approach to solving a task. The flowchart shows the
steps as boxes of various kinds, and their order by
connecting the boxes with arrows.
A flowchart is a diagram that depicts a process,
system or computer algorithm. They are widely used in
multiple fields to document, study, plan, improve and
communicate often complex processes in clear, easy-to-
understand diagrams.
Flowchart Symbols
Different flowchart shapes have different conventional
meanings. The meanings of some of the more common
shapes are as follows:
Terminator
The terminator symbol represents the starting or ending
point of the system.
                            

Process
A box indicates some particular operation.

                         
Document
This represents a printout, such as a document or a report

Decision
A diamond represents a decision or branching point.
Lines coming out from the diamond indicates different
possible situations, leading to different sub-processes.

                      
Find the largest among three different numbers
entered by the user
What is C++?
C++ was developed by Bjarne Stroustrup, as an

extension to the C language.


C++ is a cross-platform language that can be used to

create high-performance applications.


C++ gives programmers a high level of control over

system resources and memory.


The language was updated 3 major times in 2011, 2014,

and 2017 to C++11, C++14, and C++17.


What is C?
C is a general-purpose programming language that is
extremely popular, simple, and flexible to use.
 It is a procedural programming language that is
machine-independent and extensively used to write various
applications, Operating Systems like Windows, and many
other complex programs like Oracle database, Git, Python
interpreter, and more.
It is said that 'C' is a god's programming language. One
can say, C is a base for the programming. ‘
It is essential to have a background in computer memory
mechanisms because it is an important aspect when dealing
with the C programming language.
Key Differences Between C Vs C++
Enlisted below are the main differences between C Vs C+
+.
1) Type of Programming:
C is a procedural language in which the program
revolves around the functions. The entire problem is
broken down into numerous functions. The main focus of
the program is on functions or procedures to get the
things done.
C++, on the contrary, is an object-oriented
programming language. Here the data of the problem is
the main focus and the classes are built around this data.
Functions operate on the data and closely bound to data.
2) Programming Approach:
 As C is a procedural language, it follows a
top-down approach of programming. Here we
take the problem and then break it into
subproblems until we find single subproblems
that can be solved directly. Then we combine
the solutions to get the main solution.
 C++ follows a bottom-up approach to
programming. In this, we start design or
coding and then build on this low-level design
to get a high-level solution.
3) Application Development:

C is more suitable for client-side applications or low-level


implementations.

C++, on the other hand, is more suitable for server-side


applications, network applications or for applications like
gaming, etc.

4) File Extension:

The programs written in C are usually saved with “.c”


extension while the C++ programs are saved with the
“.cpp” extension.
5) Compatibility With Each Other:
C++ is a subset of C as it is developed and takes most

of its procedural constructs from the C language. Thus


any C program will compile and run fine with the C++
compiler.
However, C language does not support object-oriented

features of C++ and hence it is not compatible with C++


programs.
C++ language is generally compatible with other

generic programming languages but C language is not.


6) Data Security:
In C, the main emphasis is on functions or procedures
rather than on data. Hence as low as data security is
concerned, it is negligible in C.
In C++, as we are dealing with classes and objects, the
main building block of the program is Data. Thus, data is
hight secured using classes, access specifiers,
encapsulation, etc.
7) Structures:
Structures in C and C++ use the same concept. But the

difference is, in C, as we cannot include functions as


members.
C++ allows structures to have functions as its members.

 8, Global Variables:


C language allows multiple declarations of global

variables.
C++, however, doesn’t allow multiple declarations of

global variables.
why we learn of C++
C++ is one of the world's most popular programming
languages.
C++ can be found in today's operating systems,
Graphical User Interfaces, and embedded systems.
C++ is an object-oriented programming language
which gives a clear structure to programs and allows code
to be reused, lowering development costs.
C++ is portable and can be used to develop applications
that can be adapted to multiple platforms.
C++ is fun and easy to learn!
It’s used in a wide range of industries including
software and game development, VR, robotics, and
scientific computing.
Where we Uses of C++
There are several benefits of using C++ for developing
applications and many applications product based developed
in this language only because of its features and security.
Applications
 It is used for the development of new applications of C++.
The applications based on the graphic user interface,
which are highly used applications like adobe photoshop and
others. Many applications of Adobe systems are developed
in C++ like Illustrator, adobe premiere and image ready and
Adobe developers are considered as active in the C++
community.

39
Games
 This language is also used for developing games.
 It overrides the complexity of 3D games. It helps in
optimizing the resources.
It supports the multiplayer option with networking.
uses of C++ allows procedural programming for
intensive functions of CPU and to provide control over
hardware, and this language is very fast because of which
it is widely used in developing different games or in
gaming engines.
C++ mainly used in developing the suites of a game
tool.
Animation
 There is animated software, which is developed with the help

of the C++ language.


3D animation, modeling, simulation, rendering software are

referred to as the powerful toolset.


 It is widely used in building real-time, image processing,

mobile sensor applications, and visual effects, modeling which


is mainly coded in C++.
This developed software used for animation, environments,

motion graphics, virtual reality, and character creation. Virtual


real devices are the most popular in today’s entertainment world.
Web Browser
 This language is used for developing browsers as well.
C++ is used for making Google Chrome, Mozilla and
Internet browser Firefox.
Some of the applications are written in C++, from
which Chrome browser is one of them and others are like
a file system, the map reduces large cluster data
processing.
Mozilla has other application also written in C++ that is
email client Mozilla Thunderbird.
C++ is also a rendering engine for the open-source
projects of Google and Mozilla.
Database Access
This language is also used for developing database
software or open-source database software. The example for
this is MySQL, which is one of the most popular database
management software and widely used in organizations or
among the developers. It helps in saving time, money,
business systems, and packaged software.
There are other database software access based
applications used that are Wikipedia, Yahoo, youtube, etc.
The other example is Bloomberg RDBMS, which helps in
providing real-time financial information to investors. It is
mainly written in C++, which makes database access fast
and quick or accurate to deliver information regarding
business and finance, news around the world.
Media Access
C++ is also used for creating a media player, managing
video files and audio files.
The example is Winamp Media player, which is
developed in C++ language, which allows us to enjoy
music, access and share the videos and music files.
 It also has features like art support, streaming of audio
and video. It also provides access to Internet radio stations.
Compilers
 The compilers that are used for compiling other
languages like C#, Java, etc.
It is also used in developing these languages as well as
C++ is platform-independent and able to create a variety of
software.
Operating Systems
 It is also used for developing most of the operating
systems for Microsoft and few parts of the Apple
operating system.
Microsoft Windows 95, 98, 2000, XP, office, Internet
Explorer and visual studio, Symbian mobile operating
systems are mainly written in C++ language only.
Scanning
The applications like film scanner or camera scanner are
also developed in the C++ language.
It has been used for developing PDF technology for
print documentation, exchanging documents, archiving
the document and publish the documents as well.
Features of C++

Here is the list of most important features in C++ that can


be used to developed high-performing applications:

1. Simple
C++ is one of the most-simple languages when it comes

to programming. It is also easy to understand


C++ follows the bottom-up approach for any kind of

project development that helps in breaking down big tasks


into a chunk of tasks.
2. Object-Oriented Programming
One of the most important features because of which

C++ got famous.


 Everything is treated as objects in C++ that’s why it is

called object-oriented programming.


 Objects are used for performing all kinds of

functionalities. It has various features like


Polymorphism and Inheritance which aims to bind
function and data together in a single unit to make
applications secured and efficient.
3. Portability
C++ is not platform independent but we can say it is
portable enough to run on different machines by adding
some or no changes at all.
On different operating systems you can run the same
code.
Write code for one time and use it for every time you
need that particular functionality.
We can’t say that it’s completely platform –
independent, but java is a completed platform.
For example, you have written code in Linux but wants
to run in Windows so C++ code will run on both without
any hindrance! Simple and straight.
4. high-Level Programming Language
C++ programming language is a collection of special

features of low-level languages and high-level languages.


It can be used to develop applications based on the
required level of programing language that is low or high.
5. Rich Library
C++ library is full of in-built functions that save a huge

amount of time in the software development process.


As it contains almost all kinds of functionality a

programmer can need in the development process.


Hence saving time and increasing development speed.

 Popular Course in this category


6. Case Sensitive
As C++ is originated from C, it is also purely CASE

sensitive that means lowercase and uppercase characters


written in code will have completely different meaning and
will be treated differently.

7. Compiler-Based
As no interpretation is done in C++ code it is considered to

be a compiler-based language that makes it faster than other


programming languages like Java, etc. Without compilation,
you can’t execute any C++ code.
8. Dynamic Memory Allocation
Due to pointer support in the C++ language.

Memory allocation can be easily done dynamically

rather than static. Can free memory anytime by using the


free() function.
How we save c++
Save the file as "hello.cpp." In Notepad, click the “File”

menu and select “Save As.” When the Save As dialog


appears, change the file type to “All Files,” name the file
“hello.cpp” and click the “Save” button.
C++ Data Types
You may like to store information of various data types

like character, wide character, integer, floating point,


double floating point, boolean etc. Based on the data type
of a variable, the operating system allocates memory and
decides what can be stored in the reserved memory.
While writing program in any language, you need to use

various variables to store various information.


Variables are nothing but reserved memory locations to

store values.
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in
as well as user defined data types. Following table lists
down seven basic C++ data types −

Type Keyword

Boolean bool

Character char

Integer int

Floating point float

Double floating point double

Valueless void

Wide character wchar_t


Sr.N Type & Description
o

1 bool
Stores either value true or false.

2 char
Typically a single octet (one byte). This is an integer type.

3 int
The most natural size of integer for the machine.

4 float
A single-precision floating point value.

5 double
A double-precision floating point value.

6 void
Represents the absence of type.

7 wchar_t
A wide character type.
The following table shows the variable type, how much memory it takes to store the
value in memory, and what is maximum and minimum value which can be stored in
such type of variables.
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255

unsigned char 1byte 0 to 255


signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295


signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767


unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 8bytes -2,147,483,648 to 2,147,483,647

signed long int 8bytes same as long int


unsigned long int 8bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1
unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615

float 4bytes
double 8bytes
Variable  Declaration in C++
A variable definition tells the compiler where and how
much storage to create for the variable. A variable
definition specifies a data type, and contains a list of one
or more variables of that type as follows −
type variable_list; Here, type must be a valid C++ data
type including char, w_char, int, float, double, bool or any
user-defined object, etc., and variable_list may consist of
one or more identifier names separated by commas. Some
valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
Comments in C++
Program comments are explanatory statements that you
can include in the C++ code. These comments help
anyone reading the source code. All programming
languages allow for some form of comments.
C++ supports single-line and multi-line comments.
multi-line comments
C++ comments start with /* and end with */.
For example /* This is a comment */ /* C++ comments
can also * span multiple lines */
single-line comments
A comment can also start with //, extending to the end of
the line. 
Assignment Operators in C/C++
Assignment operators are used to assigning value to a
variable. The left side operand of the assignment operator
is a variable and right side operand of the assignment
operator is a value. The value on the right side must be of
the same data-type of the variable on the left side
otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
“=” operator

This is the simplest assignment operator. This


operator is used to assign the value on the
right to the variable on the left.
For example:a = 10; b = 20; ch = 'y';
“+=” Operators
This operator is combination of ‘+’ and ‘=’

operators. This operator first adds the current value


of the variable on left to the value on the right and
then assigns the result to the variable on the left.
Example:(a += b) can be written as (a = a + b) If
initially value stored in a is 5. Then (a += 6) = 11.
“-= operator”

This operator is combination of ‘-‘ and ‘=’ operators.


This operator first subtracts the current value of the
variable on left from the value on the right and then
assigns the result to the variable on the left.
Example:

(a -= b) can be written as (a = a - b) If initially value


stored in a is 8. Then (a -= 6) = 2.
“*= operator”

This operator is combination of ‘*’ and ‘=’ operators.


This operator first multiplies the current value of the
variable on left to the value on the right and then
assigns the result to the variable on the left.
Example:(a *= b) can be written as (a = a * b) If
initially value stored in a is 5. Then (a *= 6) = 30.
“/= operator ”

This operator is combination of ‘/’ and ‘=’ operators.


This operator first divides the current value of the
variable on left by the value on the right and then
assigns the result to the variable on the left.
Example:(a /= b) can be written as (a = a / b) If
initially value stored in a is 6. Then (a /= 2) = 3.
C++ cout
The cout object in C++ is an object of class ostream.

 It is used to display the output to the standard output device i.e.

monitor. It is associated with the standard C output stream stdout.

C++ cin
The cin object in C++ is an object of class istream.

 It is used to accept the input from the standard input device i.e.

keyboard. It is associated with the standard C input stream stdin.


Header files available in C++ for Input/Output
operations are: 
Iostream
 iostream stands for standard input-output stream.
This header file contains definitions to objects like cin,
cout, cerr etc. ... This header file is used to handle the data
being read from a file as input or data being written into
the file as output.
Iomanip
 iomanip stands for input output manipulators.
The methods declared in this files are used for
manipulating streams. This file contains definitions of
setw, setprecision, etc.
Fstream
 This header file mainly describes the file stream.
 This header file is used to handle the data being read
from a file as input or data being written into the file as
output.
The two keywords cout in C++ and cin in C++ are
used very often for printing outputs and taking inputs
respectively. These two are the most basic methods of
taking input and printing output in C++. To use cin and
cout in C++ one must include the header file iostream in
the program.
This article mainly discusses the objects defined in the
header file iostream like the cin and cout.  
Standard output stream (cout):
 Usually the standard output device is the display screen.

The C++ cout statement is the instance of the ostream

class. It is used to produce output on the standard output


device which is usually the display screen. The data
needed to be displayed on the screen is inserted in the
standard output stream (cout) using the insertion
operator(<<).

 
standard input stream (cin)
Usually the input device in a computer is the keyboard.

C++ cin statement is the instance of the

class iostream and is used to read input from the


standard input device which is usually a keyboard. 
The extraction operator(>>) is used along with the
object cin for reading inputs. The extraction operator
extracts the data from the object cin which is entered
using the keyboard.
Echo 
Echo printing in c++ is just to print out the same

integers the user entered in the same order, probably


using std::out , since you will be using std::in to input the
ints.
CMD

You might also like