1

How do you break out of a loop without a break statement? My professor HATES break statements and tells us not to use it. I'm just curious how would I break out of the while-loop if the number I got WAS NOT a prime number?

Here's my code:

#include <stdio.h>
#include <stdlib.h>

/* Prototypes */
void primeChecker(int num1);

int main() {
    int num1 = 5;

    primeChecker(num1);

    return 0;
}

void primeChecker(int num1) {
    int i, flag = 0;

    printf("Enter a number to check for prime numbers: ");
    scanf("%d", &num1);

    /* Number to start with */
    i = 2;

    while (i <= num1/2) {
        if (num1 % i == 0) {
            flag = 1;
        } else {
            i++;
        }
    }

    if (flag == 0) {
        printf("The number is a prime number!");
    } else {
        printf("The number is NOT a prime number!");
    }
}
Haris
  • 12,120
  • 6
  • 43
  • 70
Anon
  • 33
  • 2
  • 4
  • 3
    what the hell is wrong with a `break` statement? Going to have to disagree with the prof on that one. I use them and to a lesser degree `continue` statements all the time... can somebody tell me why I'm the idiot now? – yano Apr 08 '16 at 05:23
  • 4
    For simple loops (like yours) you can change the condition some way. But you also should know that unless the teacher *forces* you to not use `break` or `continue`, then you are free to use those statements, they do exist for a reason and not using them when it makes sense is stupid. Coding style is a very personal thing, and unless you have a specific guideline you *must* follow then you can and should use (and develop) your own style, something you are comfortable with. Heck, I've been programming for 30 years and my style is still evolving (albeit slowly these days). :) – Some programmer dude Apr 08 '16 at 05:25
  • 1
    And if the teacher gives you lower grades just because you use a style he or she doesn't like, then he or she is a moron and needs to be reported to his or her superiors. Personal disagreement should never be used for grading. Of course, if the teacher makes it a part of the assignment to not use some specific statements, then that's another thing. – Some programmer dude Apr 08 '16 at 05:27
  • I will add, (and maybe this is what the prof is getting at), `break` statements only break out of the CURRENT loop, so if you're in a double-or-more loop and you break, just know you'll still be in the outer loops... (which is true if you only falsify the condition for the current loop anyway). If you want to break out of all loops at a particular spot, then AFAIK, you must falsify the condition for all loops at that spot, or simply `return` if you're done with the function – yano Apr 08 '16 at 05:31
  • 2
    Your professor might not allow `break` statements in assignments for didactic reasons, though, for example when it hasn't yet been covered in the lessons and the students hand in code copied from the internet. So your professer probably doesn't _hate_ `break` statements, he just doesn't want you to use them. – M Oehm Apr 08 '16 at 05:38
  • Tried to edit my comment and it was too late ... Be wary that if you falsify the conditions of the outer loops in an inner loop and then `break` (however you choose to `break`), the remaining code from the end of the inner loop to the end of the outer loop will still execute. So that's not a "clean `break`" method – yano Apr 08 '16 at 05:39
  • You need to upgrade your professor. If it has tenure, have it assassinated. – Martin James Apr 08 '16 at 09:27

3 Answers3

2

Or

int prime = 1;
while (i <= num1/2 && prime) {
    if (num1 % i == 0){
       prime = 0;
    } else {
       i++;
    }
}

if(prime){
    printf("The number is a prime number!");
}else{
    printf("The number is not prime.");
}

I mean, you almost had it:

while (i <= num1/2 && !flag){

would have done the trick as well

BLE
  • 161
  • 7
  • 1
    And you should probably use a boolean instead of an int for prime or flag, but int works just as well, and I'm not as fluent in C as I once was. I like the code to tell the story - less comments necessary that way. I don't like break statements either, but agree with the comments that you should be able to use them where they make sense. I'm sure you're professor would just like you to think of different ways to do the same task. It's all good fun. – BLE Apr 08 '16 at 05:40
1

You can do

while (i <= num1/2) {
    if (num1 % i == 0) {
        i = num1;
    } else {
        i++;
    }
}

This makes i larger then num1/2 and the while loop exits.

You probably need some more changes to make this work.

Haris
  • 12,120
  • 6
  • 43
  • 70
1

In your case, you can use the value of flag as condition:

while (flag == 0 && i <= num1/2) {
    if (num1 % i == 0) {
        flag = 1;
    } else {
        i++;
    }
}

But that looks like Pascal rather than C. A better solution might be to refactor the loop so that it is in a separate function:

int is_prime(int num1)
{
    int i = 2;

    while (i <= num1/2) {
        if (num1 % i == 0) return 0;
        i++;
   }

   return 1;
}

This makes the code simpler and separates the input stuff in primeChecker from the actual prime checking.

M Oehm
  • 28,726
  • 3
  • 31
  • 42