Basics of Computer Programming for Beginners
What is a Program?
Suppose, I give you 10 numbers and tell you to find the average of the given 10
numbers, then how do you find the average? You add all those numbers and
then divide the sum of the numbers by the total numbers given.
Easy task yeah. Now, if 10 sets and each set contain 10 numbers then what
would you do?
For this problem, there are two solutions to solve the problem.
1. You take one set, add each number then divide it by the total numbers.
If you are thinking to solve this problem by this procedure then it is
right to solve by this but if you are going to be a software engineer then
solving the problem through this approach is not preferred.
2. You can write a program to solve the problem. Just you need to write
a program in which the computer takes input from the user and then it
uses a procedure to find the average.
I have used the word procedure many times, what does it mean?
We define a procedure by giving steps one by one to our computer system and
we call it a program. Like, taking numbers from a user, adding them, and dividing
the sum by the total number is a procedure.
In short, a program is a set of instructions.
Variables and Syntax
Variables are sort of containers for storing data values, and it is also memory
location for the data type. There are certain rules that need to be followed before
declaring them. Variables are generally names allocated to values. The names
are alphanumeric, i.e., they have a-z and 0-9. Also, you can use special
characters while declaring a variable such as $ or _.
While writing a program be it for a small operation (addition, multiplication) or
building an application, you need to require variables. The variable declared for
a value should start with an alphabet and later may include numbers or special
characters.
Syntax is a set of rules that defines the structure of a language. Every
programming language follows a different syntax. A programming language
isn’t understandable without its syntax. Syntax helps the computer to read and
understand the code. It is like giving instructions to the code.
For example,
int a =10;
here a is variable
int is data type
10 is value
Data Types
Data type is a classification specifying the type of value stored in a variable. It
also indicates what type of operation can be applied to it. Data Types are
generally classified into two types:
1. Primitive – It specifies the size and type of variable. There are eight
primitive types
1. int (4 bytes)
2. short (2 bytes)
3. long (8 bytes)
4. float (4 bytes)
5. double (8 bytes)
6. boolean (1 byte)
7. char (2 bytes)
2. Non-Primitive – Non-primitive types are used to call methods to
perform operations.
1. String (8 bytes)
2. Arrays
3. Class (empty class – 1 byte)
4. Interface
Flow Control Structures
Flow Control Structures are the ways to specify the flow of controls in a
program. A program is well clear when control structures are used in it. Its main
purpose is to analyze the program. They are 3 types:
1. Sequential – Sequential is the execution of code line by line or one
after the other. For example, cooking an item.
2. Selection – Selection is deciding whether the given condition is true
or false and on the basis of which it produces the final result.
3. Iteration (Loops) – A loop is a structure in which a statement is
repeated again and again until the given condition is satisfied. They
are of three types –
• For
• While
• Do-While
Why do we need to Learn Any Programming Language?
A computer or any programmable device understands machine language
because the computer works on electricity and electricity works on binary
numbers(0 and 1). We humans only understand natural language. To fulfill this
gap between humans and computers programming language is introduced.
What happens if either human understands machine language or a machine
understands natural language?
If that happens then we don’t need to know any programming language to give
instructions to machines and anyone can be a programmer if that situation
happens.
Programming Methods
There are three programming methodologies so far mostly used.
1. Monolithic Programming
2. Modular/ Procedural Programming
3. Object Oriented Programming
Monolithic Programming: It was practiced when programming was just
introduced. In monolithic programming, everything from code, data, and
instruction, is in a single file which makes it difficult to review the code. We are
not reusing the code.
Modular/ Procedural Programming: Procedural programming helps to divide
the work among a team and we also use function so that it allows for to reuse
of the code. Data and function are used separately.
Object Oriented Programming: OOPs are widely used in industries, we take
data and functions together and created them as a class.
In all these programming methods codes remain the same, and the logic remains
the same but one thing changes is how you organize your program code.
What is an Algorithm?
The algorithm is a step-by-step procedure for solving computational problems.
Programs mean the same, is it?
Every programming language follows a specific syntax that allows us to write
code so that it is easily understandable to the machine. Whereas, in
the algorithm, we write pseudocode. Pseudocode is an informal language that
helps programmers develop algorithms. It is neither in English nor in code.
Let’s see pseudocode for the algorithm of an average of numbers.
sum == 0, n= total number of elements
for each element num in list
begin
sum = sum + num
average = sum/x
return average
Top Most Popular Programming Languages
Learning a programming language before learning any technology is a must,
hence, it’s very important to have full command of any one programming
language. Choosing any one programming language instead of going for many
is better. Try working on basic problems using different programming
languages, and whichever seems easy to you, you can prefer working on that.
• C/C++
• Java
• JavaScript
• Python
• Kotlin
• Swift
• R
• Ruby
• Scala
• TypeScript
C++ is a general-purpose programming language and is widely used nowadays for competitive
programming. It has imperative, object-oriented and generic programming features. C++ runs on
lots of platforms like Windows, Linux, Unix, Mac etc.
C++ is a widely used Object Oriented Programming language and is relatively
easy to understand.
Writing First C++ Program – Hello World
Example
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.
All you have to do is display the message “Hello World” on the screen. Let us
now look at the program:
// 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;
}
C++ Basic Syntax
C++ is a general-purpose, object-oriented programming language. It was
developed in 1979 by Bjarne Stroustrup at AT & T Bell Laboratory. It is a high-
level programming language & advanced version of C programming language.
As Compared to other programming languages like Java, and Python, it is the
fastest programming language.
What is Syntax?
Syntax refers to the rules and regulations for writing statements in a
programming language. They can also be viewed as the grammatical rules
defining the structure of a programming language.
The C++ language also has its syntax for the functionalities it provides. Different
statements have different syntax specifying their usage but C++ programs also
have basic syntax rules that are followed throughout all the programs.
Basic Syntax of a C++ Program
We can learn about basic C++ Syntax using the following program.
The image above shows the basic C++ program that contains header files, main
function, namespace declaration, etc. Let’s try to understand them one by one.
1. Header File
The header files contain the definition of the functions and macros we are using
in our program. They are defined on the top of the C++ program.
In line #1, we used the #include <iostream> statement to tell the compiler to
include an iostream header file library which stores the definition of the cin and
cout methods that we have used for input and output. #include is a
preprocessor directive using which we import header files.
Syntax:
#include <library_name>
C++ Language also offers its users a variety of functions, one of which is
included in header files. In C++, all the header files may or may not end with
the “.h” extension.
It offers the features like library functions, data types, macros, etc by importing
them into the program with the help of a preprocessor directive “#include”.
These preprocessor directives are used to instruct the compiler that these
files need to be processed before compilation.
The “#include” preprocessor directs the compiler that the header file needs to
be processed before compilation and includes all the necessary data types and
function definitions.
Example:
// C++ program to demonstrate the use of header files
// standard input and output header file iostream
#include <iostream>
using namespace std;
int main()
{
cout << "cout is a method of iostream header file";
return 0;
}
2. Namespace
A namespace in C++ is used to provide a scope or a region where we define
identifiers. It is used to avoid name conflicts between two identifiers as only
unique names can be used as identifiers.
In line #2, we have used the using namespace std statement for specifying
that we will be the standard namespace where all the standard library
functions are defined.
Syntax:
using namespace std;
3. Main Function
Functions are basic building blocks of a C++ program that contains the
instructions for performing some specific task. Apart from the instructions
present in its body, a function definition also contains information about its
return type and parameters.
In line #3, we defined the main function as int main(). The main function is the
most important part of any C++ program. The program execution always starts
from the main function. All the other functions are called from the main function.
In C++, the main function is required to return some value indicating the
execution status.
Syntax:
int main() {
... code ....
return 0;
}
4. Blocks
Blocks are the group of statements that are enclosed within { } braces. They
define the scope of the identifiers and are generally used to enclose the body
of functions and control statements.
The body of the main function is from line #4 to line #9 enclosed within { }.
Syntax:
{
// Body of the Function
return 0;
}
5. Semicolons
As you may have noticed by now, each statement in the above code is followed
by a ( ; ) semicolon symbol. It is used to terminate each line of the statement of
the program. When the compiler sees this semicolon, it terminates the operation
of that line and moves to the next line.
Syntax:
any_statement ;
6. Identifiers
We use identifiers for the naming of variables, functions, and other user-defined
data types. An identifier may consist of uppercase and lowercase alphabetical
characters, underscore, and digits. The first letter must be an underscore or an
alphabet.
Example:
int num1 = 24;
int num2 = 34;
num1 & num2 are the identifiers and int is the data type.
7. Keywords
In the C++ programming language, there are some reserved words that are used
for some special meaning in the C++ program. It can’t be used for identifiers.
For example, the words int, return, and using are some keywords used in our
program. These all have some predefined meaning in the C++ language.
There are total 95 keywords in C++. These are some keywords.
int void if while for
auto bool break
this static new true false
case char class
8. Data Types in C++
All variables use data type during declaration to restrict the type of data to be
stored. Therefore, we can say that data types are used to tell the variables the
type of data they can store. Whenever a variable is defined in C++, the compiler
allocates some memory for that variable based on the data type with which it is
declared. Every data type requires a different amount of memory.
Data types specify the size and types of values to be stored.
C++ supports the following data types:
1. Primary or Built-in or Fundamental data type
2. Derived data types
3. User-defined data types
Data Types in C++ are Mainly Divided into 3 Types:
1. Primitive Data Types: These data types are built-in or predefined data types
and can be used directly by the user to declare variables. example: int, char,
float, bool, etc. Primitive data types available in C++ are:
• Integer
• Character
• Boolean
• Floating Point
• Double Floating Point
• Valueless or Void
• Wide Character
2. Derived Data Types: Derived data types that are derived from the primitive
or built-in datatypes are referred to as Derived Data Types. These can be of four
types namely:
• Function
• Array
• Pointer
• Reference
3. Abstract or User-Defined Data Types: Abstract or User-Defined data
types are defined by the user itself. Like, defining a class in C++ or a structure.
C++ provides the following user-defined datatypes:
• Class
• Structure
• Union
• Enumeration
• Typedef defined Datatype
Primitive Data Types
• Integer: The keyword used for integer data types is int. Integers
typically require 4 bytes of memory space and range from -
2147483648 to 2147483647.
• Character: Character data type is used for storing characters. The
keyword used for the character data type is char. Characters typically
require 1 byte of memory space and range from -128 to 127 or 0 to
255.
• Boolean: Boolean data type is used for storing Boolean or logical
values. A Boolean variable can store either true or false. The keyword
used for the Boolean data type is bool.
• Floating Point: Floating Point data type is used for storing single-
precision floating-point values or decimal values. The keyword used
for the floating-point data type is float. Float variables typically require
4 bytes of memory space.
• Double Floating Point: Double Floating Point data type is used for
storing double-precision floating-point values or decimal values. The
keyword used for the double floating-point data type is double.
Double variables typically require 8 bytes of memory space.
• void: Void means without any value. void data type represents a
valueless entity. A void data type is used for those function which does
not return a value.
• Wide Character: Wide character data type is also a character data
type but this data type has a size greater than the normal 8-bit data
type. Represented by wchar_t. It is generally 2 or 4 bytes long.
• sizeof() operator: sizeof() operator is used to find the number of
bytes occupied by a variable/data type in computer memory.
The below table summarizes the modified size and range of built-in datatypes
when combined with the type modifiers:
Data Type Size (in bytes) Range
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
Data Type Size (in bytes) Range
int 4 -2,147,483,648 to 2,147,483,647
long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4 -3.4×10^38 to 3.4×10^38
double 8 -1.7×10^308 to1.7×10^308
long double 12 -1.1×10^4932 to1.1×10^4932
wchar_t 2 or 4 1 wide character
9. Basic Input / Output in C++
C++ comes with libraries that provide us with many ways for performing input
and output. In C++ input and output are performed in the form of a sequence of
bytes or more commonly known as streams.
• Input Stream: If the direction of flow of bytes is from the device(for
example, Keyboard) to the main memory then this process is called
input.
• Output Stream: If the direction of flow of bytes is opposite, i.e. from
main memory to device( display screen ) then this process is called
output.
Header files available in C++ for Input/Output operations are:
1. iostream: iostream stands for standard input-output stream. This
header file contains definitions of objects like cin, cout, cerr, etc.
2. iomanip: iomanip stands for input-output manipulators. The methods
declared in these files are used for manipulating streams. This file
contains definitions of setw, setprecision, etc.
3. fstream: This header file mainly describes the file stream. This header
file is used to handle the data being read from a file as input or data
being written into the file as output.
The two instances cout in C++ and cin in C++ of iostream class are used very often
for printing outputs and taking inputs respectively. These two are the most basic
methods of taking input and printing output in C++. To use cin and cout in C++
one must include the header file iostream in the program.
The C++ cout statement is the instance of the ostream class. It is used to
produce output on the standard output device which is usually the display
screen. The data needed to be displayed on the screen is inserted in the standard
output stream (cout) using the insertion operator(<<).
C++ cin statement is the instance of the class istream and is used to read input
from the standard input device which is usually a keyboard.
The extraction operator(>>) is used along with the object cin for reading inputs.
The extraction operator extracts the data from the object cin which is entered
using the keyboard.
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: " << age;
return 0;
}