2. Syllabus
Program Control Statements
Sequential Constructs
Decision Making Construct
Iteration/Loop Construct
Arrays
Functions(User defined Functions, Inline Function, Function overloading)
User Defined Data Types(Structure, Union and Enumeration)
3. Program Control Statements
The conditional statements (also known as decision control structures)
such as if, if else, switch, etc. are used for decision-making purposes in
C/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.
These decision-making statements in programming languages decide the
direction of the flow of program execution.
4. Following are the decision-making
statements available in C or C++:
if Statement
if-else Statement
Nested if Statement
if-else-if Ladder
switch Statement
Conditional Operator
Jump Statements:
break
continue
goto
return
5. If-Else
If-Else statements allow the program to execute different blocks of code
depending on conditionals. All If statements have the following form:
if ( condition ) {
//body
}
6. For Loop
A for loop allows for a block of code to be executed until a conditional
becomes false. For loops are usually used when a block of code needs to
executed a fixed number of times.
Each loop consists of 3 parts, an initialization step, the conditional, and an
iteration step.
The initialization is run before entering the loop, the condition is checked at
the beginning of each run through the loop ( including the first run ), and the
iteration step executes at the end of each pass through the loop, but before
the condition is rechecked.
for( initialization ; conditional ; iteration ) {
// loop body
}
7. While Loop
A while loop is a simple loop that will run the same code over and over as
long as a given conditional is true. The condition is checked at the
beginning of each run through the loop ( including the first one ). If the
conditional is false for the beginning, the while loop will be skipped all
together.
while ( conditional ) {
// loop body
}
8. Do-while Loop
A do-while loop acts just like a while loop, except the condition is checked
at the end of each pass through the loop body. This means a do-while
loop will execute at least once.
do {
// loop body
} while ( condition );
9. Break
Break is a useful keyword that allows the program to exit a loop or switch
statement before the expected end of a that code block. This is useful in
error checking or if the outcome of a loop is not certain. For example, the
following code will break out of the for loop if a user asks to leave.
string inputs[10];
string input;
for (int i = 0; i < 10; i++ ) {
cin >> input;
11. Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type, specify the name of the array followed
by square brackets and specify the number of elements it should store.
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
12. Loop Through an Array
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "n";
}
return 0;
}
13. User defined Functions
#include <iostream>
using namespace std;
// Function declaration
void myFunction();
// The main method
int main() {
14. Cont…..
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
cout << "I just got executed!";
}
15. Inline Function
C++ provides inline functions to reduce the function call overhead.
An inline function is a function that is expanded in line when it is called.
When the inline function is called whole code of the inline function gets inserted or
substituted at the point of the inline function call.
This substitution is performed by the C++ compiler at compile time.
An inline function may increase efficiency if it is small.
Syntax:
inline return-type function-name(parameters)
{
// function code
}
16.
17. Inline functions Advantages:
Function call overhead doesn’t occur.
It also saves the overhead of push/pop variables on the stack when a
function is called.
It also saves the overhead of a return call from a function.
When you inline a function, you may enable the compiler to perform
context-specific optimization on the body of the function. Such
optimizations are not possible for normal function calls. Other
optimizations can be obtained by considering the flows of the calling
context and the called context.
An inline function may be useful (if it is small) for embedded systems
because inline can yield less code than the function called preamble and
return.
18. Example
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "n";
return 0;
}
19. Function overloading
With function overloading, multiple functions can have the same name with
different parameters.
Example
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)
#include <iostream>
using namespace std;
int plusFuncInt(int x, int y) {
return x + y;
}
21. Structure
Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
Create a Structure
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
22. Access Structure Members
#include <iostream>
#include <string>
using namespace std;
int main() {
struct {
int myNum;
string myString;
} myStructure;
24. Advantages of Structure
Structure stores more than one data type of the same object together.
It is helpful when you want to store data of different or similar data types
such as name, address, phone, etc., of the same object.
It makes it very easy to maintain the entire record as we represent
complete records using a single name.
The structure allows passing a whole set of records to any function with
the help of a single parameter.
An array of structures can also be created to store multiple data of similar
data types.
25. Disadvantages of Structure
If the complexity of the project goes increases, it becomes hard to manage
all your data members.
Making changes to one data structure in a program makes it necessary to
change at several other places. So it becomes difficult to track all changes.
The structure requires more storage space as it allocates memory to all the
data members, and even it is slower.
The structure takes more storage space as it gives memory to all the
different data members, whereas union takes only the memory size
required by the largest data size parameters, and the same memory is
shared with other data members.
26. C++ program to demonstrate the
making of structure
#include <iostream>
using namespace std;
// Define structure
struct GFG {
int G1;
char G2;
float G3;
};
27. Cont….
// Driver Code
int main()
{
// Declaring a Structure
struct GFG Geek;
Geek.G1 = 85;
Geek.G2 = 'G';
Geek.G3 = 989.45;
cout << "The value is : "
28. Cont….
<< Geek.G1 << endl;
cout << "The value is : "
<< Geek.G2 << endl;
cout << "The value is : "
<< Geek.G3 << endl;
return 0;
}
29. Union
In "c++," programming union is a user-defined data type that is used to
store the different data type's values.
However, in the union, one member will occupy the memory at once.
In other words, we can say that the size of the union is equal to the size of
its largest data member size.
Union offers an effective way to use the same memory location several
times by each data member.
30. Cont….
#include <iostream>
using namespace std;
// Defining a Union
union GFG {
int Geek1;
char Geek2;
float Geek3;
};
31. Cont…..
int main()
{
Union GFG G1;
G1.Geek1=34;
cout<<“The first value at”<<“the allocated memory “<<G1.Geek1<<endl;
G1.Geek2 = 34;
32. Cont….
cout << "The next value stored "
<< "after removing the "
<< "previous value : " << G1.Geek2 << endl;
G1.Geek3 = 34.34;
cout << "The Final value value "
<< "at the same allocated "
<< "memory space : " << G1.Geek3 << endl;
return 0;
}
33. Advantages of Union
Union takes less memory space as compared to the structure.
Only the largest size data member can be directly accessed while using a
union.
It is used when you want to use less (same) memory for different data
members.
It allocates memory size to all its data members to the size of its largest
data member.
34. Disadvantages of Union
It allows access to only one data member at a time.
Union allocates one single common memory space to all its data
members, which are shared between all of them.
Not all the union data members are initialized, and they are used by
interchanging values at a time.
35. Enumeration
Enumeration (Enumerated type) is a user-defined data type that can be
assigned some limited values.
These values are defined by the programmer at the time of declaring the
enumerated type.
If we assign a float value to a character value, then the compiler generates
an error.
In the same way, if we try to assign any other value to the enumerated data
types, the compiler generates an error.
Enumerator types of values are also known as enumerators. It is also
assigned by zero the same as the array. It can also be used with switch
statements.
36. Cont….
Syntax:
enum enumerated-type-name
{
value1, value2, value3…..valueN
};
An enumeration is a user-defined data type that consists of integral constants.
To define an enumeration, keyword enum is used.
enum season { spring, summer, autumn, winter };
Here, the name of the enumeration is season.
And, spring, summer and winter are values of type season.
37. Example
#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday };
int main()
{
week today;