# Thread: breaking out of a loop problem

1. ## breaking out of a loop problem

So my homework is:

1. Your program will accept a sequence of positive integers (no greater than 2000000000, though your program does not need to check) until it sees an input of 100 for the second time. It will then print the number of odd numbers, even numbers and multiples of 3 entered (including 100s). Example follows:
Enter a sequence of integers (second 100 to quit)
25
36
100
20
98
200
3
86
100

Number of odd numbers is 2
Number of even numbers is 7
Number of multiples of 3 is 2

This is what I got but its not turning out the way I want to. The only part Im stuck on is how to break the loop after "the second 100" and for some reason my "mutiple of three count is always 0.

.................................................. ..
.................................................. ...
------------------------------------------
------------------------------------------

hey guys, thanks for all the suggestions. i managed to get the program to break out of the loop after the "second 100." Everything seems to work fine but now my even number(which includes the 100s) is incorrect by one(ie. 8 instead of 7)

Heres my new code.

Code:
```#include <stdio.h>

int main()
{
int num;
int oddcount = 0;
int evencount = 0;
int hundredcount = 0;
int multiplethreecount = 0;

printf("Enter a sequence of integers(second 100 to quit)\n");
scanf("%d", &num);
while (1) {
if (num == 100) {
hundredcount++;
if (hundredcount == 2)
break;
}
if (num%2 == 0) {
evencount++;
}
if (num%3 == 0)
multiplethreecount++;
if (num%2 != 0)
oddcount++;
scanf("%d", &num);
}

evencount += hundredcount;

printf("Number of odd numbers is %d \n", oddcount);
printf("Number of even numbers is %d \n", evencount);
printf("Number of multiples of 3 is %d \n", multiplethreecount);
return 0;
}```

2. You will want to move your num % 3 conditional up the list. Since you are checking if the number is even or odd first, it will never get there.

Also, you can use the keyword "break" to break out of a loop.

3. an odd no or an even no can be a multiple of three.

4. Originally Posted by Kudose
You will want to move your num % 3 conditional up the list. Since you are checking if the number is even or odd first, it will never get there.

Also, you can use the keyword "break" to break out of a loop.
If he does that, and the number is a multiple of 3, the code won't check if the number is even or odd

my suggestion is to change the else-if statements into if statements

you aren't updating the x_count inside your program, and it looks like the loop will only execute when the list begins with 100
remove the first while loop and place an if (num == 100) x_count++; inside the loop

5. Originally Posted by ಠ_ಠ
If he does that, and the number is a multiple of 3, the code won't check if the number is even or odd

my suggestion is to change the else-if statements into if statements

you aren't updating the x_count inside your program, and it looks like the loop will only execute when the list begins with 100
remove the first while loop and place an if (num == 100) x_count++; inside the loop

6. It seems that a do ... while loop would be appropriate:

Code:
```hundreds_count = 0;

do {
get input
logic
if (x == 100) ++hundreds_count;
} while (hundreds_count < 2);```
Or alternatively a do loop in a for loop:

Code:
```for (i = 0; i != 2; ++i) {
do {
get input
logic
} while (x != 100);
}```

7. I wonder why break is so often seen as such a terrible monster.
Sometimes it is handy, and allows you to avoid strange looking workarounds, which are actually way harder to interpret than using break. In this case, when we use break, things will look almost like pseudocode - no need to introduce loops inside loops etc. (Although anaon's do-while looks good too - but I perssonally am not used to do-while).

I would write
Code:
```while(1)   //repeat
{