• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java program dry run confusion

The Day Dreamer

Senior member
#Homework

void test (int n)
{
for(int i=1;i<=n);i++)
if(n%i==0)
System.out,println(i)

If an integer 24 is passed to n.

Can you show the dry working please. I am getting confused that integer i will become 1 again because of for condition. No idea if right or wrong. Please help. Thanks 🙂
 
Last edited:
That can't even compile and run, unless you mis-typed it. Also, use the code tags for better readability. Anyway, your 'for' expression is mis-formatted.
 
Actually the question is to give the output in the following functions definitions so its kinda dry run to get output.

Yes, for expression, corrected.
 
Your for expression still isn't correct, use code tags, and look up what the '%' operator means.


I really feel as if saying anything more than that is doing your homework for you.
 
Actually the question is to give the output in the following functions definitions so its kinda dry run to get output.
Ah, I get it, a "run it in your head" question.

I am getting confused that integer i will become 1 again because of for condition.
Let me grab you an example for loop from Wikipedia. (I used php tags just for coloring - it's not really "PHP Code".)

Here is an example of the traditional for loop in Java 7.

PHP:
for(int i = 0; i < 100; i++){
    //Prints the numbers 0 to 99, each separated by a space.
    System.out.print(i);
    System.out.print(' ');
}
System.out.println();

You see the format of the loop. You see what it says it does. ("Prints the numbers 0 to 99, each separated by a space.") Does that answer your question?
 
Be sure to put in the effort to understand this before moving on, don't just Google some text to paste into your homework.

You need to know the basics like how loops work. If you don't grasp the basics you'll be even more lost as you continue and will fail your later work.
 
EDIT: Oh, you just need to understand looping.

for (A;B;C)
A is the initial condition
B is the logical requirement to continue in the loop
C is the action to take when the activities in the for loop are complete. Typically an incrementer.

i will not reset. The loop simply stops repeating when the logical condition is false.

Code:
void test (int n)
{
    for(int i=1;i<=n);i++) {  // A;B;C
        if(n%i==0) {
            System.out.println(i)
        }
    }
}

can be thought of as this (in this case):

Code:
void test (int n)
{
    int i = 1;  // A
    while(i<=n) {  // B
        if(n%i==0) {
            System.out.println(i)
        }
        i++;  // C
    }
}
 
Last edited:
Back
Top