Function for Java

Asked

Viewed 275 times

1

I have the following code:

public class Programa{
    public static void main(String args[]){
        int a = 10;
        for (int i = 1; i<=2; i++){
            for(int j=0; j<=2; j++){
                if((i%2 == 0) && (j % 2 == 0)) {
                    a = a * 2;
                } else {
                    a = a + 1;
                }
            }
        }
        System.out.println("O valor de a é " + a);
    }
}

Everything in it I can understand perfectly, except the part of the for. Testing the program it results in 54, but how and why? I’m having a hard time playing him.

  • 1

    What exactly don’t you understand? You know what the operator % does? You know how the for?

  • So I know that the operator % gets the rest of the division. I know that in the example of i, it starts in 1, ends in 2 and adds 1 for each time it adds up. However I cannot understand how it arrived in 54 :/

  • Have you tried a table test of the algorithm? It may be useful also, to understand the sequence of the execution of the program, to print the variable a within if and else when executing the code.

  • i printed the variable '-' gave 54 rsrs to trying to understand how it arrived at this value...

  • Print inside the if and else! Will help you.

1 answer

4


Run the code below and see if it helps you understand :)

public class Programa {
    public static void main(String args[]){
        int a = 10;
        for (int i = 1; i<=2; i++){
            for(int j=0; j<=2; j++){
                System.out.println("\nCom i = " + i + " e j = " + j + ", temos: ");
                System.out.println("O valor de a, antes do IF/ELSE, é " + a); 
                if((i%2 == 0) && (j % 2 == 0)) {
                    a = a * 2;
                    System.out.println("O valor agora depois do IF é " + a);
                } else {
                    a = a + 1;
                    System.out.println("O valor agora depois do ELSE é " + a);
                }
            }
        }
        System.out.println("O valor de a ao final é " + a);
    }
}

Exit from the program:

Com i = 1 e j = 0, temos: 
O valor de a, antes do IF/ELSE, é 10
O valor agora depois do ELSE é 11

Com i = 1 e j = 1, temos: 
O valor de a, antes do IF/ELSE, é 11
O valor agora depois do ELSE é 12

Com i = 1 e j = 2, temos: 
O valor de a, antes do IF/ELSE, é 12
O valor agora depois do ELSE é 13

Com i = 2 e j = 0, temos: 
O valor de a, antes do IF/ELSE, é 13
O valor agora depois do IF é 26

Com i = 2 e j = 1, temos: 
O valor de a, antes do IF/ELSE, é 26
O valor agora depois do ELSE é 27

Com i = 2 e j = 2, temos: 
O valor de a, antes do IF/ELSE, é 27
O valor agora depois do IF é 54

O valor de a ao final é 54
  • I just did that. When the i arrives in 2 and the j also it makes a 'new Verificao' to see if the two are in 2? is the only explanation for it add up from 1 in 1 until 13..

  • When i and j are equal to 2, it executes the code within Else, as 2%2 is equal to 0. Before executing the code within Else, the a was value 27. After executing Else, the value of a turns 54 :).

  • @Math, I ended up removing my comment with my error before seeing your :P. Thanks for the fix!

  • But I thought as follows: a begins at 10, i at 1, and j at 0. The program updates i to 2, j to 1 and adds 1 to a. Then it keeps i(2), adds 1 more to j(2) and adds 1 to a(12) then it multiplies a by 2 because i and j are 2, thus remaining 24. Wouldn’t that be the answer??

  • @William, I’ll edit again, see if it helps to understand the sequence!

  • 3

    I think it’s worth pointing out that the for from inside the whole wheel, then starts again when the for from outside increment 1, from what I understand the AP thought the two were incremented simultaneously at each iteration.

Show 1 more comment

Browser other questions tagged

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