Computer Programming
Computer Programming
PROGRAMMING I
INTRODUCTION TO C++
• C++ Program Structure
• The basic structure of a C++ program consists of the following parts:
• Header file inclusion section: This is the section where we include all
required header files whose functions we are going to use in the
program.
• Namespace section: This is the section where we use the namespace.
• The main() section: In this section, we write our main code. The
main() function is an entry point of any C++ programming code from
where the program's execution starts.
#include <iostream>
using namespace std;
• /*
• C++ comments can also
• span multiple lines
• */
• To represent a short and concise step in the program for users to understand
better.
• To explain a step in a detailed way that is not expressed explicitly in the code.
• To leave different hints for users to grab in the code itself.
• To leave comments for fun or recreation.
• To temporarily disable part of the code for debugging purposes.
• To add metadata to the code for future purposes.
• To create documentation for the code, for example, in Github pages.
• C++ Constants/Literals
• Constants refer to fixed values that the program may not alter and they are called
literals.
• Constants can be of any of the basic data types and can be divided into Integer
Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.
• Again, constants are treated just like regular variables except that their values
cannot be modified after their definition.
• Defining Constants
• There are two simple ways in C++ to define constants −
• Using #define preprocessor.
• Using const keyword.
• The #define Preprocessor
• Following is the form to use #define preprocessor to define a constant
−
• #define identifier value
• e.g: #define LENGTH 10
• Type Keyword
• Boolean bool
• Character char
• Integer int
• Floating point float
• Double floating point double
• Valueless void
• Wide character wchar_t
• C++ Variable
• 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.
• int i, j, k;
• char c, ch;
• float f, salary;
• double d;
• The line int i, j, k; both declares and defines the variables i, j and k; which
instructs the compiler to create variables named i, j and k of type int.
• Variable Initialization in C++
• Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as follows −
• Syntax
• type variable_name = value;
• Example:
• extern int d = 3, f = 5; // declaration of d and f.
• int d = 3, f = 5; // definition and initializing d and f.
• char x = 'x'; // the variable x has the value 'x'.
• For definition without an initializer: variables with static storage duration are
implicitly initialized with NULL (all bytes have the value 0); the initial value of all
other variables is undefined.
• Variable Scope in C++
• A scope is a region of the program and broadly speaking there are three places,
where variables can be declared −
• Inside a function or a block which is called local variables,
• In the definition of function parameters which is called formal parameters.
• Outside of all functions which is called global variables.
• Global Variables
• Global variables are defined outside of all the functions, usually on top of the
program. The global variables will hold their value throughout the life-time of
your program.
• A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration.
• Local and Global Variables with Same Names
• A program can have same name for local and global variables but value of local
variable inside a function will take preference.
• Example
cout << "Please enter your name: ";
cin >> name;
• Storage Classes in C++
• A storage class defines the scope (visibility) and life-time of variables
and/or functions within a C++ Program. These specifiers precede the
type that they modify. There are following storage classes, which can
be used in a C++ Program
• auto
• register
• static
• extern
• mutable
• The auto Storage Class
• The auto storage class is the default storage class for all local variables.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
• Arithmetic Operators
• Arithmetic operators in C++ are the basic operators, which are used for basic mathematical or arithmetical
operations on operands. These operators are essential for performing calculations and manipulating data
within a program.
• There are following arithmetic operators supported by C++ language −
• Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
main() {
int a = 21;
int b = 10;
int c ;
if( a == b ) {
cout << "Line 1 - a is equal to b" << endl ;
} else {
cout << "Line 1 - a is not equal to b" << endl ;
}
return 0;
}
• Logical Operators
• Logical operators are used to perform logical operations on boolean values (true
or false). These operators are essential for controlling the flow of a program
based on conditions. There are three primary logical operators in C++ as
mentioned below −
• There are following logical operators supported by C++ language.
• Assume variable A holds 1 and variable B holds 0, then −
Operator Description Example
main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout << "Line 1 - Condition is true"<< endl ;
}
if(a || b) {
cout << "Line 2 - Condition is true"<< endl ;
}
if(a && b) {
cout << "Line 3 - Condition is true"<< endl ;
} else {
cout << "Line 4 - Condition is not true"<< endl ;
}
return 0;
}
• Bitwise Operators
• Bitwise operators are used to perform operations at the bit level on integer data
types. These operations work on direct manipulation of bits, such as low-level
programming, graphics, and cryptography.
• Bitwise operator works on bits and perform bit-by-bit operation. The truth tables
for &, |, and ^ are as follows −
p q p & q p | q p ^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Operator Description Example
Binary AND Operator copies a bit to the result if it exists (A & B) will give 12 which is 0000
&
in both operands. 1100
Binary OR Operator copies a bit if it exists in either (A | B) will give 61 which is 0011
|
operand. 1101
Binary XOR Operator copies the bit if it is set in one (A ^ B) will give 49 which is 0011
^
operand but not both. 0001
(~A ) will give -61 which is 1100
Binary Ones Complement Operator is unary and has the
~ 0011 in 2's complement form due
effect of 'flipping' bits.
to a signed binary number.
Binary Left Shift Operator. The left operands value is
A << 2 will give 240 which is 1111
<< moved left by the number of bits specified by the right
0000
operand.
Add AND assignment operator, It adds right operand to the left C += A is equivalent to C =
+=
operand and assign the result to left operand. C+A
C <<= 2 is same as C = C
<<= Left shift AND assignment operator.
<< 2
C >>= 2 is same as C = C
>>= Right shift AND assignment operator.
>> 2
C &= 2 is same as C = C &
&= Bitwise AND assignment operator.
2
C ^= 2 is same as C = C ^
^= Bitwise exclusive OR and assignment operator.
2
main() {
int a = 21;
int c ;
c = a;
cout << "Line 1 - = Operator, Value of c = : " <<c<< endl ;
c <<= 2;
c += a; cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ;
cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ;
c -= a;
c >>= 2;
cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ; cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ;
c *= a; c &= 2;
cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ; cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;
c /= a;
c ^= 2;
cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ;
cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;
c = 200;
c %= a; c |= 2;
cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ; cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;
return 0;
}
• Misc Operators
• Miscellaneous operators, often abbreviated as "misc operators", include a variety
of operators that don’t fit neatly into other categories like arithmetic or logical
operators. Here are some common miscellaneous operators and their definitions
−
• The following table lists some other operators that C++ supports.
Sr.No Operator & Description
sizeof
1 sizeof operator returns the size of a variable. For example, sizeof(a), where ‘a’ is
integer, and will return 4.
Condition ? X : Y
2 Conditional operator (?). If Condition is true then it returns value of X otherwise
returns value of Y.
,
Comma operator causes a sequence of operations to be performed. The value of
3
the entire comma expression is the value of the last expression of the comma-
separated list.
. (dot) and -> (arrow)
4 Member operators are used to reference individual members of classes,
structures, and unions.
Cast
5 Casting operators convert one data type to another. For example, int(2.2000)
would return 2.
&
6 Pointer operator & returns the address of a variable. For example &a; will give
actual address of the variable.
*
7 Pointer operator * is pointer to a variable. For example *var; will pointer to a
variable var.
• Operators Precedence in C++
• Operator precedence determines the grouping of terms in an expression. This
affects how an expression is evaluated. Certain operators have higher precedence
than others; for example, the multiplication operator has higher precedence than
the addition operator −
• Here, operators with the highest precedence appear at the top of the table, those
with the lowest appear at the bottom. Within an expression, higher precedence
operators will be evaluated first.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
cout << "Value of (a + b) * c / d is :" << e << endl ;
e = ((a + b) * c) / d; // (30 * 15 ) / 5
cout << "Value of ((a + b) * c) / d is :" << e << endl ;
e = a + (b * c) / d; // 20 + (150/5)
cout << "Value of a + (b * c) / d is :" << e << endl ;
return 0;
}
Classwork
• Write a C++ program to perform arithmetic operations: addition,
subtraction, division, multiplication and modulus
• Write a C++ program to convert Fahrenheit to Celsius and vice versa