Thread: Break doesn't work the way I expected it to

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    7

    Break doesn't work the way I expected it to

    Hi there,

    I wrote a program that should find all the Prime Numbers in an interval specified by the user.

    Here is the program that works:

    insert
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int isPrime;
    int high;
    int low;
    int i;
    
    
        printf("Enter the lower number: \n");
        scanf(" %d", &low);
        printf("Enter the higher number: \n");
        scanf(" %d", &high);
    
        for(low ; low < high ; ++low){
            isPrime=1;
    
            if (low <= 1) {
             ++low;
             continue;
          }
    
    
            for(i=2 ; i <= low /2 ; ++i ){
                if(low % i==0)
                isPrime=0;
            }
            if(isPrime==1)
            printf(" %d ", low);
    }
    
        return 0;
    }
    Here's the thing. I'm trying to add a Break following the second For in order to save redundecy but everytime I do, it stops working properly and I can't seem to figure why... :/

    Here the same prog just with the Break:

    insert
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int isPrime;
    int high;
    int low;
    int i;
    
    
        printf("Enter the lower number: \n");
        scanf(" %d", &low);
        printf("Enter the higher number: \n");
        scanf(" %d", &high);
    
        for(low ; low < high ; ++low){
            isPrime=1;
    
            if (low <= 1) {
             ++low;
             continue;
          }
    
    
            for(i=2 ; i <= low /2 ; ++i ){
                if(low % i==0)
                isPrime=0;
                break;
            }
            if(isPrime==1)
            printf(" %d ", low);
    }
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            for(i=2 ; i <= low /2 ; ++i ){
                if(low % i==0)   //!! what happened to these braces?
                isPrime=0;
                break;
            }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The break is not within the if statement, so your inner for loop always terminates at the end of the first iteration.

    You need to indent your code more consistently too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    THANK YOU!

    Went over it so many times but kept missing it...

    and I will definitely work on my indentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FAQ-Draft: Why scanf("%c", &c) doesn't work as expected
    By AndiPersti in forum C Programming
    Replies: 19
    Last Post: 10-09-2012, 03:14 AM
  2. Replies: 7
    Last Post: 04-23-2012, 07:52 AM
  3. Replies: 4
    Last Post: 04-05-2011, 11:55 AM
  4. Why this do-while loop doesn't work as I expected?
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 09:47 AM

Tags for this Thread