Can I place two conditions/increments within the same loop?

Asked

Viewed 300 times

3

I want to basically do this

for (j = parametro, int k=0; j < parametro + 3, k<3; j++, k++) {
    previsoes[i] += valores[j] * pesos[k] ;
}
  • 1

    You ruined my little joke, brother =/

  • Anyway, I hope my answer has helped you

  • It helped a lot, thank you!

1 answer

10


Can.

Note that after the first statement you do not need to specify the type. The increment is correct. I took advantage and added an example of how to put "two conditions".

Thus:

class Main {
    public static void main(String[] args) {
        for (int i = 0, k = 0; i < 10 && k < 3; i++, k++) {
            System.out.println(i);
            System.out.println(k);
        }
    }
}

See working on Repl.it

  • 1

    Good! + 1! Just one detail: it’s not that it doesn’t accurate specify the type of the second variable. It can’t specify the type of the second variable (even if it is the same type as the first). It is as if you declare two attributes of the class separated with comma. This only works if the attributes are of the same type.

  • 1

    @Igorventurelli Yes, it’s because you can only add one statement one-line. I’ll make this clearer. I appreciate the feedback

Browser other questions tagged

You are not signed in. Login or sign up in order to post.