Programming Fundamental: Lecture-2-3
Programming Fundamental: Lecture-2-3
Lecture-2-3
Today’s Lecture
What is Programming?
Programming Errors
Variables in C++
Operators in C++
Operators Precedence
2
What is Programming?
4
First Program in C++
#include <conio.h>
#include <iostream>
main()
{
cout<<" Welcome to G C University";
getch();
}
→# is H A S H and also called SHARP
→#include: This is a pre-processor directive. It is not part of our program; it is
an instruction to the compiler. It tells the C compiler to include the
contents of a file i.e. iostream. The compiler knows that it is a
system file, and therefore looks for it in a special place. 5
First Program in C++
Namespace in C++
Different libraries may have functions or variables with same name
A namespace is designed to overcome this difficulty by using additional
information to differentiate similar functions, classes, variables etc. with
the same name available in different libraries.
Using namespace, you can define the context in which names are defined.
In essence, a namespace defines a scope.
A namespace definition begins with the keyword namespace followed by
the namespace name as follows:
namespace namespace_name;
6
First Program in C++
name_space::code;
You can also avoid prepending of namespaces with the using namespace
directive which tells the compiler that the subsequent code is making use
of names in the specified namespace
The using directive can also be used to refer to a particular item within a
namespace. For example,
using std::cout;
7
First Program in C++
#include directive
Both user and system header files are included using the preprocessing
directive ‘#include’.
main() Function
All C++ Must have main() function
When the operating system runs a program in C, it passes control of the
computer over to that program.
The main() function uses its parentheses() to contain any information
typed after the program name at the command prompt. This is useful for
more advanced programming.
The curly braces {} are used for organization. They contain programming
instructions that belong to the function. Those programming instructions
are how the function carries out its task or does its thing.
Without main() function a C++ program will not execute.
9
First Program in C++
cout in C++
The predefined object cout is an instance of ostream class.
The cout object is said to be "connected to" the standard output device,
which usually is the display screen.
The cout is used in conjunction with the stream insertion operator, which
is written as << which are two less than signs indicates the direction of
data
10
First Program in C++
11
Programming Errors
12
Variables in C++
What is Variable?
A variable is used to store a piece of data for processing.
It is called variable because you can change the stored value with new
value during program execution
A variable is a named storage location, that stores a value of a particular
data type
13
Variables in C++
Variable
14
Variables in C++
Dissecting a Variable
In a Program a variable Consists of:
Name: Used as identifier for the variable. e.g. radius, area, age
Type: Represents the type of data a variable will hold. e.g. Integer,
Float
15
Variables in C++
16
Variables in C++
and plural variables. For example, you may use the variable row to refer to a
single row number and the variable rows to refer to many rows.
17
Variables in C++
18
Variable Data Types
Data Types
While doing programming in any programming language, you need to use
various variables to store various information.
Variables are nothing but reserved memory locations to store values, this
means variable reserve some space in memory.
Information store in variables may be 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.
A data type is set of values and operations performed on those value
19
Variable Data Types
20
21
22
Sample Program
// This program illustrates how data in the variables are manipulated.
#include <iostream>
#include <string>
using namespace std;
int main() {
int num1, num2;
double sale;
char first;
string str;
num1 = 4;
cout << "num1 = " << num1 << endl;
num2 = 4 * 5 - 11;
cout << "num2 = " << num2 << endl;
sale = 0.02 * 1000;
cout << "sale = " << sale << endl;
first = 'D';
cout << "first = " << first << endl;
str = "It is a sunny day.";
cout << "str = " << str << endl;
return 0;
}
Exercise
Suppose that num1, num2, and num3 are int variables and the following
statements are executed in sequence.
1. num1 = 18;
2. num1 = num1 + 27;
3. num2 = num1;
4. num3 = num2 / 5;
5. num3 = num3 / 4;
Consider the following program segment:
//include statement(s)
//using namespace statement
int main()
{
//variable declaration
//executable statements
//return statement
}
a. Write a statement that includes the header file iostream.
b. Write a statement that allows you to use cin, cout, and endl without the prefix std::.
c. Write statement(s) that declare the following variables: num1,num2, num3, and
average of type int.
d. Write statements that store 125 into num1, 28 into num2, and -25 into num3.
e. Write a statement that stores the average of num1, num2, and num3 into average.
f. Write statement(s) that output the values of num1, num2, num3, and average.
g. Compile and run your program.
Repeat Programming Exercise above by declaring num1, num2, and num3, and
average of type double. Store 75.35 into num1, -35.56 into num2, and 15.76 into
num3.
Variable Data Types
Sizeof Operator
The sizeof is a keyword, but it is a compile-time operator that determines
the size, in bytes, of a variable or data type.
The sizeof operator can be used to get the size of classes, structures, unions
and any other user defined data type.
The syntax of using sizeof is as follows:
26
Variable Data Types
typedef Declaration
You can create a new name for an existing type using typedef. Syntax to
define a new type using typedef is:
typedef type newname;
The following tells the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer
variable called distance:
feet distance;
28
Primitive Data Types
Enclosed in single quotation marks. e.g. 'A', 'a', '0', '*', '+', '$', '&', ‘ ‘
29
Operators in C++
What is an operator?
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
C++ is rich in built-in operators and provides the following types of
operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
30
Operators in C++
Arithmetic Operator
Following are arithmetic operators available in C++
Assume variable A holds 10 and variable B holds 20, then:
31
Operators in C++
Relational Operator
Relational operators are used to compare two values or expressions to
evaluate the relationship.
Comparing floating point values using any of the relational operators is
dangerous. This is because small rounding errors in the floating point
operands may cause an unexpected result.
A relational operator comparison will always return one of the two
possible values either true or false.
For the example on next slide assume variable A holds 10 and variable B
holds 20, then:
32
Operators in C++
Relational Operator
33
Operators in C++
Logical Operator
The logical operators, logical A N D (&&) and logical OR (||), are used to
combine multiple conditions formed using relational or equality
expressions
The operator && corresponds to the Boolean logical operation A N D,
which yields true if both its operands are true, and false otherwise.
The operator | | corresponds to the Boolean logical operation OR, which
yields true if either of its operands is true, thus being false only when both
operands are false.
The Logical Operator, logical N OT (!) has only one operand, to its right,
and inverts it, producing false if its operand is true, and true if its operand
is false.
34
Operators in C++
Bitwise Operator
Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, | , and ^ are as follows:
35
Operators in C++
Assignment Operator
An assignment operator is the operator used to assign a new value to a
variable
36
Assignment Rules
Assignment Rules
assigns a literal value (of the RHS) to a variable (of the LHS)
evaluates an expression (of the RHS) and assign the resultant value to a
variable (of the LHS).
The RHS shall be a value; and the LHS shall be a variable (or memory
address).
Some Valid and Invalid Assignments
X+ 3 = y + 4 Wrong
x +4 = Z Wrong
2=x Wrong
Z = x +4 Correct
x=y Correct
x=2 Correct
37
Assignment Example
38
Operator Precedence
What is Precedence
In mathematics and computer programming, the order of operations
(sometimes called operator precedence) is a rule used to clarify which
procedures should be performed first in a given mathematical expression.
Highest: ( )
Next: * , /, %
Lowest: + , -
39