Programming fundamentals
Programming fundamentals
• The conditional statements (also known as decision control structures) such as if, if else,
switch, etc. are used for decision-making purposes in C programs.
• They are also known as Decision-Making Statements and are used to evaluate one or more
conditions and make the decision whether to execute a set of statements or not
• They're come situations in real life when we need to make some decisions and based on
these decisions, we decide what should we do next.
• Similar situations arise in programming also where we need to make some decisions and
based on these decisions we will execute the next block of code.
• For example
• In C if x occurs then execute y else execute z. There can also be multiple conditions like in C
if x occurs then execute p, else if condition y occurs execute q, else execute r. This condition
of C else-if is one of the many ways of importing multiple conditions.
if in C++
• The if statement is the simplest decision-making statement. It is used to decide whether a
certain statement or block of statements will be executed or not i.e if a certain condition is
true then a block of statements is executed otherwise not.
Syntax of if Statement
If (condition)
Explanation
Here, the condition after evaluation will be either true or false. C if statement accepts boolean values
– if the value is true then it will execute the block of statements below it otherwise not. If we do not
provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the first
immediately below statement to be inside its block.
Example
#include <stdio.h>
int main()
int i = 10;
if (i > 15) {
if-else in C
• The if statement alone tells us that if a condition is true it will execute a block of statements
and if the condition is false it won’t. But what if we want to do something else when the
condition is false
• Here comes the C else statement. We can use the else statement with the if statement to
execute a block of code when the condition is false. The if-else statement consists of two
blocks, one for false expression and one for true expression.
Syntax of if else in C
If (condition)
{
Statements to be executed if condition is true
Else
Examples
#include <iostream>
int main()
if (num > 0)
else
return 0;
Nested if-else in C
• A nested if in C is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement. Yes, C allow us to nested if
statements within if statements, i.e, we can place an if statement inside another if
statement
switch Statement in C
• The switch case statement is an alternative to the if else if ladder that can be used to
execute the conditional code based on the value of the variable specified in the switch
statement. The switch block consists of cases to be executed based on the value of the
switch variable
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Implement a program to determine whether a number is positive, negative, or zero using
an if-else statement.
#include <iostream>
int main()
if (num > 0)
else
return 0;
Loops in programming
C++ Loops
• In Programming, sometimes there is a need to perform some operation more than once or
(say) n number of times.
• Loops come into use when we need to repeatedly execute a block of statements.
• For example: Suppose we want to print “Hello World” 10 times. This can be done in two
ways as shown below:
Manually we have to write cout for the C++ statement 10 times. Let’s say you have to write it 20
times (it would surely take more time to write 20 statements) now imagine you have to write it 100
times, it would be really hectic to re-write the same statement again and again. So, here loops have
their role.
int main()
return 0;
• In Loop, the statement needs to be written only once and the loop will be executed 10 times
as shown below.
• The C++ Course includes comprehensive lessons on the different types of loops available
in C++, ensuring you can implement them effectively in your programs
1. Entry Controlled loops: In this type of loop, the test condition is tested before entering the
loop body. For Loop and While Loop is entry-controlled loops.
2. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the
end of the loop body. Therefore, the loop body will execute at least once, irrespective of
whether the test condition is true or false. the do-while loop is exit controlled loop.
• while loop– First checks the condition, then executes the body.
• for loop– firstly initializes, then, condition check, execute body, update.
//code to be executed
C++ for loop is same as C/C#. We can initialize variable, check condition and
increment/decrement value
Develop a program to display the first 10 multiples of any user-provided number using a for
loop
#include <iostream>
int main() {
int num;
cout << "The first 10 multiples of " << num << " are:" << endl;
return 0;
while (condition)
{
// code block to be executed
}
Example
In the example below the code in the loop will run , over and over again, as long as a variable (i)
is lexx thsan 5
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
syntax
• do {
// code block to be executed
}
while (condition)
Syntax
Example
Create a program where a const function protects class members from unintended changes
#include <iostream>
class Rectangle {
private:
int length;
int width;
public:
return length;
return width;
length = l;
width = w;
};
int main() {
std::cout << "Length: " << rect.getLength() << ", Width: " << rect.getWidth() << std::endl;
rect.setDimensions(6, 8);
std::cout << "Updated Length: " << rect.getLength() << ", Updated Width: " << rect.getWidth() <<
std::endl;
return 0;
Explanation
1. Private Members:
• length and width store the rectangle's dimensions and are not accessible directly from
outside the class.
2. Const Functions:
• getLength(), getWidth(), and getArea() are const functions. They provide read-only access to
the class members without modifying them.
3. Non-Const Function:
• setDimensions(int l, int w) allows controlled updates to the rectangle's dimensions.
4. Main Function:
• Only one copy of that member is created for the entire class and is shared by all the objects
of that class, no matter how many objects are created.
• It is initialized before any object of this class is created, even before the main starts outside
the class itself.
Syntax
className {
static data_type data_member_name;
....
}
Show function overloading by writing a program to add two integers and two floats.
#include <iostream>
return a + b;
return a + b;
int main() {
// Add two integers
return 0;
#include <iostream>
T sum(T a, U b) {
return (a+b);
int main() {
cout<<"sum :-"<<sum(5,3)<<endl;
cout<<"ave :-"<<sum(5,3)/2<<endl;
Inheritance in C++
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class
(called the derived class) to acquire properties and methods from another class (called the base
class). It promotes code reuse and hierarchical classification.
1. Single Inheritance:
class Base {
public:
void display() {
cout << "Base class display method" << endl;
};
};
Multiple Inheritance:
class Base1 {
public:
void display1() {
};
class Base2 {
public:
void display2() {
};
};
Multilevel Inheritance:
class Base {
public:
void display() {
cout << "Base class method" << endl;
};
};
};
Hierarchical Inheritance:
class Base {
public:
void display() {
};
};
};
Hybrid Inheritance:
Combines two or more types of inheritance, often leading to a diamond problem (resolved using
virtual inheritance).
class Base {
public:
void display() {
cout << "Base class method" << endl;
};
};
};
};
Syntax of Inheritance
class Base {
};
};
Public Inheritance:
Public members of the base class become public in the derived class.
Protected Inheritance:
Public and protected members of the base class become protected in the derived class.
Private Inheritance:
Public and protected members of the base class become private in the derived class.
class Derived : private Base {};