Programming Lecture
Programming Lecture
Introduction:
A programming language is a set of instructions and syntax used to create software programs.
Some of the key features of programming languages include:
1. Syntax: The specific rules and structure used to write code in a programming
language.
2. Data Types: The type of values that can be stored in a program, such as numbers,
strings, and booleans.
3. Variables: Named memory locations that can store values.
4. Operators: Symbols used to perform operations on values, such as addition,
subtraction, and comparison.
5. Control Structures: Statements used to control the flow of a program, such as if-
else statements, loops, and function calls.
6. Libraries and Frameworks: Collections of pre-written code that can be used to
perform common tasks and speed up development.
7. Paradigms: The programming style or philosophy used in the language, such as
procedural, object-oriented, or functional.
Examples of popular programming languages include Python, Java, C++, JavaScript, and Ruby.
Each language has its own strengths and weaknesses and is suited for different types of
projects.
A programming language is a formal language that specifies a set of instructions for a
computer to perform specific tasks. It’s used to write software programs and applications,
and to control and manipulate computer systems. There are many different programming
languages, each with its own syntax, structure, and set of commands. Some of the most
commonly used programming languages include Java, Python, C++, JavaScript, and C#. The
choice of programming language depends on the specific requirements of a project, including
the platform being used, the intended audience, and the desired outcome. Programming
languages continue to evolve and change over time, with new languages being developed and
older ones being updated to meet changing needs.
Are you aiming to become a software engineer one day? Do you also want to develop a
mobile application that people all over the world would love to use? Are you passionat e
enough to take the big step to enter the world of programming? Then you are in the right
place because through this article you will get a brief introduction to programming. Now
before we understand what programming is, you must know what is a computer. A computer
is a device that can accept human instruction, processes it, and responds to it or a computer
is a computational device that is used to process the data under the control of a computer
program. Program is a sequence of instruction along with data.
Most Popular Programming Languages –
• C
• Python
• C++
• Java
• SCALA
• C#
• R
• Ruby
• Go
• Swift
• JavaScript
Features of C++
2. Machine Independent
A C++ executable is not platform-independent (compiled programs on Linux won’t run on
Windows), however, they are machine-independent. Let us understand this feature of C++
with the help of an example. Suppose you have written a piece of code that can run on
Linux/Windows/Mac OSx which makes the C++ Machine Independent but the executable file
of the C++ cannot run on different operating systems.
3. Simple
It is a simple language in the sense that programs can be broken down into logical units and
parts, has rich library support and has a variety of data types. Also, the Auto Keyword of C++
makes life easier.
4. High-Level Language
C++ is a High-Level Language, unlike C which is a Mid-Level Programming Language. It makes
life easier to work in C++ as it is a high-level language it is closely associated with the human-
comprehensible English language.
5. Popular
C++ can be the base language for many other programming languages that supports the
feature of object-oriented programming. Bjarne Stroustrup found Simula 67, the first object-
oriented language ever, lacking simulations, and decided to develop C++.
6. Case-sensitive
It is clear that C++ is a case-sensitive programming language. For example, cin is used to take
input from the input stream. But if the “Cin” won’t work. Other languages like HTML and
MySQL are not case-sensitive languages.
7. Compiler Based
C++ is a compiler-based language, unlike Python. That is C++ programs used to be compiled
and their executable file is used to run them. C++ is a relatively faster language than Java and
Python.
9. Memory Management
• C++ allows us to allocate the memory of a variable or an array in run time. This is
known as Dynamic Memory Allocation.
• In other programming languages such as Java and Python, the compiler
automatically manages the memories allocated to variables. But this is not the case
in C++.
• In C++, the memory must be de-allocated dynamically allocated memory manually
after it is of no use.
• The allocation and deallocation of the memory can be done using the new and
delete operators respectively.
•
Writing First C++ Program – Hello World Example
C++ is a widely used Object Oriented Programming language and is relatively easy to
understand. The “Hello World” program is the first step towards learning any programming
language and is also one of the most straightforward programs you will learn.
The Hello World Program in C++ is the basic program that is used to demonstrate how the
coding process works. All you have to do is display the message “Hello World” on the console
screen.
return 0;
}
4. int main() { }
A function is a group of statements that are designed to perform a specific task. The main()
function is the entry point of every C++ program, no matter where the function is located in
the program.
The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’
indicates the ending of the main function.
5. cout<<“Hello World”;
std::cout is an instance of the std::ostream class, that is used to display output on the screen.
Everything followed by the character << in double quotes ” ” is displayed on the output
device. The semi-colon character at the end of the statement is used to indicate that the
statement is ending there.
6. return 0
This statement is used to return a value from a function and indicates the finishing of a
function. This statement is basically used in functions to return the results of the operations
performed by a function.
8. Indentation
As you can see the cout and the return statement have been indented or moved to the right
side. This is done to make the code more readable. We must always use indentations and
comments to make the code more readable.
1. Always include the necessary header files for the smooth execution of functions. For
example, <iostream> must be included to use std::cin and std::cout.
2. The execution of code begins from the main() function.
3. It is a good practice to use Indentation and comments in programs for easy
understanding.
4. cout is used to print statements and cin is used to take inputs.
C++ Variables
Variables in C++ is a name given to a memory location. It is the basic unit of storage in a
program.
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done on
the variable effects that memory location.
• In C++, all the variables must be declared before use.
How to Declare Variables?
A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;
Examples:
// Declaring float variable
float simpleInterest;
// Declaring integer variable
int time, speed;
// Declaring character variable
char var;
We can also provide values while declaring the variables as given below:
int a=50,b=100; //declaring 2 variable of integer type
float f=50.8; //declaring 1 variable of float type
char c='Z'; //declaring 1 variable of char type
Rules For Declaring Variable
• The name of the variable contains letters, digits, and underscores.
• The name of the variable is case sensitive (ex Arr and arr both are different
variables).
• The name of the variable does not contain any whitespace and special characters
(ex #,$,%,*, etc).
• All the variable names must begin with a letter of the alphabet or an
underscore(_).
• We cannot used C++ keyword(ex float,double,class)as a variable name.
Valid variable names:
int x; //can be letters
int _yz; //can be underscores
int z40;//can be letters
Invalid variable names:
int 89; Should not be a number
int a b; //Should not contain any whitespace
int double;// C++ keyword CAN NOT BE USED
Types of Variables
There are three types of variables based on the scope of variables in C++
• Local Variables
• Instance Variables
• Static Variables
•
Let us now learn about each one of these variables in detail.
1. Local Variables: A variable defined within a block or method or constructor is
called a local variable.
• These variables are created when entered into the block or the function
is called and destroyed after exiting from the block or when the call
returns from the function.
• The scope of these variables exists only within the block in which the
variable is declared. i.e. we can access this variable only within that
block.
• Initialization of Local Variable is Mandatory.
2. Instance Variables: Instance variables are non-static variables and are declared in a
class outside any method, constructor, or block.
• As instance variables are declared in a class, these variables are created
when an object of the class is created and destroyed when the object is
destroyed.
• Unlike local variables, we may use access specifiers for instance
variables. If we do not specify any access specifier then the default
access specifier will be used.
• Initialization of Instance Variable is not Mandatory.
• Instance Variable can be accessed only by creating objects.
3. Static Variables: Static variables are also known as Class variables.
• These variables are declared similarly as instance variables, the
difference is that static variables are declared using the static
keyword within a class outside any method constructor or block.
• Unlike instance variables, we can only have one copy of a static variable
per class irrespective of how many objects we create.
• Static variables are created at the start of program execution and
destroyed automatically when execution ends.
• Initialization of Static Variable is not Mandatory. Its default value is 0
• If we access the static variable like the Instance variable (through an
object), the compiler will show the warning message and it won’t halt
the program. The compiler will replace the object name with the class
name automatically.
• If we access the static variable without the class name, the Compiler will
automatically append the class name.
Instance Variable Vs Static Variable
• Each object will have its own copy of the instance variable whereas We can only
have one copy of a static variable per class irrespective of how many objects we
create.
• Changes made in an instance variable using one object will not be reflected in
other objects as each object has its own copy of the instance variable. In the case
of static, changes will be reflected in other objects as static variables are common
to all objects of a class.
• We can access instance variables through object references and Static Variables
can be accessed directly using the class name.
• The syntax for static and instance variables:
class Example
{
static int a; // static variable
int b; // instance variable
}
Constants in C
The constants in C are the read-only variables whose values cannot be modified once they are
declared in the C program. The type of constant can be an integer constant, a floating pointer
constant, a string constant, or a character constant. In C language, the const keyword is used
to define the constants.
What is a constant in C?
As the name suggests, a constant in C is a variable that cannot be modified once it is declared
in the program. We can not make any change in the value of the constant variables after they
are defined.
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable
can store like integer, character, floating, double, etc. Each data type requires different
amounts of memory and has some specific operations which can be performed over it. The
data type is a collection of data with values having fixed values, meaning as well as its
characteristics.
Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.
User Defined
The user-defined data types are defined by the user himself.
Data Types
The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.
Different data types also have different ranges up to which they can store numbers. These
ranges may vary from compiler to compiler. Below is a list of ranges along with the memory
requirement and format specifiers on the 32-bit GCC compiler.
Size Format
Data Type (bytes) Range Specifier
-2,147,483,648 to
int 4 %d
2,147,483,647
-2,147,483,648 to
long int 4 %ld
2,147,483,647
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
Note: The long, short, signed and unsigned are datatype modifier that can be used with some
primitive data types to change the size or length of the datatype.
The following are some main primitive data types in C:
Integer Data Type
The integer datatype in C is used to store the integer numbers(any number including positive,
negative and zero without decimal part). Octal values, hexadecimal values, and decimal
values can be stored in int data type in C.
• Range: -2,147,483,648 to 2,147,483,647
• Size: 4 bytes
• Format Specifier: %d
Syntax of Integer
We use int keyword to declare the integer variable:
int var_name;