Advertisement
// C# pre-increment operation
using System;
class A
{
public static void Main()
{
//Local variables
char ch = 'b';
byte b = 20;
short sh = 99;
int i = 16;
long l = 99999;
float f = 20.5f;
double d = 15.5;
Console.WriteLine("Original character " + ch);
Console.WriteLine("Incremented character " + ++ch);
Console.WriteLine("Original byte " + b);
Console.WriteLine("Incremented byte " + ++b);
Console.WriteLine("Original short " + sh);
Console.WriteLine("Incremented short " + ++sh);
Console.WriteLine("Original int " + i);
Console.WriteLine("Incremented int " + ++i);
Console.WriteLine("Original long " + l);
Console.WriteLine("Incremented long " + ++l);
Console.WriteLine("Original float " + f);
Console.WriteLine("Incremented float " + ++f);
Console.WriteLine("Original double " + d);
Console.WriteLine("Incremented double " + ++d);
}
}
Original character a
Incremented character b
Original byte 10
Incremented byte 11
Original short 100
Incremented short 101
Original int 10
Incremented int 11
Original long 99999
Incremented long 100000
Original float 20.5
Incremented float 21.5
Original double 15.5
Incremented double 16.5
// C# post-increment operation
using System;
class A
{
public static void Main()
{
//Local variables
char ch = 'b';
byte b = 20;
short sh = 99;
int i = 16;
long l = 99999;
float f = 20.5f;
double d = 15.5;
Console.WriteLine("Original character " + ch);
Console.WriteLine("Incremented character " + ch++);
Console.WriteLine("Original byte " + b);
Console.WriteLine("Incremented byte " + b++);
Console.WriteLine("Original short " + sh);
Console.WriteLine("Incremented short " + sh++);
Console.WriteLine("Original int " + i);
Console.WriteLine("Incremented int " + i++);
Console.WriteLine("Original long " + l);
Console.WriteLine("Incremented long " + l++);
Console.WriteLine("Original float " + f);
Console.WriteLine("Incremented float " + f++);
Console.WriteLine("Original double " + d);
Console.WriteLine("Incremented double " + d++);
}
}
Original character a
Incremented character a
Original byte 10
Incremented byte 10
Original short 100
Incremented short 100
Original int 10
Incremented int 10
Original long 99999
Incremented long 99999
Original float 20.5
Incremented float 20.5
Original double 15.5
Incremented double 15.5
Advertisement
//C# Example of correctly displaying the incremented value after a post-increment operation
using System;
class A
{
public static void Main()
{
//Local variables
char ch = 'b';
byte b = 20;
short sh = 99;
int i = 16;
long l = 99999;
float f = 20.5f;
double d = 15.5;
Console.WriteLine("Original character " + ch);
ch++;
Console.WriteLine("Incremented character " + ch);
Console.WriteLine("Original byte " + b);
b++;
Console.WriteLine("Incremented byte " + b);
Console.WriteLine("Original short " + sh);
sh++;
Console.WriteLine("Incremented short " + sh);
Console.WriteLine("Original int " + i);
i++;
Console.WriteLine("Incremented int " + i);
Console.WriteLine("Original long " + l);
l++;
Console.WriteLine("Incremented long " + l);
Console.WriteLine("Original float " + f);
f++;
Console.WriteLine("Incremented float " + f);
Console.WriteLine("Original double " + d);
d++;
Console.WriteLine("Incremented double " + d);
}
}
Original character b
Incremented character c
Original byte 20
Incremented byte 21
Original short 99
Incremented short 100
Original int 16
Incremented int 17
Original long 99999
Incremented long 100000
Original float 20.5
Incremented float 21.5
Original double 15.5
Incremented double 16.5
//C# Example of Post and pre-increment operators in arithmetic expressions.
using System;
class A
{
public static void Main()
{
//Local variables
int a = 10, b=10, c=10, d=10;
//Printing the values of local variables
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
Console.WriteLine("c = " + c);
Console.WriteLine("d = " + d);
//first expression
int result = a++ - 10;
Console.WriteLine("Result of a++ - 10 = "+ result);
Console.WriteLine("The incremented value of a = "+ a);
//second expression
result = ++b - 10;
Console.WriteLine("Result of ++b - 10 = "+ result);
Console.WriteLine("The incremented value of b = "+ b);
//third expression
result = ++c - 10 + c++;
Console.WriteLine("Result of ++c - 10 + c++ = "+ result);
Console.WriteLine("The incremented value of c = "+ c);
//fourth expression
result = d++ + ++d +10;
Console.WriteLine("Result4 of d++ + ++d +10 = "+ result);
Console.WriteLine("The incremented value of d = "+ d);
}
}
Output-
a = 10
b = 10
c = 10
d = 10
Result of a++ - 10 = 0
The incremented value of a = 11
Result of ++b - 10 = 1
The incremented value of b = 11
Result of ++c - 10 + c++ = 12
The incremented value of c = 12
Result4 of d++ + ++d +10 = 32
The incremented value of d = 12
a++ - 10
++b - 10
++c - 10 + c++
d++ + ++d +10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement