Home > Net >  Role of decrement operator in the following statement?
Role of decrement operator in the following statement?

Time:02-16

Going through a solution for a problem I'm trying to solve and I am not able to understand how the decrement operator works in the following statement.

if (--degree[j] == 0) bfs.add(j);

CodePudding user response:

Write some simple code to confirm what you think

int arr[] = {1};
    
if (-- arr[0] == 0) {
    System.out.println("true");
}

// and again
if (-- arr[0] != 0) {
    System.out.println("true");
}

output

true
true

CodePudding user response:

import java.util.Arrays;
import static java.lang.System.*;

public class Main {
    public static void main(String[] args) {
            boolean thrown = false;
            int[] arr = new int[]{1};

            --arr[0];
            out.println("true");

            if (-- arr[0] != 0) out.println("true");
    }
}

**Output **

true
true
  • Related