Topic: Multiple break for two or more level inner loops


Author: Rupesh Yadav <7yadavrupesh@gmail.com>
Date: Sun, 4 Jan 2015 08:41:47 -0800 (PST)
Raw View
------=_Part_2449_974857735.1420389707974
Content-Type: multipart/alternative;
 boundary="----=_Part_2450_1281209182.1420389707975"

------=_Part_2450_1281209182.1420389707975
Content-Type: text/plain; charset=UTF-8

Simple case: if *condition* is satisfied i want to break from all loops.

Example :

for(M){
    for(N){
        for(K){
            if(condition is true){
                flag = 1;
                break;
            }
        }
        if(flag==1) //  worst case checking MxN times
        break;
    }
    if(flag==1) // checking M times
        break;
}

for(M){
    for(N){
        for(K){
            if(condition is true){
                break break break;  // exit from three loops
            }
        }
    }
}

We can eliminate :
1. No extra *flag* variables
2. No need to put *if(condition)*

Correct me if there is something wrong or there is already an option to
optimize it.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_2450_1281209182.1420389707975
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Simple case: if <b>condition</b> is satisfied i want to br=
eak from all loops.<br><br>Example : <br><br>for(M){<br>&nbsp;&nbsp; &nbsp;=
for(N){&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(K){=
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &=
nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(condition is true){<br>&nbsp;=
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;flag =
=3D 1;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&n=
bsp; &nbsp;break;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nb=
sp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp=
;&nbsp; &nbsp;if(flag=3D=3D1) //&nbsp;  worst case checking MxN times<br>&n=
bsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;break;<br>&nbsp;&nbsp; &nbsp;}<br>&nbsp=
;&nbsp; &nbsp;if(flag=3D=3D1) // checking M times<br>&nbsp;&nbsp; &nbsp;&nb=
sp;&nbsp; &nbsp;break;<br>}<br><br>for(M){<br>&nbsp;&nbsp; &nbsp;for(N){&nb=
sp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(K){&nbsp;&nbs=
p; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br>&nbsp;&nbsp; &nbsp;&nbsp=
;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(condition is true){<br>&nbsp;&nbsp; &nb=
sp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;break break bre=
ak;&nbsp; // exit from three loops<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp=
;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br>&nbsp;&=
nbsp; &nbsp;}<br>}<br><br>We can eliminate :<br>1. No extra <b>flag</b> var=
iables<br>2. No need to put <b>if(condition)</b><br><br>Correct me if there=
 is something wrong or there is already an option to optimize it.</div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_2450_1281209182.1420389707975--
------=_Part_2449_974857735.1420389707974--

.


Author: Bo Persson <bop@gmb.dk>
Date: Sun, 04 Jan 2015 17:54:24 +0100
Raw View
On 2015-01-04 17:41, Rupesh Yadav wrote:
> Simple case: if *condition* is satisfied i want to break from all loops.
>
> Example :
>
> for(M){
>      for(N){
>          for(K){
>              if(condition is true){
>                  flag = 1;
>                  break;
>              }
>          }
>          if(flag==1) //  worst case checking MxN times
>          break;
>      }
>      if(flag==1) // checking M times
>          break;
> }
>
> for(M){
>      for(N){
>          for(K){
>              if(condition is true){
>                  break break break;  // exit from three loops
>              }
>          }
>      }
> }
>
> We can eliminate :
> 1. No extra *flag* variables
> 2. No need to put *if(condition)*
>
> Correct me if there is something wrong or there is already an option to
> optimize it.
>

Check 1 - does it need to be optimized? Is MxN if-statements really
significant compared to MxNxK other operations?


Option 1 - put the loop in a separate function and use return instead of
break.

Option 2 - use a goto. :-)  Be sure to measure that the goto doesn't
interfere with the compiler's loop optimizations.


Bo Persson


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

.


Author: "'James Dennett' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Mon, 5 Jan 2015 19:23:46 -0800
Raw View
--001a113447f2b83f0d050bf355e0
Content-Type: text/plain; charset=UTF-8

On Sun, Jan 4, 2015 at 8:41 AM, Rupesh Yadav <7yadavrupesh@gmail.com> wrote:

> Simple case: if *condition* is satisfied i want to break from all loops.
>
> Example :
>
> for(M){
>     for(N){
>         for(K){
>             if(condition is true){
>                 flag = 1;
>                 break;
>             }
>         }
>         if(flag==1) //  worst case checking MxN times
>         break;
>     }
>     if(flag==1) // checking M times
>         break;
> }
>
> for(M){
>     for(N){
>         for(K){
>             if(condition is true){
>                 break break break;  // exit from three loops
>             }
>         }
>     }
> }
>
> We can eliminate :
> 1. No extra *flag* variables
> 2. No need to put *if(condition)*
>
> Correct me if there is something wrong or there is already an option to
> optimize it.
>

See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3879.pdf for a
proposal from Andrew Tomazos that includes something similar to (but IMO
better than) this.

Note, however, that the proposal was rejected by EWG in Rapperswil in 2014.

-- James

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--001a113447f2b83f0d050bf355e0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
un, Jan 4, 2015 at 8:41 AM, Rupesh Yadav <span dir=3D"ltr">&lt;<a href=3D"m=
ailto:7yadavrupesh@gmail.com" target=3D"_blank">7yadavrupesh@gmail.com</a>&=
gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);bord=
er-left-style:solid;padding-left:1ex"><div dir=3D"ltr">Simple case: if <b>c=
ondition</b> is satisfied i want to break from all loops.<br><br>Example : =
<br><br>for(M){<br>=C2=A0=C2=A0 =C2=A0for(N){=C2=A0=C2=A0 =C2=A0<br>=C2=A0=
=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0for(K){=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=
=A0=C2=A0=C2=A0 =C2=A0<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=
=A0 =C2=A0if(condition is true){<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0flag =3D 1;<br>=C2=A0=C2=A0 =C2=A0=C2=
=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0break;<br>=C2=A0=C2=
=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0}<br>=C2=A0=C2=A0 =C2=A0=C2=
=A0=C2=A0 =C2=A0}<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0if(flag=3D=3D1) =
//=C2=A0  worst case checking MxN times<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =
=C2=A0break;<br>=C2=A0=C2=A0 =C2=A0}<br>=C2=A0=C2=A0 =C2=A0if(flag=3D=3D1) =
// checking M times<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0break;<br>}<br=
><br>for(M){<br>=C2=A0=C2=A0 =C2=A0for(N){=C2=A0=C2=A0 =C2=A0<br>=C2=A0=C2=
=A0 =C2=A0=C2=A0=C2=A0 =C2=A0for(K){=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=
=C2=A0=C2=A0 =C2=A0<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =
=C2=A0if(condition is true){<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=
=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0break break break;=C2=A0 // exit from th=
ree loops<br>=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0}<br>=
=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0}<br>=C2=A0=C2=A0 =C2=A0}<br>}<br><br=
>We can eliminate :<br>1. No extra <b>flag</b> variables<br>2. No need to p=
ut <b>if(condition)</b><br><br>Correct me if there is something wrong or th=
ere is already an option to optimize it.</div></blockquote><div><br></div><=
div>See <a href=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/=
n3879.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3879.pd=
f</a> for a proposal from Andrew Tomazos=C2=A0that includes something simil=
ar to (but IMO better than) this. =C2=A0</div><div><br></div><div>Note, how=
ever, that the proposal was rejected by EWG in Rapperswil in 2014.</div><di=
v><br></div><div>-- James</div><div><br></div></div></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a113447f2b83f0d050bf355e0--

.