0% found this document useful (0 votes)
109 views5 pages

Incrementation/Decrementation: Types of Increment and Decrement Operator

The document discusses increment and decrement operators in Java. It explains: 1. There are two types of operators - ++ to increment a value by 1 and -- to decrement a value by 1. 2. There are pre- and post-increment/decrement operators that determine if the value is changed before or after use in an expression. 3. Loops allow repeating a set of instructions while a condition is true, reducing redundant code. A for loop example prints "JAVA" 5 times more concisely than listing each print statement.

Uploaded by

Night King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views5 pages

Incrementation/Decrementation: Types of Increment and Decrement Operator

The document discusses increment and decrement operators in Java. It explains: 1. There are two types of operators - ++ to increment a value by 1 and -- to decrement a value by 1. 2. There are pre- and post-increment/decrement operators that determine if the value is changed before or after use in an expression. 3. Loops allow repeating a set of instructions while a condition is true, reducing redundant code. A for loop example prints "JAVA" 5 times more concisely than listing each print statement.

Uploaded by

Night King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Incrementation/Decrementation

Incrementation and decrementation in Java programming let you easily add 1


or subtract 1 from a variable. To achieve this, we have two different types of
operators. Now you might be wondering which are those just like him?

++ Increment operator: increments a value by 1

- -- Decrement operator: decrements a value by 1

1.

Types of Increment and Decrement operator:


1) Pre-increment/ Pre-decrement operator.
2) Post-increment/post-decrement operator.
Pre-increment Operator:
1. “++” is written before Variable name.
2. Value is Incremented First and then incremented value is used in
expression.

Let us write a simple code for pre-incrementation

Example for Pre incrementation:


class Demo
{
public static void main(String[] args)
{
int a = 5;
System.out.println(a);
++a;
System.out.println(a);
}
}
output :
5
6
Post-increment Operator:
1. “++” is written after Variable name.
2. Value is used in expression first and then gets incremented.

Example for Post incrementation:


class Demo
{
public static void main(String[] args)
{
int a = 5;
System.out.println(a);
a++;
System.out.println(a);
}

Can you guess the output?

output:
5
6

Pre-decrement Operator:
1. “--” is written before Variable name.
2. Value is decremented First and then decremented value is used in
expression.

Example for Pre decrementation:


class Demo
{
public static void main(String[] args)
{
int a = 5;
System.out.println(a);
--a;
System.out.println(a);
}
}
output:
5
4

Post-decrement Operator:
3. “--” is written after Variable name.
4. Value is used in expression first and then gets decremented.

Example for Post decrementation:


class Demo
{
public static void main(String[] args)
{
int a = 5;
System.out.println(a);
a--;
System.out.println(a);
}

output:
5
4

Let us now learn Loops

LOOPS:
You might be now wondering why loops?

Looping in programming languages is a feature which facilitates the execution


of a set of instructions/functions repeatedly while some condition evaluates to
true.
Still confused?
In simple words if you are doing the same operation multiple times, you can
make use of loops.
Well how if you ask me, let us consider a simple for loop to understand.
Let us look at its flow chart:

Consider the example given below without using for loop


class Demo
{
public static void main(String[] args)
{
System.out.println(“JAVA”);
System.out.println(“JAVA”);
System.out.println(“JAVA”);
System.out.println(“JAVA”);
System.out.println(“JAVA”);
}
}
As you can in the above code you are printing JAVA 5 times that is doing the
same operation multiple times.
Output:
JAVA
JAVA
JAVA
JAVA
JAVA
Let us now make use of for-loop and write the code:
class Demo
{
public static void main(String[] args)
{
int i;
for(i=1; i<=5;i++)
{
System.out.println(“JAVA”);
}
}
}

You can now see the way we made use of for-loop and reduced the size of code.
Output:
JAVA
JAVA
JAVA
JAVA
JAVA

Both the codes gave us the same output but the code was written effectively
using for-loop.

Isn’t that great how loops reduce the size of code

You might also like