Literals, Punctuators, DMA and Looping in C++ (CPP).

In this tutorial we will discuss on literals, punctuators, DMA and looping in C++ (CPP).

Literals

Literals (often referred to as constants) are data items that never change their value during the execution of the program. The following types of literals are available in C++. 

  1. Integer Constants
  2. Character Constants
  3. Floating Constants
  4. Strings Constants


Punctuators

A punctuator is a token that has syntactic and semantic meaning to the compiler, but the exact significance depends on the context. A punctuator can also be a token that is used in the syntax of the preprocessor. Following are the punctuators available in C++:

When a list of declarations and a group of statements are enclosed within a pair of curly braces, it is known as statement block.

The syntax of a block is:
{     //Opening brace list of declarations
    statement1;
    statement2;
    ... ... ... ... ...
    statement n;
}     //Closing brace



When variables are declared inside a block, it is visible within that block only.

Namespace 

→ Namespace is a feature in C++ which helps us to define a named scope to overcome the difficulty encountered in handling the variables and functions that has same name but which corresponds to different scope/class/library.

→ The syntax for defining a namespace is:

namespace namespace_name
{
declarations;
}


→ Once the namespace has been defined, it can be used in the given program by using the following syntax:
        using namespace namespace_name;


Example 1:  Write a program is CPP to illustrate the concept of namespace.

#include<iostream>
 using namespace std;
namespace pulchowk

int roll; 
char name[20];

using namespace pulchowk;
int main()
{
cout<<"Enter the name and roll no"<<endl;
cin>>name>>roll; 
cout<<"The entered details are"<<endl; 
cout<<name<<endl<<roll; 
return 0;
}


It is to be noted that if the statements,

using namespace std; and

using namespace pulchowk;

were not written then the program would have taken following form with scope resolution operator used:

#include<iostream>
namespace pulchowk
{
int roll;
char name[20];
}

int main()
{
std::cout<<"Enter the name and roll no"<<std::endl;
std::cin>>pulchowk::name>>pulchowk::roll;
std::cout<<"The entered details are"<<std::endl;
std::cout<<pulchowk::name<<std::endl<<pulchowk::roll;
return 0;
}

User Defined Constant const
→ The syntax for defining a user defined constant is:
    const data_type constant_name=value; 
→ For example:
        const int a=5;
        const float PI=3.11414;
→ The above statements defines two constants with names a and PI respectively with value equal to 5 and 3.1414 respectively.
→ The keyword const can also be used to declare constant objects

Input/Output Streams and Manipulators

Streams: Flow of data
Input Stream: Flow of data from input device to CPU.
Output Stream: Flow to data from CPU to output device.
Manipulators: Manipulators in C++ are the objects or functions used for formatting output as per the user requirements. Examples: 
endl setw() 
setfill() 
setprecision()

Dynamic Memory Allocation with new and delete operators

Dynamic memory allocation can be achieved with the use of new and delete operator.

A. DMA for a single variable:

→ The syntax for dynamic memory allocation for a single variable is:
            data_type *pointer_variable; 
            pointer_variable=new data_type;
→ The above statements dynamically allocates the memory for a variable and assigns the address to the pointer which can be used to input and display the data.
→ Once the use of memory is no longer required, the allocated memory can be de-allocated using the delete operator using following syntax: delete pointer_variable;

Example: WAP in CPP to illustrate the concept of DMA for a single variable. #include<iostream>
using namespace std; int main() {
int *ptr;
ptr=new int;
*ptr=555; cout<<"The entered number is:"<<*ptr;
delete ptr;
return 0; }
Sample Input/Output: The entered number is:555

B. DMA for an array:

→ The syntax for dynamic memory allocation for an array is:
        data_type *pointer_variable;
        pointer_variable=new data_type[size];
→ The above statements dynamically allocates the memory for an array and assigns the starting address of the array to the pointer which can be used to access the members of the array.
→ Once the use of memory is no longer required, the allocated memory for array can be de-allocated using the delete operator using following syntax: 
        delete [] pointer_variable;

Example: WAP in CPP to illustrate the concept of DMA for an array.
WAP to calculate the sum and average of n integer numbers entered by the
user using DMA for array.
#include<iostream>
using namespace std; int main() { int n; float *ptr,sum=0,avg;
cout<<"Enter the no. of terms"<<endl;
cin>>n; ptr=new float[n];
cout<<"Start entering the values..."<<endl;
for(int i=0;i<n;i++)     { cin>>ptr[i];
       sum=sum+ptr[i]; } avg=sum/n;
cout<<"Sum="<<sum<<endl<<"Average="<<avg;
delete []ptr;
return 0;
}

A. Conditions:
Different conditions can be checked and appropriate action can be taken in CPP using the structures like simple if, if...else, else if ladder. 
Their syntax and flowchart are as follows:

1. Simple if: The syntax of simple if is as follows:- if(test expression) { statement-block; } statement x;

2.if...else

if...else The syntax of if else is as follows:- if(test expression) {     true-block statement(s); } else {     false-block statement(s); }

else if ladder: The syntax of else if ladder is as follows:- if(condition-1)else {{ statement-1;default-statement; }} else if(condition-2)statement-x; { statement-2; } else if(condition-3) { statement-3; } - - - - - - - - - - - - - - - - - - - - - - - - else if(condition-n) { statement-n; }

B. Looping:

Different looping structures that are available in CPP are for loop, while loop, do while loop. Their syntax and flowchart are as follows: for loop

→ The syntax for the for loop is given below:

for(initialization_expression;test_expression;update_expression)

{

body of loop; Entry 

}

while loop
→ The syntax for the while loop is given below:
while(test expression)
{
body of loop or statement(s);
}

→ It is to be noted that while loop is also known as an entry controlled loop.

do…while loop
→ The syntax for the while loop is given below:

do {
body of loop or statement(s);
} while(test expression);

→ It is to be noted that do while loop is also known as an exit controlled loop.


This is the end of  Literals, Punctuators, DMA and Looping in C++ (CPP).

Previous Post Next Post