PROGRAMMING CONCEPTS AND C++ BASICS – UNIT 1
PROGRAMMING CONCEPTS
What is a Computer Program?
A computer program is nothing but a set of instructions (smallest unit of
execution) that are used to execute particular tasks to get particular results. It is
required for programmers to learn basic concepts of mathematics to write
programs. For different types of tasks, we have to write different programs. The
set of instructions used to perform a specific task to obtain a specific result is
termed a computer program. The computer program is generated by
programmers or software developers. The code is then processed and executed
to provide the output of the program.
Structured programming (modular programming)
Structured programming, or modular programming, is a programming
paradigm that facilitates the creation of programs with readable code and
reusable components. All modern programming languages support
structured programming, but the mechanisms of support -- like the syntax of
the programming languages -- vary.
Object-oriented programming (OOP)
Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented
programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in
programming. The main aim of OOP is to bind together the data and the functions that operate on them so
that no other part of the code can access this data except that function.
OOPs Concepts:
Class
Objects
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
C++ BASICS:
INTRODUCTION TO C++
• C++ is a general-purpose programming language that was developed as an enhancement of the C language
to include object-oriented paradigm. It is an imperative and a compiled language.
• C++ is a high-level, general-purpose programming language designed for system and application
programming. It was developed by Bjarne Stroustrup at Bell Labs in 1983 as an extension of the C
programming language. C++ is an object-oriented, multi-paradigm language that supports procedural,
functional, and generic programming styles.
• C++ has a large, active community of developers and users, and a wealth of resources and tools available for
learning and using the language. Some of the key features of C++ include:
• Object-Oriented Programming: C++ supports object-oriented programming, allowing developers to create
classes and objects and to define methods and properties for these objects.
• Templates: C++ templates allow developers to write generic code that can work with any data type, making
it easier to write reusable and flexible code.
LAYOUT OF C++/Structure of C++ Program
Documentation Section:
This section comes first and is used to document the logic of the program that the programmer going to code.
It can be also used to write for purpose of the program.
Whatever written in the documentation section is the comment and is not compiled by the compiler.
Documentation Section is optional since the program can execute without them.
Linking Section:
The linking section contains two parts:Header Files,Namespaces:
Definition Section:
It is used to declare some constants and assign them some value.
Global Declaration Section:
Here, the variables And the class definitions which are going to be used in the program are declared to make them
global.
The scope of the variable declared in this section lasts until the entire program terminates.
Function Declaration Section:
It contains all the functions which our main functions need.
Usually, this section contains the User-defined functions.
This part of the program can be written after the main function but for this, write the function prototype in this
section for the function which for you are going to write code after the main function.
Main Function:
The main function tells the compiler where to start the execution of the program. The execution of the program
starts with the main function.
All the statements that are to be executed are written in the main function.
The compiler executes all the instructions which are written in the curly braces {} which encloses the body of the
main function.
Once all instructions from the main function are executed, control comes out of the main function and the
program terminates and no further execution occur.
C++ Data Types
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.
C++ supports the following data types:
Primary or Built-in or Fundamental data type
Derived data types
User-defined data types
C++ Data Types
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.
C++ supports the following data types:
Primary or Built-in or Fundamental data type
Derived data types
User-defined data 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.
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
User-Defined DataTypes
The data types that are defined by the user are called the derived datatype or user-defined derived data type.
These types include:
Class
Structure
Union
Enumeration
Typedef
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.
A typical variable declaration is of the form:
Syntax
type variableName = value;
Where type is one of C++ types (such as int), and variableName is the name of the variable (such as x or myName). The equal sign
is used to assign values to the variable.
Any example of your choice
Constants
Constants in C++ refer to variables with fixed values that cannot be changed. Once they are defined in the program, they remain
constant throughout the execution of the program.
We can define the constants in C++ using three ways:
• Using const Keyword
• Using constexpr Keyword
• Using #define Preprocessor
1. Constant using const Keyword
Defining constants using const keyword is one of the older methods that C++ inherited from C language. In this
method, we add the const keyword in the variable definition as
Syntax for const Keyword
const datatype variablename=value;
We first use the const keyword to make sure that our variable is a constant followed by the datatype of the
constant variable then proceed with the name of the constant variable and finally give the appropriate value
to the constant variable.
Note: We have to initialize the constant variable at the time of declaration as we cannot modify the value of it
later in the program.
Program :
# include<iostream.h>
#include<conio.h>
void main()
{
int a=2;
cout<<a;
const int b=4;
cout<<b;
}
2. Constants using constexpr Keyword
The constexpr keyword is similar to const keyword and is also used to declare constants in C++. But the major
difference is that the constexpr constants are initialized at compiler time, which is why their value must be
known at the compile time. On the other hand, const keyword constants can be initialized at runtime and
compile time.
Syntax for constexpr
constexpr datatype variable_name=value;
We first use the constexpr keyword to make sure that our variable is a constant followed by the DataType of the
constant variable then proceed with the name of the constant variable and finally give the appropriate value
to the constant variable.
Program
# include<iostream.h>
#include<conio.h>
void main()
{
int constexpr a=30;
cout<<a;
}
3. Constants using #define Preprocessor
We can also define constants using the #define. Constants created using the #define preprocessor are called
“macro constants”. Unlike the other methods, constants defined using this method simply work as an alias
for their value which is substituted during preprocessing.
Syntax:
#define MACRO_NAME replacement_value
We use the #define directive to create a macro. First, we start with the #define directive, followed by the name
we want to give to our macro. Then, we provide a replacement value for the macro. Whenever the
preprocessor encounters this macro in our code, it will replace it with the specified value.
Program
#include<iostream.h>
#include<conio.h>
#define Side 5
void main()
{
const double area=Side*Side;
cout<<area;
}
Important Points about Constants
The following are some major properties of C++ constants discussed in the article:
• Constants are the variables with fixed values.
• We have to initialize the constants at the time of declaration and we cannot change this value later in the
program.
• const and constexpr can be used to define a constant.
• #define is also used to define a macro constant.
Keywords
keywords play a crucial role in defining the syntax and functioning of the language. They include reserved words
with functions, such as specifying data types, managing program flow, and activating additional features.
list of 32 Keywords in C++ Language
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigne void volatile while
d
asm dynamic_cast namespace reinterpret_cas bool
t
explicit new static_cast false catch
operator template friend private class
this inline public throw const_cast
delete mutable protected true try
typeid typename using virtual wchar_t
Operators : An operator is a symbol that operates on a value to perform specific mathematical or logical
computations.
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Ternary or Conditional Operators
1) Arithmetic Operators
Program of your choice
2) Relational Operators
Program of your choice
3) Logical Operators
These operators are used to combine two or more conditions or constraints or to complement the evaluation of the
original condition in consideration. The result returns a Boolean value, i.e., true or false.
Program of your choice
4) Bitwise Operators These operators are used to perform bit-level operations on the operands .
Operator Name Description
& AND Sets each bit to
1 if both bits are
1
| OR Sets each bit to
1 if one of two
bits is 1
^ XOR Sets each bit to
1 if only one of
two bits is 1
~ NOT Inverts all the
bits
<< Zero fill left shift Shifts left by
pushing zeros in
from the right
and let the
leftmost bits fall
off
>> Signed right Shifts right by
shift pushing copies
of the leftmost
5) Assignment Operators
These operators are used to assign value to a variable. The left side operand of the assignment operator is a
variable and the right side operand of the assignment operator is a value. The value on the right side must be
of the same data type as the variable on the left side otherwise the compiler will raise an error.
Program of your choice
6) Ternary or Conditional Operators(?:)
This operator returns the value based on the condition.
expression1?expression2:expression3;
The ternary operator ? determines the answer on the basis of the evaluation of Expression1. If it is true,
then Expression2 gets evaluated and is used as the answer for the expression. If Expression1 is false,
then Expression3 gets evaluated and is used as the answer for the expression.
Program
Branching : Decision-making in C++ involves the usage of conditional statements (also called decision control
statements) to execute specific blocks of code primarily based on given situations and their results.
Types of Decision-Making Statements in C++
In C++, the following decision-making statements are available:
• if Statement
• if-else Statement
• if-else-if Ladder
• Nested if Statement
• switch Statement
• Conditional Operator
• Jump Statements: break, continue
1. if in C++
In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if
the given condition is true. The body of the ‘if’ statement is executed only if the given condition is true.
Syntax of if
Flowchart
Program
2. if-else in C++
The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition.
If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the
condition is false, the code inside the ‘else’ block is executed.
Syntax of if-else in C++
if (condition) {
// Code to be executed if the condition is true
}
else {
// Code to be executed if the condition is false
}
Program of if…else
3. if-else if
The if-else-if statements allow us to include additional situations after the preliminary if condition. The ‘else if’
condition is checked only if the above condition is not true. , and the `else` is the statement that will be
executed if none of the above conditions is true. If some condition is true, then no only the associated block
is executed.
Syntax if-else-if Ladder
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true
}
else {
// code to be executed if both the condition is false
}
Program
#include<iostream.h>
void main()
{
int age=18;
if(age<13){
cout<<“chile”;}
else if(age>=1 and age<=18)
{
cout<<“growing stage”;
}
else{
cout<<“adult”’;
}}
4. Nested if-else
The Nested if-else statement contains an ‘if’ statement inside another ‘if’ statement. This structure lets in more
complex selection-making by way of comparing multiple conditions. In this type of statement, multiple
conditions are checked, and then the body of the last if statement is executed.
if (condition1) {
// code to be executed if condition 1 is true
if (condition2) {
// code to be executed when condition 2 is true.
}
else {
// code to be executed if condition1 is true but condition2 is false.
}
}
else {
// code to be executed when condition 1 is false
}
Program of nested if else
5. Switch Statement
switch (expression) {
case value1:
// code to be executed if the value of expression is value1.
break;
case value2:
// code to be executed if the value of expression is value2.
break;
//…..
default:
// code to be executed if expression doesn't match any case.
}
Program of switch case
6. Ternary Operator ( ? : )
The conditional operator is also known as a ternary operator. It is used to write conditional operations provided
by C++. The ‘?’ operator first checks the given condition, if the condition is true then the first expression is
executed otherwise the second expression is executed. It is an alternative to an if-else condition in C++.
Syntax of Ternary Operator in C++
condition ? expression1 : expression2
Program of Conditional operator
7. Jump Statements in C++
Jump statements are used to alter the normal flow of the code. If you want to break the flow of the program
without any conditions then these jump statements are used. C++ provides four types of jump statements.
• break
• Continue
• goto
• return
A) break
break is a control flow statement that is used to terminate the loop and switch statements whenever a break
statement is encountered and transfer the control to the statement just after the loop.
break statement is generally used when the actual number of iterations are not predefined so we want to terminate
the loop based on some conditions.
Syntax
break;
LOOPING STATEMENTS:
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a
loop statement in most of the programming languages −
Program of break
B) continue
The continue statement is used to skip the loop body for the current iteration and continue from the next iteration.
Unlike the break statement which terminates the loop completely, continue allows us just to skip one
iteration and continue with the next iteration.
Syntax
continue;
Program of continue statement
C) goto
It is a jump statement that is used to transfer the control to another part of the program. It transfers the control
unconditionally to the labeled statement.
Syntax
goto label;
// ...
label:
// Statement or block of code
Program
#include<iostream.h>
void main()
{
int age=17;
if(age<18)
{
goto Noteligible;
}
else
{
cout<<“You can vote”;
}
Noteligible:
cout<<“u are not eligible”;
}
D) return
The return statement is used to exit the function immediately and optionally returns a value to the calling
function. It returns to the function from where it was called and if it is the ‘main’ function then it marks the
end of the execution. So basically, return is a mechanism used to communicate the results back to the calling
function.
Syntax
return expression;
C++ programming language provides the following type of loops to handle looping requirements.
While loop
In C++, while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is
recommended to use while loop than for loop.
while(condition){
//code to be executed
Flowchart:
Any program of while loop of your choice with output
For Loop
The C++ for loop is used to iterate a part of the program several times.
for(initialization; condition; incr/decr){
//code to be executed
}
Flowchart
Any program of for loop of your choice with output
Do-While Loop
The C++ do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while loop.
The C++ do-while loop is executed at least once because condition is checked after loop body.
do{
//code to be executed
}while(condition);
Flowchart
Any program of do..while loop of your choice with output