-1

why is my code executing the body inside the for loop 3 times?

should it not only happen 2 times?

why 3 times?

when I run this I get:

"4 divided by 2 the remainder is

0

4 divided by 3 the remainder is

1

4 divided by 4 the remainder is

0

loop has exited out of for loop because D is now 5 count is now 2.

code inside if statement should've happend 2 times

value of N is 4"

public class forIf {

public static void main (String[] args) {

int D;
int N = 4;
int count;

count = 0;

for (D = 2; D <= N; D++) {
  if (N % D == 0)
    count++;
    System.out.println( N + " divided by " + D + " the remainder is");
    System.out.println( N % D );
}
System.out.println("loop has exited out of for loop because D is now " + D);
System.out.println("count is now " + count + ". code inside if statement should've happend " + count + " times");
System.out.println("value of N is "+ N);
}
}
// shouldn't the code inside the if statement only happen twice?
// because N % D is only true twice?
// why is it running that block 3 times?
  • 3
    Because Java is not Python, it pays no attention to indentation. Your `if` covers only `count++`, not the two prints, which are outside the `if`. Add curly braces to fix this problem. Voting to close as a typo. – Sergey Kalinichenko Sep 07 '18 at 14:59
  • 1
    To strengthen what dasblinkenlight says: *always* use optional curly braces (on for, if, while etc), to *avoid* the problem in the first place. – Andy Turner Sep 07 '18 at 15:05
  • Ah ok. So always use curly brackets to enclose even 1 line of code. But if there aren't any curly brackets, it would only run that one line? Ah ok I just saw the other duplicate question. Thanks again! – javastudent Sep 07 '18 at 15:13

3 Answers3

1

why is my code executing the body inside the for loop 3 times?

Here is what the loop variables are each time you loop

int N = 4;
for (D = 2; D <= N; D++) ...

Loop 1: D = 2 , 2 <= 4 is true  
Loop 2: D = 3 , 3 <= 4 is true  
Loop 3: D = 4 , 4 <= 4 is true  

This is why your loop happens three times.

As for this why the

code inside if statement should've happend 2 times

As others have also pointed out, the only code inside your if-block is count++; if you want to include the printlns then they must be inside braces of the if-block.

if (N % D == 0) { // <-- brace
    count++;
    System.out.println( N + " divided by " + D + " the remainder is");
    System.out.println( N % D );
} // <-- brace
xtratic
  • 4,600
  • 2
  • 14
  • 32
0

Your if has only one statement count++. The two print statements are not part of it. You need to enclose the block within {..}

for (D = 2; D <= N; D++) {
  if (N % D == 0) {
    count++;
    System.out.println( N + " divided by " + D + " the remainder is");
    System.out.println( N % D );
  }
}
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • wow for some reason i did that earlier and it still didn't work as I intended. But the extra 2 curly brackets fixed it. Thank you! – javastudent Sep 07 '18 at 15:06
0

When the contents of your if (and while and for and so on) are more than one line, you need to enclose them in brackets ({}). Actually, you should always do this, even when it's just one line, because doing so is easier to read and less bug-prone. Your if should look like this:

for (D = 2; D <= N; D++) {
  if (N % D == 0) {
    count++;
    System.out.println( N + " divided by " + D + " the remainder is");
    System.out.println( N % D );
  }
}
Tobb
  • 11,850
  • 6
  • 52
  • 77