0% found this document useful (0 votes)
13 views

Programming Lecture

A programming language is a formal system used to create computer programs and applications. It consists of syntax rules, data types, variables, operators, and control structures. Popular programming languages include Python, Java, C++, JavaScript, and Ruby. Each language has strengths for different types of projects. Programming languages continue to evolve over time to meet changing needs.

Uploaded by

jrmatibag08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Lecture

A programming language is a formal system used to create computer programs and applications. It consists of syntax rules, data types, variables, operators, and control structures. Popular programming languages include Python, Java, C++, JavaScript, and Ruby. Each language has strengths for different types of projects. Programming languages continue to evolve over time to meet changing needs.

Uploaded by

jrmatibag08
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Introduction to Programming Languages

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

Characteristics of a programming Language –


• A programming language must be simple, easy to learn and use, have good
readability, and be human recognizable.
• Abstraction is a must-have Characteristics for a programming language in which the
ability to define the complex structure and then its degree of usability comes.
• A portable programming language is always preferred.
• Programming language’s efficiency must be high so that it can be easily converted
into a machine code and its execution consumes little space in memory.
• A programming language should be well structured and documented so that it is
suitable for application development.
• Necessary tools for the development, debugging, testing, maintenance of a
program must be provided by a programming language.
• A programming language should provide a single environment known as Integrated
Development Environment(IDE).
• A programming language must be consistent in terms of syntax and semantics.

Basic Terminologies in Programming Languages:

• Algorithm: A step-by-step procedure for solving a problem or performing a task.


• Variable: A named storage location in memory that holds a value or data.
• Data Type: A classification that specifies what type of data a variable can hold,
such as integer, string, or boolean.
• Function: A self-contained block of code that performs a specific task and can be
called from other parts of the program.
• Control Flow: The order in which statements are executed in a program, including
loops and conditional statements.
• Syntax: The set of rules that govern the structure and format of a programming
language.
• Comment: A piece of text in a program that is ignored by the compiler or
interpreter, used to add notes or explanations to the code.
• Debugging: The process of finding and fixing errors or bugs in a program.
• IDE: Integrated Development Environment, a software application that provides a
comprehensive development environment for coding, debugging, and testing.
• Operator: A symbol or keyword that represents an action or operation to be
performed on one or more values or variables, such as + (addition), – (subtraction),
* (multiplication), and / (division).
• Statement: A single line or instruction in a program that performs a specific action
or operation.

Advantages of programming languages:

1. Increased Productivity: Programming languages provide a set of abstractions that


allow developers to write code more quickly and efficiently.
2. Portability: Programs written in a high-level programming language can run on
many different operating systems and platforms.
3. Readability: Well-designed programming languages can make code more readable
and easier to understand for both the original author and other developers.
4. Large Community: Many programming languages have large communities of users
and developers, which can provide support, libraries, and tools.

Disadvantages of programming languages:

1. Complexity: Some programming languages can be complex and difficult to learn,


especially for beginners.
2. Performance: Programs written in high-level programming languages can run
slower than programs written in lower-level languages.
3. Limited Functionality: Some programming languages may not have built-in
support for certain types of tasks or may require additional libraries to perform
certain functions.
4. Fragmentation: There are many different programming languages, which can lead
to fragmentation and make it difficult to share code and collaborate with other
developers.

Tips for learning new programming language:


1. Start with the fundamentals: Begin by learning the basics of the language, such as
syntax, data types, variables, and simple statements. This will give you a strong
foundation to build upon.
2. Code daily: Like any skill, the only way to get good at programming is by practicing
regularly. Try to write code every day, even if it’s just a few lines.
3. Work on projects: One of the best ways to learn a new language is to work on a
project that interests you. It could be a simple game, a web application, or
anything that allows you to apply what you’ve learned that is the most important
part.
4. Read the documentation: Every programming language has documentation that
explains its features, syntax, and best practices. Make sure to read it thoroughly to
get a better understanding of the language.
5. Join online communities: There are many online communities dedicated to
programming languages, where you can ask questions, share your code, and get
feedback. Joining these communities can help you learn faster and make
connections with other developers.
6. Learn from others: Find a mentor or someone who is experienced in the language
you’re trying to learn. Ask them questions, review their code, and try to
understand how they solve problems.
7. Practice debugging: Debugging is an essential skill for any programmer, and you’ll
need to do a lot of it when learning a new language. Make sure to practice
identifying and fixing errors in your code.

