Site icon T4Tutorials.com

Prefix and postfix increment and decrement in C++ (C Plus Pls)

Prefix and postfix increment and decrement in C++ (C Plus Pls)

In this latest tutorial, we will try to cover the following topics;

  1. Prefix increment and post fix  increment in C++ (C Plus Pls)
  2. Prefix decrement and post fix  decrement in C++ (C Plus Pls)

Prefix increment and post fix  increment in C++ (C Plus Pls)

// Prefix increment and post fix  increment in C++ (C Plus Pls)
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d;
a=b=0;
d=c=0;
a=b++; //post fix
c=++d;  //pree fix
cout<<"a="<<a<<endl<<"b="<<b<<endl;
cout<<"c="<<c<<endl<<"d="<<d<<endl;
}

Output

Figure: postfix prefix increment in C++ (C Plus Plus)

Prefix deccrement and post fix  decrement in C++ (C Plus Pls)

// Prefix deccrement and post fix  decrement in C++ (C Plus Pls)
#include <iostream>
using 
d=c=0;namespace std;
int main()
{
int a,b,c,d;
a=b=0;
a=b--;  //post fix decrement
c= --d;  //pree fix decrement
cout<<"a="<<a<<endl<<"b="<<b<<endl;
cout<<"c="<<c<<endl<<"d="<<d<<endl;
}

Output

Figure: postfix prefix decrement in C++ (C Plus Plus)

Prefix Increment and Postfix Increment in C++ using If else Statement

Lets begin with “Prefix Increment and Postfix Increment program in C++ using If else Statement”

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;

a=b=0;
c=d=0;

if(a==0 && c==0)
{
a=b;
b++;

d++;
c=d;
}

cout<<"a = "<<a<<endl<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl<<"d = "<<d<<endl;

system("pause");
}

Prefix Increment and Postfix Increment in C++ using for loop

Lets begin with “Prefix Increment and Postfix Increment program in C++ using for loop”.

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;

a=b=0;
c=d=0;

for(int i=c; i<1; i++)
{
a=b++;
c=++d;
}
cout<<"a = "<<a<<endl<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl<<"d = "<<d<<endl;
system("pause");
}

Prefix Increment and Postfix Increment in C++ using While loop

Lets begin with “Prefix Increment and Postfix Increment program in C++ using While loop”.

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;

a=b=0;
c=d=0;
int i=c;
while(i<1)
{
a=b++;
c=++d;
i++;
}
cout<<"a = "<<a<<endl<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl<<"d = "<<d<<endl;
system("pause");
}

Prefix Increment and Postfix Increment in C++ using Do While loop

Lets begin with “Prefix Increment and Postfix Increment program in C++ using Do While loop”.

#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;

a=b=0;
c=d=0;
int i=c;
do
{
a=b++;
c=++d;
i++;
}
while(i<1);
cout<<"a = "<<a<<endl<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl<<"d = "<<d<<endl;
system("pause");
}

Prefix Increment and Postfix Increment Program in C++ using the User Define Function

Lets begin with “Prefix Increment and Postfix Increment program in C++ using the user define function”.

#include<iostream>
using namespace std;
int fixes(int&,int&,int&,int&);
int main()
{
int a,b,c,d;

a=b=0;
c=d=0;

fixes(a,b,c,d);
cout<<"a = "<<a<<endl<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl<<"d = "<<d<<endl;
system("pause");
}
int fixes(int &j, int &k, int &l, int &m)
{
j=k++;
l=++m;
}