Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Increment and Decrement | Loops
Java Basics

Increment and DecrementIncrement and Decrement

Increment

The increment operator, denoted by "++", is used to increase the value of a variable by 1. It is commonly used in loops to control the iteration process. There are two ways to use the increment operator:

  • Post-increment (i++): The variable's value is incremented after it is used in the expression. For example:
java

Main.java

  • Pre-increment (++i): The variable's value is incremented before it is used in the expression. For example:
java

Main.java

Decrement

The decrement operator, denoted by "--," is used to decrease the value of a variable by 1. It follows the same rules as the increment operator and can be used in a similar way.

Here's an example of using increment and decrement in a for loop:

java

Main.java

In the first "for" loop, the variable "i" is initialized to "0," incremented by "1" after each iteration, and the loop executes until "i" is no longer less than "5." This will output the numbers from 0 to 4.

In the second "for" loop, the variable "j" is initialized to "5," decremented by "1" after each iteration, and the loop executes until "j" is no longer greater than "0." This will output the numbers from 5 to 1 in descending order.

Note

The increment (++) and decrement (--) operators are useful for controlling the flow and counting in loops. They provide a convenient way to manipulate variables within the loop's execution.

Assignment Operators

Java also allows you to simplify expressions using assignment operators. In general, if increment increases the value of a variable by 1, and decrement decreases it by 1, then with assignment operators, we can customize any operation. For example,

x = x + 2 equals to x+=2

You can also do it with any type of operation, even with multiplication and division:

x = x * 4 equals to x*=4;

Let's take a look at a real example of using an assignment operation in a for loop:

java

main.java

You can see in the code above how we increment the variable i by 10 with each iteration as long as i < 50.

This way, we can shorten and combine different operations while immediately assigning the result to a variable. Very useful!

1. What will be the output of the following code snippet?
2. What will be the output of the following code snippet?

What will be the output of the following code snippet?

Select the correct answer

What will be the output of the following code snippet?

Select the correct answer

Everything was clear?

Section 3. Chapter 5
course content

Course Content

Java Basics

Increment and DecrementIncrement and Decrement

Increment

The increment operator, denoted by "++", is used to increase the value of a variable by 1. It is commonly used in loops to control the iteration process. There are two ways to use the increment operator:

  • Post-increment (i++): The variable's value is incremented after it is used in the expression. For example:
java

Main.java

  • Pre-increment (++i): The variable's value is incremented before it is used in the expression. For example:
java

Main.java

Decrement

The decrement operator, denoted by "--," is used to decrease the value of a variable by 1. It follows the same rules as the increment operator and can be used in a similar way.

Here's an example of using increment and decrement in a for loop:

java

Main.java

In the first "for" loop, the variable "i" is initialized to "0," incremented by "1" after each iteration, and the loop executes until "i" is no longer less than "5." This will output the numbers from 0 to 4.

In the second "for" loop, the variable "j" is initialized to "5," decremented by "1" after each iteration, and the loop executes until "j" is no longer greater than "0." This will output the numbers from 5 to 1 in descending order.

Note

The increment (++) and decrement (--) operators are useful for controlling the flow and counting in loops. They provide a convenient way to manipulate variables within the loop's execution.

Assignment Operators

Java also allows you to simplify expressions using assignment operators. In general, if increment increases the value of a variable by 1, and decrement decreases it by 1, then with assignment operators, we can customize any operation. For example,

x = x + 2 equals to x+=2

You can also do it with any type of operation, even with multiplication and division:

x = x * 4 equals to x*=4;

Let's take a look at a real example of using an assignment operation in a for loop:

java

main.java

You can see in the code above how we increment the variable i by 10 with each iteration as long as i < 50.

This way, we can shorten and combine different operations while immediately assigning the result to a variable. Very useful!

1. What will be the output of the following code snippet?
2. What will be the output of the following code snippet?

What will be the output of the following code snippet?

Select the correct answer

What will be the output of the following code snippet?

Select the correct answer

Everything was clear?

Section 3. Chapter 5
some-alt