Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Incremento e Decremento | Loops
Noções Básicas de Java
course content

Conteúdo do Curso

Noções Básicas de Java

Noções Básicas de Java

1. Primeiros Passos
2. Tipos básicos, operações
3. Loops
4. Arrays
5. String

book
Incremento e Decremento

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:

Incremento

O operador de incremento, representado por "++", é utilizado para aumentar o valor de uma variável em 1. Ele é comumente usado em loops para controlar o processo de iteração. Existem duas formas de usar o operador de incremento:

java

Main

copy
123
int i = 0; System.out.println(i++); // Output: 0 System.out.println(i); // Output: 1

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

java

Main

copy
123
int i = 0; System.out.println(++i); // Output: 1 System.out.println(i); // Output: 1

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.

Decremento

O operador de decremento, representado por "--", é usado para diminuir o valor de uma variável em 1. Segue as mesmas regras que o operador de incremento e pode ser utilizado de maneira semelhante.

java

Main

copy
1234567891011121314
package com.example; public class Main { public static void main(String[] args) { System.out.println("Increment operation"); for (int i = 0; i < 5; i++) { System.out.println("Iteration " + i); } System.out.println("Decrement operation"); for (int j = 5; j > 0; j--) { System.out.println("Countdown " + j); } } }

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.

Nota

Os operadores de incremento (++) e decremento (--) são úteis para controlar o fluxo e a contagem em loops. Eles oferecem uma maneira conveniente de manipular variáveis durante a execução do loop.

java

main

copy
123456789
package com.example; public class Main { public static void main(String[] args) { for (int i = 0; i < 50; i+=10) { System.out.println("The current value of `i` is " + i); } } }

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?

What will be the output of the following code snippet?

Selecione a resposta correta

What will be the output of the following code snippet?

What will be the output of the following code snippet?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 6
We're sorry to hear that something went wrong. What happened?
some-alt