Does break not work on loops anymore?

This is a discussion on Does break not work on loops anymore? within the C++ Programming forums, part of the General Programming Boards category; Hi, I was trying to solve a Project Euler question. find the largest palindrome from the product of two 3 ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Does break not work on loops anymore?

    Hi,
    I was trying to solve a Project Euler question. find the largest palindrome from the product of two 3 digit number.

    the idea was if i start from the largest i and j, and if it was a palindrome it would be the largest. And I would break from the loop. But it wouldn't work...

    I had to use a boolean...so the question is? Does break not work ? It was in an if statement, but since the if was in a loop. It should still break?

    I'm baffled here.
    Is it ok if I post the code?
    You ended that sentence with a preposition...Bastard!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Sure you can post the code. (Obviously the broken code.)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    You would need to post a specific example.

    break only works for one level, so if you've got some nested loop or something, it will only break one level.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Salem View Post
    break only works for one level, so if you've got some nested loop or something, it will only break one level.
    Oh yes, that's why...
    if i used a function, would it break from the function or still that one level?
    You ended that sentence with a preposition...Bastard!

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Code:
    #include <iostream>
    using namespace std; 
    int convertToString(long int n) ;
    int checkPalindrome(string s);
    void main()
    {
        int i; 
        int j,large =0 ;
        int num1=0, num2=0 ;
        for(i=999;i>99;i--)
        {
            for(j=999;j>99;j--)
            {
                
               if(!convertToString(i*j))
               {
                 
                    num1 = i ; 
                    num2 =j ; 
                    large = i* j ;
                    break; 
               }
            }
           
        }
            
            
       cout << num1 << " " << num2 << " " << large  ;
    
    }
    int convertToString(long int n) 
    {
        
       char d; 
        string s;
        
        while(n>0)
        {
            d =  ((n % 10) + 48) ;
            s = s + d ;
           
            n/=10 ;
        }
     
    
        return checkPalindrome(s) ;
    }
    int checkPalindrome(string s)
    {
        int l = s.length()-1 ;
    
        int j = l ;
        
        for(int i=0;i< j;i++)
        {
            if(s[i]!=s[j])
            {   
               return 1;
            }
            j-- ;
    
        }
        
            return 0 ;
    }
    You ended that sentence with a preposition...Bastard!

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You'll need to add a bool keep_going or something that each loop level checks. Or in this case, a <whisper>goto</whisper> might be prudent.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    for(i=999;i>99;i--)
    {
        for(j=999;j>99;j--)
        {
                
           if(!convertToString(i*j))
           {
               
                num1 = i ; 
                num2 =j ; 
                large = i* j ;
                break; 
           }
        }
         
    }
    Haven't looked at anything other than the break issue, but if you wish to break from both those loops you could probably either:
    1. Set i to anything 99 or less immediately prior to calling break
    2. Set i and j both to something 99 or less at the end of the if block and not call break at all
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    It seems to work with the boolean. I will try hk_mp5 suggestion.
    Is there anyway to optimize it further? like if i use register int or something.
    Code:
    #include <iostream>
    using namespace std; 
    int convertToString(long int n) ;
    int checkPalindrome(string s);
    void main()
    {
        int i = 999; 
        int j=999,large =0 ;
        int num1=0, num2=0 ;
     
        while(i>99)
        {
            while(j>i)
            {
              
               if(!convertToString(i*j))
               {
                    num1 = i ; 
                    num2 =j ; 
                  
                    large = i* j ; 
                    
               }
                if(large>i*j)
                    break ; 
              
                j--;
            }  
            j= 999;
            i--;
            
        }
        
       cout << num1 << " " << num2 << " " << large  ;
    
    }
    int convertToString(long int n) 
    {
        
       char d; 
        string s;
        
        while(n>0)
        {
            d =  ((n % 10) + 48) ;
            s = s + d ;
           
            n/=10 ;
        }
     
    
        return checkPalindrome(s) ;
    }
    int checkPalindrome(string s)
    {
        int l = s.length()-1 ;
    
        int j = l ;
        
        for(int i=0;i< j;i++)
        {
            if(s[i]!=s[j])
            {   
               return 1;
            }
            j-- ;
    
        }
        
            return 0 ;
    }
    You ended that sentence with a preposition...Bastard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 01:11 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21