Features of C++

C++ is a general-purpose programming language that was developed as an enhancement of


the C language to include an object-oriented paradigm. It is an imperative
and compiled language. C++ has a number of features, including:
• Object-Oriented Programming
• Machine Independent
• Simple
• High-Level Language
• Popular
• Case-sensitive
• Compiler Based
• Dynamic Memory Allocation
• Memory Management
• Multi-threading
1. Object-Oriented Programming
C++ is an Object-Oriented Programming Language, unlike C which is a procedural
programming language. This is the most important feature of C++. It can create/destroy
objects while programming. Also, It can create blueprints with which objects can be created.

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.

8. Dynamic Memory Allocation


When the program executes in C++ then the variables are allocated the dynamical heap
space. Inside the functions, the variables are allocated in the stack space. Many times, We are
not aware in advance how much memory is needed to store particular information in a
defined variable and the size of required memory can be determined at run time.

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.

C++ Hello World Program


C++

// C++ program to display "Hello World"

// Header file for input output functions


#include <iostream>
using namespace std;

// Main() function: where the execution of


// program begins
int main()
{
// Prints hello world
cout << "Hello World";

return 0;
}

Working of Hello World Program in C++


Let us now understand every line and the terminologies of the above program.

1. // C++ program to display “Hello World”


This line is a comment line. A comment is used to display additional information about the
program. A comment does not contain any programming logic.
When a comment is encountered by a compiler, the compiler simply skips that line of code.
Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is a comment.
2. #include
This is a preprocessor directive. The #include directive tells the compiler to include the
content of a file in the source code.
For example, #include<iostream> tells the compiler to include the standard iostream
file which contains declarations of all the standard input/output library functions.

3. using namespace std


This is used to import the entity of the std namespace into the current namespace of the
program. The statement using namespace std is generally considered a bad practice. When
we import a namespace we are essentially pulling all type definitions into the current scope.
The std namespace is huge. The alternative to this statement is to specify the namespace to
which the identifier belongs using the scope operator(::) each time we declare a type. For
example, std::cout.

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;

// Declaring multiple variables:


type variable1_name, variable2_name, variable3_name;
A variable name can consist of alphabets (both upper and lower case), numbers, and the
underscore ‘_’ character. However, the name must not start with a number.

datatype: Type of data that can be stored in this variable.


variable_name: Name given to the variable.
value: It is the initial value stored in the variable.

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.

How to Define Constant in C?


We define a constant in C language using the const keyword. Also known as a const type
qualifier, the const keyword is placed at the start of the variable declaration to declare that
variable as a constant.

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.

The data types in C can be classified as follows:


Types Description

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

short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

-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

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long 0 to


8 %llu
int 18,446,744,073,709,551,615
Size Format
Data Type (bytes) Range Specifier

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 %f
1.2E-38 to 3.4E+38

double 8 %lf
1.7E-308 to 1.7E+308

long double 16 %Lf


3.4E-4932 to 1.1E+4932

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;

The integer data type can also be used as


1. unsigned int: Unsigned int data type in C is used to store the data values from zero
to positive numbers but it can’t store negative values like signed int.
2. short int: It is lesser in size than the int by 2 bytes so can only store values from -
32,768 to 32,767.
3. long int: Larger version of the int datatype so can store values greater than int.
4. unsigned short int: Similar in relationship with short int as unsigned int with int.
Note: The size of an integer data type is compiler-dependent. We can use sizeof operator to
check the actual size of any data type.

You might also like