0
for (i = 2; i < input; i++){
    for (d = 2; d < input; d++){
        product = d*i;
        printf("%d\n", product);
        break;
    }
}

This snippet of code is part of my solution to a homework question for my Intro to C class. The actual question is something involving prime numbers, but the solution that I thought of requires the use of for loops nested like this. I can't get them to behave correctly, though. I assume using an array would simplify things, but we haven't taken them up yet and are not allowed to use them in our solution. Anyway:

If, for example, my input is 10, my values of i should be {2,3,4,5,6,7,8,9}. My values for d should also be {2,3,4,5,6,7,8,9}.

What I expect this loop to do is multiply each i by the full loop of d:

2*2
2*3
2*4
.
.
.
2*9
3*2
3*3
.
.
.
.

and onwards. However, it is instead just multiplying i by d = 2, and I get the following:

4
6
8
10
.
.
.
20

What am I doing wrong?

2
  • 2
    that's because you break; the inner loop so it's only executed once... Commented Jun 28, 2013 at 14:42
  • 1
    Welcome to Stack Overflow. Please read the FAQ soon. I've improved the presentation of the data by removing the blank lines and then selecting the data lines and using the {} button above the edit box to indent the data by 4 spaces, which puts it into constant-width font and highlights it, as shown. Commented Jun 28, 2013 at 14:45

4 Answers 4

9
for (d = 2; d < input; d++){
        product = d*i;
        printf("%d\n", product);
        break;  // because of this line the loop breaks on first iteration.
}

Remove break; from your code and it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, everybody. Can't believe I missed such a stupid mistake. Sorry for the poor formatting!
4

Your break statement makes the inner loop run only once. So you only multiply i by d=2.

You probably want this:

for (i = 2; i < input; i++){
    for (d = 2; d < input; d++){
        product = d*i;
        printf("%d\n", product);
        // break; this statement is causing you to exit loop after 1 iteration
    }
}    

Comments

2

cut the break;, it's exiting your inner loop

essentially, the break statement turns the following for loop

for (d = 2; d < input; d++){

into an if statement

3 Comments

It's not really even an if statement, is it? It's converting it into two sequential statements.
@JonathanLeffler but what if input is less than or equal to 2?
Then the outer loop will have exited first...?
2

Remove the break; line. When your code gets here (on the first iteration), the inner loop (d=2...) will be skipped out of.

For statements aren't like switch statements where you need to end with break;. You use a break statement to exit the loop not continue to the next iteration (that's what continue is for).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.