ch1 computer programming
ch1 computer programming
ch1 computer programming
Introduction to programming
Computer stand for :
Common Operating Machine Particularly Used for Trade, Education, and Research
Common Operating Machine Particularly Used for Technical, Education and Research
Computer is not an acronym, it is a word derived from a word "compute" which means to
calculate. So, in simple words you can say that computer is an electronic device which is used
for fast calculation.
"A computer is a general purpose electronic device that is used to perform arithmetic
and logical operations automatically. A computer consists of a central processing unit
and some form of memory."
A Computer is an electronic device that accepts data, performs
computations, and makes logical decisions according to instructions
that have been given to it; then produces meaningful information in a
form that is useful to the user.
In current world we live in, computers are almost used in all walks of
life for different purposes. They have been deployed to solve different
real life problems, from the simplest game playing (calculator) up to
the complex nuclear energy production.
Computer programs:
In order to solve a given problem, computers must be given the correct
instruction about how they can solve it.
Its sets of instructions that control a computer’s processing of data.
The terms computer programs, software programs, source code
or just programs are the instructions that tells the computer what to
do. Computer requires programs to function, and a computer programs
does nothing unless its instructions are executed by a CPU.
Computer programs are often written by professionals known as
Computer Programmers (simply programmer).
Source code is written in one of programming languages
Computer programming:
1|Page
By:-Abdela A.
Computer programming (often shortened to programming or coding)
is the process of writing, testing, debugging/troubleshooting, and
maintaining the source code of computer programs.
Writing computer programs means writing instructions that will make the
computer follow and run a program based on those instructions. Each
instruction is relatively simple, yet because of the computer's speed, it is
able to run millions of instructions in a second.
2|Page
By:-Abdela A.
Problem Solving Skill: skills on how to solve real world problem and
represent the solution in understandable format.
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
+(c vs c+):
C++ is the extension of C language, in C++ you not only use new feature but also use C
features. The main difference between C and C++ is, C is the function or procedure oriented
programming language and C++ is the object oriented programming language
2. #include <iostream>
3. using namespace std;
4. void main () Output :Hello World
5. {
6. cout << "Hello world!"<<endl;
7. }
5|Page
By:-Abdela A.
Many programmers make the last character printed by a function a newline (\n) and endl.
This ensures that the function will leave the screen cursor positioned at the beginning of a
new line.
Comments
Compiler directive
main function
Braces
parenthesis
This is a comment line. All lines beginning with two slash signs (//) are
considered comments and do not have any effect on the behaviour of the
program. The programmer can use them to include short explanations or
observations within the source code itself. In this case, the line is a brief
description of what our program is. Comments Are remarks that are ignored by
the complier and you may write a comment anywhere in the program
Example:
1. Compiler directive:
1. Pre-processor
2. Header file
Pre-processor (#include)
Lines beginning with a hash sign (#) are directives for the pre-processor.
They are not regular code lines with expressions but indications for the
compiler's pre-processor.
In this case the directive #include<iostream> tells the pre-processor to
include the iostream standard file.
Header files (<iostream.h>)
iostream.h is a standard C++ header file and contains definitions for
input and output
Are instructions to the compiler rather than C++ instructions that
instruct the C++ compiler to load a file from disk into the current
program?
C++ support many types of header which available in built-in library
7|Page
By:-Abdela A.
However, in C++ we write more than one header file for one program
We write header before main function and not written everywhere in
the program unlike comment
A header file may be included in one of two ways.
# include < iostream.h> or
# include “iostream.h”
The header file in angle brackets means that file reside in standard include directory.
The header files in double quotes means that file reside in current directory.
Types of Header files System header files: It is comes with
compiler.
1. List and discuss available c++ header file with their function?
2. Discuss types of error in programming with example?
3. What is the difference between object code and source code?
4. What is the similarities and difference between c and c++?
main () function
Is a function that runs first in C++ programs
This line corresponds to the beginning of the definition of the main
function.
The main function is the point by where all C++ programs start their
execution, independently of its location within the source code.
It does not matter whether there are other functions with other names
defined before or after it – the instructions contained within this
function's definition will always be the first ones to be executed in any
C++ program.
For that same reason, it is essential that all C++ programs have a main
function.
The word main is followed in the code by a pair of parentheses ( ).
That is because it is a function declaration:
There are two types of main function in C++
int main()
int main has return value i.e. return 0;
void main()
8|Page
By:-Abdela A.
Void main has no return value
Right after these parentheses we can find the body of the main
function enclosed in braces {}.
What is contained within these braces is what the function does when
it is executed.
Program execution starts by executing whatever instructions found in
the body of statement.
Every C++ program must have only one main function.
The parentheses () following the main to tell the compiler that main is
a function
A brief look at cout and cin
cin---------------extraction operator (>>)
cout---------------insertion operator (<<)
cout is an object used for printing data to the screen.
cout represents the standard output stream in C++, and the meaning
of the entire statement is to insert a sequence of characters (in this
case the Hello World sequence of characters) into the standard output
stream (which usually is the screen).
To print a value to the screen, write the word cout, followed by the
insertion operator also called output or redirection operator (<<) and
the object to be printed on the screen.
Syntax:
Cout<<Object;
The object at the right hand side can be:
• A literal string: “Hello World”
• A variable: a place holder in memory
Cin is an object used for taking input from the keyboard.
To take input from the keyboard, write the word cin, followed by the
input redirection operator (>>) and the object name to hold the input
value.
Syntax:
Cin>>Object
9|Page
By:-Abdela A.
cin will take value from the keyboard and store it in the memory.
Thus the cin statement needs a variable which is a reserved memory
place holder.
Both << and >> return their right operand as their result, enabling
multiple input or multiple output operations to be combined into one
statement.
The following example will illustrate how multiple input and output
can be performed: E.g.
Cin>>var1>>var2>>var3;
Here three different values will be entered for the three variables. The
input should be separated by a space, tan or newline for each
variable.
Cout<<var1<<”, “<<var2<<” and “<<var3;
Braces {}
o Are used to mark the beginning and the end of a block of code.
o Every opening brace must have a corresponding closing brace .
Statements
Are instructions and commands that make the computer work.
Each statement must end in semicolon (;).
The semicolon tells the compiler that the statement is completed.
Notice that the statement ends with a semicolon character (;). This
character is used to mark the end of the statement and in fact it
must be included at the end of all expression statements in all C+
+ programs (one of the most common syntax errors is indeed to
forget to include some semicolon after a statement).
return statement
One of several means to exit a function.When used at the end of main the
value 0 indicates the program terminated successfully.
Example: 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). A return code of 0 for the main function is generally interpreted as the
10 | P a g e
By:-Abdela A.
program worked as expected without any errors during its execution. This
is the most usual way to end a C++ console program.
Basic Elements of c++(Tokens):
C++ Tokens are the smallest individual units of a program .
It’s the group of characters that forms a basic building block is called as Token. There are
the following five types of token in C++:
Comments
Keywords (reserved words)
Identifier
Constant/literals
Operators
Reserved/Key words have a unique meaning within a C++ program. These symbols, the
reserved words, must not be used for any other purposes. In generally reserved words are:
11 | P a g e
By:-Abdela A.
The following are some of the reserved words of C++/c:
Identifiers:
Identifiers are the basic building blocks of a program. Various data items with symbolic
names in C++ are called as Identifiers.
12 | P a g e
By:-Abdela A.
Following data items are called as Identifier in C++ –
1. Names of functions
2. Names of arrays
3. Names of variable
4. Names of classes.
Consist only of letters(A-Z or a-z), the digits 0-9, or the underscore symbol( _)
C++ is case-sensitive so that Uppercase Letters and Lower Case letters are different
The name of identifier cannot begin with a digit. However, Underscore can be used as first
character while declaring the identifier.
Only alphabetic characters, digits and underscore (_) are permitted in C++ language for
declaring identifier.
Other special characters(?,/,”,@,! etc) are not allowed for naming a variable / identifier
13 | P a g e
By:-Abdela A.
Characters Literals
Character literals are enclosed in single quotes. A character literal can be
a plain character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal
character (e.g., '\u02C0'). There are certain characters in C++ when they
are preceded by a backslash they will have special meaning and they are
used to represent like newline (\n) or tab (\t). Here, you have a list of
some of such escape sequence codes
Definition of variable
Declaration of variable
Initialization of variable
Scope of variable
14 | P a g e
By:-Abdela A.
Definition of C++ variables
Variables represent storage locations in the computer’s memory.
Constants are data items whose values cannot change while the
program is running. Variables allow you to store and work with data in
the computer’s memory.
A variable provides us with named storage that our programs can
manipulate. Each variable in C++ has a specific type, which
determines the size and layout of the variable's memory; the range of
values that can be stored within that memory; and the set of
operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the
underscore character. It must begin with either a letter or an
underscore. Upper and lowercase letters are distinct because C++ is
case-sensitive
Variable consists of three things:
data type
variable name
value
While writing program in any language, you need to use various variables
to store various information. Variables are nothing but reserved memory
15 | P a g e
By:-Abdela A.
locations to store values. This means that when you create a variable you
reserve some space in memory.
Data type is a keyword used to identify type of data. It is used for storing
the input of the program into the main memory (RAM) of the computer by
allocating sufficient amount of memory space in the main memory of the
computer. Based on the data type of a variable, the operating system
allocates memory and decides what can be stored in the reserved
memory. In general every programming language is containing three
categories of data types.
These are the data types whose variable can hold maximum one value at
a time, in C++ the following are primitive data types
• Long
• Short
16 | P a g e
By:-Abdela A.
• Unsigned
• Signed
As the meaning tells, signed and unsigned modifiers deals with the
(+/-) sign of the variable signed variable stores the signed value in
the allocated memory.
Unsigned variable does not store signed value. Sign takes 1 bit extra.
So if we are using unsigned value then we can get one bit extra space
to save the value of a variable. The range of values for unsigned types
starts from 0.
4. int type allows use of the shorthand notation. So, the following
variable definitions are identical short int a; and short a; unsigned
int a; and unsigned a; long int a; and long a;
17 | P a g e
By:-Abdela A.
Variable initialization
Examples:
18 | P a g e
By:-Abdela A.
char name=’jawar’;
// This program shows variable initialization and declaration.
int age=32;
#include <iostream>
#include <string>
using namespace std;
int main()
flaot GPA=3.75;
{
string month = "February"; month is initialized to "February"//
Operator
int year, //inyear
c++:is not initialized
An =
days operator is a is
28; // days symbol that to
initialized tells
28the compiler to perform specific
year = 2007; // Now
mathematical year ismanipulations.
or logical assigned a value
cout << "In " << year << " " << month << " had " << days << "
days.\n";
return 0;
}
19 | P a g e
By:-Abdela A.
• Arithmetic operator
• Relational operator
• Logical operator
• Increment/decrement operator
• Bitwise operator
• Misc Operators(this include the following operator)
Size of()
Condition? X : Y
Type casting
. (dot) and -> (arrow)
Comma operator(,)
20 | P a g e
By:-Abdela A.
storing a value at some memory location (typically denoted by a variable).
Its left operand should be an lvalue, and its right operand may be an
arbitrary expression. The latter is evaluated and the outcome is stored in
the location denoted by the lvalue.
Relational Operator
C++ provides six relational operators for comparing numeric quantities.
Relational operators evaluate to 1 (representing the true outcome) or 0
(representing the false outcome).
Bitwise Operators:
21 | P a g e
By:-Abdela A.
C++ provides six bitwise operators for manipulating the individual bits in an integer
Quantity.
Conditional Operator:
The conditional operator takes three operands. It has the general form:
operand1 ? operand2 : operand3
First operand1 is evaluated, which is treated as a logical condition. If the
result is nonzero then operand2 is evaluated and its value is the final
result. Otherwise, operand3 is evaluated and its value is the final result.
For example:
int m = 1, n = 2;
int min = (m < n ? m : n); // min receives 1
The sizeof Operator
C++ provides a useful operator, sizeof, for calculating the size of any data
item or type. It takes a single operand which may be a type name (e.g.,
int) or an expression (e.g., 100) and returns the size of the specified entity
in bytes. The outcome is totally machine-dependent.
This operator accepts one parameter, which can be either a type or a
variable itself and returns the size in bytes of that type or object:
a = sizeof (char);
This will assign the value 1 to a because char is a one-byte long type. The
value returned by sizeof is a constant, so it is always determined before
program execution.
22 | P a g e
By:-Abdela A.
Explicit casting
Implicit casting
Syntax: (<data – type> )value; or <data – type> (value);
A value in any of the built-in types we have see so far can be converted
(type-cast) to any of the other types. For example:
(int) 3.14 // converts 3.14 to an int to give 3
(long) 3.14 // converts 3.14 to a long to give 3L
(double) 2 // converts 2 to a double to give 2.0
(char) 122 // converts 122 to a char whose code is 122
As shown by these examples, the built-in type identifiers can be used as
type operators. Type operators are unary (i.e., take one operand) and
appear inside brackets to the left of their operand. This is called explicit
type conversion. When the type name is just one word, an alternate
notation may be used in which the brackets appear around the operand:
int(3.14) // same as: (int) 3.14
In some cases, C++ also performs implicit type conversion. This
happens when values of different types are mixed in an expression. For
example:
i = i + d; // means: i = int(double(i) + d)
double d = 1; // d receives 1.0
int i = 10.5; // i receives 10
Increment/Decrement Operators:
The auto increment (++) and auto decrement (--) operators provide a
Convenient way of, respectively, adding and subtracting 1 from a numeric
variable.
• Increment operator: increment variable by 1
• Decrement operator: decrement variable by 1
– Pre-increment: ++variable
– Post-increment: variable++
– Pre-decrement: --variable
– Post-decrement: variable—
23 | P a g e
By:-Abdela A.
Operator Precedence and associatively (order):
The order in which operators are evaluated in an expression is significant
and is determined by precedence rules. These rules divide the C++
operators into a number of precedence levels Operators in higher levels
take precedence over operators in lower levels.
24 | P a g e
By:-Abdela A.