# Thread: Does break not work on loops anymore?

1. ## 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?

2. Sure you can post the code. (Obviously the broken code.)

3. 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.

4. Originally Posted by Salem
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?

5. 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 ;
}

6. 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.

7. 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

8. 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 ;
}